This content has been marked as final. Show 3 replies
In C++, a literal string has type "array of const char". You are using a literal to initialize a pointer to non-const. The compiler is warning you about a potential run-time error. You are allowed to assign via the pointer, but trying to change a const value has undefined results.
By default in Sun C++, literal strings are put into read-only memory. You will get an immediate program crash if you attempt to modify a literal string, such as by assigning via the pointer.
If you don't intend to change what the "value" members of the struct point to, you should delcare them as "const char*". If you then attempt to assign via the pointers, you will get a compile-time error instead of a run-time crash.
The -features=no%conststrings option causes literal strings to be put into read-write memory, so you won't get an immediate program crash when you modify the string. But modifying the literal could cause a program crash somewhere else. Example:
In this example, we over-write the terminating null of "1234" with '5', so printf just keeps trying to print characters until it (by accident) finds a null byte, or runs off the end of the program and crashes.
char*p="1234"; p='5'; printf("%s", p);
If you intend to modify what the "value" members point to, you should allocate memory and copy the literal strings into the allocated memory.
Why does the code cause a fault, when the author is attempting to only read the contents, via the printf, not modify (write) it?
Do you mean the example at the end of the previous posting?
The programmer replaced the terminating null of the string with a non-null character.
The %s modifier to printf says to print characters from the starting address up to the next null byte. After printing 5 characters from the string, printf continues to read and print characters, looking for a null byte. There is no guarantee that a null byte will be seen before hitting the end of allocated memory. At that point, you get a memory violation and the program crashes.
If it happened that there was a null byte before the end of memory, the program would not crash at that point, but printf would output some number of inappropriate characters. You might wind up printing a control character to a device that caused misbehavior. If you were using sprintf instead of printf, you could write beyond the end of the allocated sprintf buffer, with unpredictable results.