Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: Problems with sting constants and gcc -On

2005-10-19 by bdmlpc

Hello Robert,

sorry for this last remark, but it is not an interpretation of C
standard. 

char* f1 = "Hello";

In this case the compiler will allocate exact 4 bytes to store a
pointer to "Hello" in it. If you use 'f' (without an index) the
pointer will be loaded from its 4 byte position in memory. That's why
"Hello" is treated as constant initializer (by most compilers). It may
be tollerated by some compilers.

char f2[] = "Hello";       or
char f2[5+1] = "Hello";

In this case the compiler will allocate 6 bytes (5 + end delimieter
'\0') which are initialized with "Hello" in RAM. If you use 'f' the
pointer to this 6byte-array is used as an immediate value in assembler
because its position in RAM is fixed. See the following assignments.

Just try following assignments:

char buff1[] = "Hello";
char buff2[] = "World";
char* p1;
const char* p2 = "0123456";

p1 = buff1[];         //Is OK
p1[2] = 'L';          //Is OK
printf("%s", buff);   //Will print "HeLlo"
p1++;                 //Is Ok
printf("%s", p1);     //Will print "eLlo"
printf("%c", p2[3]);  //Will print "3"
p2++;                 //Is ok, because only "0123456" is 
                      //constant, not the pointer itself

buff1 = buff2;        //Is not OK
buff1 = p1;           //Is not OK
buff++;               //Is not OK

Last tree lines will result in following errors: 

error: incompatible types in assignment
error: incompatible types in assignment
error: wrong type argument to increment

because you try to change an "immediate" pointer. On the other hand
you would loose the reference to "Hello".

Due to C's complexity and the possibility to create code that nobody
else can understand than the programmer himself, there's a nice slogan
(I hope my translation is correct): "C is the language, where
programmers can create monuments of their brilliancy!" ;-) 
Pointers and casts are my favourite and the best elements to protect
your code against others (if neccessary). This is called
job-protection! ;-)

   Sten

--- In lpc2000@yahoogroups.com, Robert Adsett <subscriptions@a...> wrote:
>
> At 09:08 PM 10/18/05 +0000, Guillermo Prandi wrote:
> >Actually:
> >
> >char *f = "Hello";
> >
> >Left "Hello" in the ROM area. Whilst:
> >
> >char f[] = "Hello";
> >
> >Left "Hello" in the RAM area (after copying from ROM, of course).
> >
> >Guille
> 
> I rather suspect that's an allowed interpretation of the standard 
> then.  GCC is quite good about that.  It certainly follows the
practice of 
> some UNIX compilers.  Unfortunately that's a non-obvious type pun.
> 
> Thankfully lint (PC-Lint) catches it (although as a C++, not a C error, 
> Makes sense; AIUI the type of a string literal is different in the two 
> languages.)
> 
> char *f = "test";
> e:\cygwin\home\radsett\newlib-lpc\test11.c  33  Info 1776:
Converting a string
>      literal to char * is not const safe (initialization)
> 
> Thanks, another hole in my knowledge corrected.
> 
> Robert
> 
> 
> " 'Freedom' has no meaning of itself.  There are always
restrictions,   be 
> they legal, genetic, or physical.  If you don't believe me, try to
chew a 
> radio signal. "  -- Kelvin Throop, III
> http://www.aeolusdevelopment.com/
>

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.