[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Initialization of struct to zero with a cast

Voyageur Galactique

2/10/2011 9:48:00 AM

Hi,

Suppose I have a struct T .

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

I want to initialize t to zero.
21 Answers

Voyageur Galactique

2/10/2011 9:51:00 AM

0

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

David RF

2/10/2011 10:51:00 AM

0

On 10 feb, 10:51, Voyageur Galactique <voyageur.galacti...@gmail.com>
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/initi...

Voyageur Galactique

2/10/2011 11:42:00 AM

0

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 <davran...@gmail.com> wrote:
> On 10 feb, 10:51, Voyageur Galactique <voyageur.galacti...@gmail.com>
> 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/initi...

Ben Bacarisse

2/10/2011 12:36:00 PM

0

Voyageur Galactique <voyageur.galactique@gmail.com> 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.

Tom St Denis

2/10/2011 12:43:00 PM

0

On Feb 10, 4:47 am, Voyageur Galactique
<voyageur.galacti...@gmail.com> 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

Ben Bacarisse

2/10/2011 12:45:00 PM

0

Voyageur Galactique <voyageur.galactique@gmail.com> 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.

David RF

2/10/2011 12:47:00 PM

0

On 10 feb, 12:42, Voyageur Galactique <voyageur.galacti...@gmail.com>
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

Voyageur Galactique

2/10/2011 1:51:00 PM

0

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

Voyageur Galactique

2/10/2011 1:54:00 PM

0

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

Voyageur Galactique

2/10/2011 2:20:00 PM

0

I thank you all for your instructive answers.

Just a few pieces informations I found about compound literals
regarding portability :

- "C++ supports this feature as an extension to Standard C++ for
compatibility with C"
quoting :
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc02compound_li...

- "As an extension, GCC supports compound literals in C90 mode and in C
++."
quoting :
http://gcc.gnu.org/onlinedocs/gcc-4.5.2/gcc/Compound-Lit...

- Chances are it is not supported in MSVC but I didn't tried.

To finish, an alternate solution to add to my collection :
found in :
http://gcc.gnu.org/onlinedocs/gcc-4.5.2/gcc/Compound-Lit...

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