The {0} initialization zeros out the entire struct, not just the first
member of the struct.
These two are functionally equivalent:

char foo[10] = {0};
MyStruct bar = {0};

and

char foo[10];
memset(foo, 0, sizeof(foo));
MyStruct bar;
memset(&bar, 0, sizeof(bar));

On Sun, Mar 22, 2009 at 2:36 PM, Paul Topping <[email protected]> wrote:

>
> Your {0} initialization only inits the first element of the struct (or the
> whole struct if it consists of a single element), whereas memset inits the
> struct completely to 0 regardless of its size or internal structure.
>
> On the other hand, zeroing an entire struct with memset is dangerous unless
> it is guaranteed to be POD. If someone later changes the struct such that it
> is no longer POD, memset will still compile and run but it may overwrite
> some struct member constructor's work.
>
> Paul
>
> > -----Original Message-----
> > From: [email protected] [mailto:chromium-
> > [email protected]] On Behalf Of Book'em Dano
> > Sent: Sunday, March 22, 2009 11:30 AM
> > To: Chromium-dev
> > Subject: [chromium-dev] Re: Quick question about struct initialization
> >
> >
> > Sorry if I wasn't clear enough, but that's actually what my question
> > is.
> >
> > What's the need/point of calling memset if initialization via " = {0};
> > " works just as well. Are you saying that it doesn't? Or are you
> > saying that you're not quite sure, and to be on the safe side, do
> > both. I'd like to avoid doing work (even if only 1 a liner) that isn't
> > needed. i'd like to put this to rest once and for all, so if anyone
> > knows, could please help me out here.
> >
> > Cheers,
> > Danny
> >
> > On Mar 22, 5:23 am, Smita <[email protected]> wrote:
> > > It works fine with gcc.
> > > However, you might want to initialize the entire array to 0 using a
> > > memset.
> > >
> > > Smita
> > >
> > > On Mar 21, 10:23 pm, "Book'em Dano" <[email protected]> wrote:
> > >
> > > > Can someone please confirm whether it's safe to initialize a POD
> > > > struct using:
> > >
> > > > MyStruct blat = {0};
> > >
> > > > with gcc on Linux/Mac? I know this works fine with the VC compiler,
> > > > but I dont have gcc handy.
> > >
> > > > Thanks,
> > > > D
> >
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
Chromium Developers mailing list: [email protected] 
View archives, change email options, or unsubscribe: 
    http://groups.google.com/group/chromium-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to