An Easy Question
-
In Release mode with VS2008, Why "var" prints "Whats up" instead of "newval" It crashes in debug mode. I guess the reason might be the auto-memory allocation is "read-only". If I use char *var = new char[15] then it works fine.
#include <stdio.h>
#include <conio.h>
#include <string>int main()
{
char *var = "\nWhats Up";
strcpy(var, "newval");
printf(var);
getch();
return 0;
} -
In Release mode with VS2008, Why "var" prints "Whats up" instead of "newval" It crashes in debug mode. I guess the reason might be the auto-memory allocation is "read-only". If I use char *var = new char[15] then it works fine.
#include <stdio.h>
#include <conio.h>
#include <string>int main()
{
char *var = "\nWhats Up";
strcpy(var, "newval");
printf(var);
getch();
return 0;
}When you create
var
in this way it is pointing to a constant string so yourstrcpy()
command is not allowed. What you should code is:char \*var = "\\nWhats Up"; var = "newval";
// or
char var[] = "\nWhats Up";
strcpy(var, "newval");although in the second case you could easily overwrite the buffer if you are not careful.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
When you create
var
in this way it is pointing to a constant string so yourstrcpy()
command is not allowed. What you should code is:char \*var = "\\nWhats Up"; var = "newval";
// or
char var[] = "\nWhats Up";
strcpy(var, "newval");although in the second case you could easily overwrite the buffer if you are not careful.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
Thanks, I got it
-
When you create
var
in this way it is pointing to a constant string so yourstrcpy()
command is not allowed. What you should code is:char \*var = "\\nWhats Up"; var = "newval";
// or
char var[] = "\nWhats Up";
strcpy(var, "newval");although in the second case you could easily overwrite the buffer if you are not careful.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
Richard MacCutchan wrote:
although in the second case you could easily overwrite the buffer if you are not careful.
Did you mean overrun, didn't you? BTW: Good answer, my 5.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Richard MacCutchan wrote:
although in the second case you could easily overwrite the buffer if you are not careful.
Did you mean overrun, didn't you? BTW: Good answer, my 5.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Well spotted! Odd how it looked quite sensible when I wrote it.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman