This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Unomitted frame pointers


> -----Original Message-----
> From: gcc-owner On Behalf Of jlh
> Sent: 10 December 2004 16:05
> To: Thomas R. Truscott

> > Off-topic, but the hello.c program does:
> > 	write(2, "Hello World!\n\0", 16);
> > The string length is 15, not 16.
> 
> 15 is what gets allocated for storing the string (counting
> the additional \0 that always gets appened to string constants).
> But neither the explicit nor the implicit \0 should be printed
> to the console, so the call should definitely read:
> 
>      write(2, "Hello World!\n", 13);
> 
> jlh


  Except of course on targets where '\n' expands to a two char CR-LF
combination.

  So really the safe way to code this is

#define HELLOSTRING "Hello World!\n"

      write (2, HELLOSTRING, strlen(HELLOSTRING)-1);

and let the compiler statically compute the string length for you at
compiletime.  The #define is used to make sure we don't end up with two copies
of the string that get out of sync, as would be bound to happen sooner or later
if we wrote

      write (2, "Hello World!\n", strlen("Hello World!\n")-1); 

    cheers, 
      DaveK
-- 
Can't think of a witty .sigline today....


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]