rationale for eliding modifications to string constants

Dave Korn dave.korn.cygwin@gmail.com
Tue Sep 14 22:48:00 GMT 2010


On 14/09/2010 19:47, Uday P. Khedker wrote:

> But may be you are right, what facilitate dead code elimination
> be based on modification of read-only data. However, if that is
> the case, I wonder what is the reason why change happens when s is
> an array...

  Because the array, unlike the string, isn't const.

char *s = (char *)"string";

-> s is a pointer to a string (that the compiler happens to know is constant).

char s[] = "string";

-> s is an array of non-constant chars, initialised at startup from a string
constant; it's the same as writing

char s[] = { 's', 't', 'r', 'i', 'n', 'g', '\0' };

  Arrays and pointers are not the same thing in C!

    cheers,
      DaveK



More information about the Gcc mailing list