Guess the result of following code.
void test1()
{
char str[] = "hello"; // string copy.
printf("%s\n", str);
*str = 'H'; // copied data would be changed.
}
void test2()
{
char* str = "world"; // static data pointer assignment to str variable.
printf("%s\n", str);
*str = 'W'; // static data area would be changed(acccessible or not?).
}
int main()
{
test1();
test1();
test2();
test2();
}
[Result - CodeGear C++Builder 2007 - Release Mode, Optimization None]
hello
hello
world
World
[Result - Microsoft Visual Studio 2005 - Release Mode, Optimization Disabled (/Od)]
hello
hello
world
******************************************************
EXCEPTION_ACCESS_VIOLATION 0xc0000005
Code: 0xc0000005
Flags: Continuable
Exception Record: 0x00000000
Address: 0x00401BC0
Number of Parameters: 2
------------------------------------------------------
Attempt to write inaccessible data at 0x40250c
******************************************************
[Conclusion]
In C++Builder, data in static data area are able to be changed by application source code level in run time, while in Visual Studio, they are not.
[Downlload]