In gcc you can do the following:

char *p = "ABC";
p[0] = 'a';

The compiler will not complain but the program will crash with Segmentation fault (at least gcc 4.6.3 on my amd64 machine). gcc puts literal strings in ro section ( .section .rodata gas directive). Interesting that

"ABC"[0] = 'a';

throws the "warning: assignment of read-only location ‘"AAA"[0]’ [enabled by default]" warning.

You can use -Wwrite-strings command line option to turn "warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]" warnings on or use -fno-const-strings to put strings in normal (no ro) section.

I don't know what is written in the standard but IMHO it is better that the compiler complains when the constant literal string is assigned to non-constant pointer since it (potentially) causes the run time error (which is never shown in sdcc case).

Putting literals strings into rw memory by default doesn't make sense in embedded systems since it is an overhead in RAM, ROM and instruction cycles consumption.

An option would be to implement -fno-const-strings in sdcc too, so the user can choose where the string literals are allocated...

And now the answer to your question from my point of view: in sdcc string literal is constants an the compiler should throw an error when assigning it to a non constant char pointer.


On 25. 10. 2012 01:06, Maarten Brock wrote:
Hi all,

In recent regression tests there are a few warnings about pointers losing
their const qualifier in code like this:
char *s = "abc";

Now my question is if this "abc" string literal is constant or not?
SDCC thinks it is because it likes to put it in unmodifiable code memory
But GCC thinks it's not. It even allows *s='A'; So apparently it didn't
even bother to put it in a write-protected segment for the MMU.

Can anyone point to the definitive answer?


Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
sdcc-devel mailing list