memory access error in some simple code :)

P

Pavel Sorokin

Hello,

This is probably a very dumb question, but I don't understand what's
the problem with the following code. It gives a memory access
violation on the marked line. I'm using VC++ 7.1.

void reverse(char* str, int start, int end)
{
for(int i=start; i<(start+end)/2; i++)
{
char buf = str;
int endIndex = end-i;
str = str[endIndex]; // <-- this causes an error
str[endIndex] = buf;
}
}

Thanks,
Pavel
 
A

Artie Gold

Pavel said:
Hello,

This is probably a very dumb question, but I don't understand what's
the problem with the following code. It gives a memory access
violation on the marked line. I'm using VC++ 7.1.

void reverse(char* str, int start, int end)
{
for(int i=start; i<(start+end)/2; i++)
{
char buf = str;
int endIndex = end-i;
str = str[endIndex]; // <-- this causes an error
str[endIndex] = buf;
}
}

The code itself -- while it could be improved -- is fine. The problem
(he said, turning up the sensitivity of his crystal ball) is the way
you're calling the function.

No doubt you're doing something like:

char * something = "reverse me";
reverse(something);

In this case you're trying to alter the value of a string literal (which
should more properly be defined as a `const char *') which is not
permissable.

HTH,
--ag
 
J

John Harrison

Hello,

This is probably a very dumb question, but I don't understand what's
the problem with the following code. It gives a memory access
violation on the marked line. I'm using VC++ 7.1.

void reverse(char* str, int start, int end)
{
for(int i=start; i<(start+end)/2; i++)
{
char buf = str;
int endIndex = end-i;
str = str[endIndex]; // <-- this causes an error
str[endIndex] = buf;
}
}

Thanks,
Pavel


Could be anything, str could be NULL, str could be garbage, end could be
too big, end could be negative, start could be too big, start could be
negative.

Either check the values for yourself, or post a complete program. There
isn't enough information to solve your problem in what you have posted.

john
 
A

Anil Mamede

Pavel said:
Hello,

This is probably a very dumb question, but I don't understand what's
the problem with the following code. It gives a memory access
violation on the marked line. I'm using VC++ 7.1.

void reverse(char* str, int start, int end)
{
for(int i=start; i<(start+end)/2; i++)
{
char buf = str;
int endIndex = end-i;
str = str[endIndex]; // <-- this causes an error
str[endIndex] = buf;
}
}

Thanks,
Pavel


Maybe endIndex is bigger that the char* length. Try to use the debugger
step by step and watch the endIndex value.


BTW,

char string[40] = "Hello World";

Suppose reserve(string, 4, 7);

In the first iteration:
i = start = 4
end = 7
endIndex = 7 - 4 = 3

buf = str[4] = 'o'
char[3] = 'l'
str = "Helol World";

Next iteration:

i = start = 5
end = 7
endIndex = 7 - 5 = 2

buf = str[5] = ' '
char[2] = 'l'
str = "He ollWorld";

You want that result above or this:

"HelloW oorld"?


Anil Mamede
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top