>> 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.

> And which of these options are used in our host regression tests? Because
> I don't see the warning.

None.

Borut

On Thu, Oct 25, 2012 at 9:28 AM, Maarten Brock <sourceforge.brock@dse.nl> wrote:
> 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.

Ah, so it segmentation faults if a regression test fails. That's also a
way to fail. (gcc-torture-execute-20051104-1.c)

> 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.

And which of these options are used in our host regression tests? Because
I don't see the warning.

> 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.
>
> Borut

So it seems we agree to disagree with the standard! String literals should
be constant. And warnings should be given. But then I propose to change
the gcc tests to conform and not use char *s = "123" in there.

Maarten

>
> 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
>> (ROM).
>> 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?
>>
>> Maarten


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
sdcc-devel mailing list
sdcc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-devel