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


We would end up with two copies of the string. #defines are preprocessed, so it would expand to the second thing. The _safe_ way to do it is

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

Then we can be sure that it refers to the same physical address in RAM. "Two copies" is when the strings are positioned at diffrent non-overlapping addresses. Though optimization and inlining might mess it up.

Samuel Lauber

----- Original Message -----
From: "Dave Korn" <dave.korn@artimi.com>
To: "'jlh'" <jlh@gmx.ch>, "'Thomas R. Truscott'" <trt@cs.duke.edu>
Subject: RE: Unomitted frame pointers
Date: Fri, 10 Dec 2004 16:23:59 -0000

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

-- 
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze


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