I have below programme where memory is read-only, but I am still able to write to it.
main()
{
char *p="amit";
p[0]="s";
printf("%s",p);
}
The output is "smit"
Is it the expected output since p
points a location that is read-only? When I run this programme on GCC or on Visual C++, I get a segmentation fault, but on Turbo C++ I get "smit".
Please confirm this behaviour...
Writing to a string literal gives undefined behavior, so the compiler is allowed to do anything it wants when you do that.
The simple answer is that Turbo C++ compiles for real-mode, so none of the memory is protected. Neither the compiler nor the OS will stop you from writing anything anywhere.