Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Initialization of struct to zero with a cast

Reply
Thread Tools

Initialization of struct to zero with a cast

 
 
Voyageur Galactique
Guest
Posts: n/a
 
      02-10-2011
Hi,

Suppose I have a struct T .

struct T* t = malloc(sizeof(struct T));

I want to initialize t to zero.
 
Reply With Quote
 
 
 
 
Voyageur Galactique
Guest
Posts: n/a
 
      02-10-2011
Hi,

Suppose I have a struct T .

struct T* t = malloc(sizeof(struct T));

I want to initialize t to zero.

I did it in the following way :

*t = (struct T) {0};

I would like to know if it is safe ?
From my experience, it works well with gcc, but is it guaranteed to
work on other compiler ?

Thanks
 
Reply With Quote
 
 
 
 
David RF
Guest
Posts: n/a
 
      02-10-2011
On 10 feb, 10:51, Voyageur Galactique <(E-Mail Removed)>
wrote:
> Hi,
>
> Suppose I have a struct T .
>
> struct T* t = malloc(sizeof(struct T));
>
> I want to initialize t to zero.
>
> I did it in the following way :
>
> *t = (struct T) {0};
>
> I would like to know if it is safe ?
> From my experience, it works well with gcc, but is it guaranteed to
> work on other compiler ?
>
> Thanks


http://www.ex-parrot.com/~chris/random/initialise.html
 
Reply With Quote
 
Voyageur Galactique
Guest
Posts: n/a
 
      02-10-2011
Thank you for pointing me this artcile.

I answers one part of my question.

If I well understand, the statement :
struct T t = {0}
will set the fist field to 0 and initialize other fields the same way
it does for static storage.

A practical example is the following :

struct T t_init = {0}, *t = malloc(sizeof(struct T));
*t = t_init;

It is equivalent to :

static struct T t_init;
struct T* t = malloc(sizeof(struct T));
*t = t_init;

But what about the casting technique ?

struct T* t = malloc(sizeof(struct T));
*t = (struct T) {0};

It works, but I'm not sure it should.


On Feb 10, 11:51*am, David RF <(E-Mail Removed)> wrote:
> On 10 feb, 10:51, Voyageur Galactique <(E-Mail Removed)>
> wrote:
>
>
>
> > Hi,

>
> > Suppose I have a struct T .

>
> > struct T* t = malloc(sizeof(struct T));

>
> > I want to initialize t to zero.

>
> > I did it in the following way :

>
> > *t = (struct T) {0};

>
> > I would like to know if it is safe ?
> > From my experience, it works well with gcc, but is it guaranteed to
> > work on other compiler ?

>
> > Thanks

>
> http://www.ex-parrot.com/~chris/random/initialise.html


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-10-2011
Voyageur Galactique <(E-Mail Removed)> writes:

> Suppose I have a struct T .
>
> struct T* t = malloc(sizeof(struct T));
>
> I want to initialize t to zero.


The page that someone else has pointed you to explains that "zero" is,
sadly, slightly ambiguous in this context.

> I did it in the following way :
>
> *t = (struct T) {0};
>
> I would like to know if it is safe ?


Yes, that is absolutely safe. It will work even when a null pointer (or
a floating point zero) is not all bits zero.

> From my experience, it works well with gcc, but is it guaranteed to
> work on other compiler ?


The trouble is that it's C99 -- the newest C standard -- and not all
compilers implement C99.

--
Ben.
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      02-10-2011
On Feb 10, 4:47*am, Voyageur Galactique
<(E-Mail Removed)> wrote:
> Hi,
>
> Suppose I have a struct T .
>
> struct T* t = malloc(sizeof(struct T));
>
> I want to initialize t to zero.


use calloc instead?

Tom
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-10-2011
Voyageur Galactique <(E-Mail Removed)> writes:
<snip>
> But what about the casting technique ?
>
> struct T* t = malloc(sizeof(struct T));
> *t = (struct T) {0};
>
> It works, but I'm not sure it should.


For a conforming C99 compiler yes. Technically it's not a cast. The
construct:

(type-name){ initialisers }

is called a compound literal. It's effect is to make an un-named object
of the given type. A cast produces just a value whereas a compound
literal is an object you can take the address of.

<snip>
--
Ben.
 
Reply With Quote
 
David RF
Guest
Posts: n/a
 
      02-10-2011
On 10 feb, 12:42, Voyageur Galactique <(E-Mail Removed)>
wrote:

> > > I want to initialize t to zero.

>
> > > I did it in the following way :

>
> > > *t = (struct T) {0};

>
> > > I would like to know if it is safe ?


Yes, set all struct members to 0
 
Reply With Quote
 
Voyageur Galactique
Guest
Posts: n/a
 
      02-10-2011
> Technically it's not a cast. The construct:
>
> (type-name){ initialisers }
>
> is called a compound literal. It's effect is to make an un-named object
> of the given type. A cast produces just a value whereas a compound
> literal is an object you can take the address of.
> --
> Ben.


Thank you, it completely answers my question.
I'm very pleased to learn it's something else than a cast. I couldn't
understand how it could ever work.
 
Reply With Quote
 
Voyageur Galactique
Guest
Posts: n/a
 
      02-10-2011
> > struct T* t = malloc(sizeof(struct T));
>
> > I want to initialize t to zero.

>
> use calloc instead?
>
> Tom


Of course not. My question was misleading. I wanted to say,
"initialize *t to zero", or more precisely "intialize all fieds in t*
struct to zero"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
static struct initialization in a Class:: -- not my struct christian.bongiorno@gmail.com C++ 2 09-20-2006 06:53 PM
Can you set a class instance's attributes to zero by setting the instance to zero? Gerard Flanagan Python 3 11-19-2005 06:58 PM
memset all bits to zero will let float/double to zero? Zhiqiang Ye C Programming 53 06-28-2004 01:23 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments