Initializing Structures

Started by
13 comments, last by SiCrane 19 years, 4 months ago
i always wonder if ....

MyStruct tStruct = {0};
is the same as:

MyStruct tStruct;
memset(&tStruct,0,sizeof(MyStruct));
? Lazzar
---------------------------------------------------------if god gave us the source code, we could change the world!
Advertisement
Quote:Original post by Lazzar
i always wonder if ....
MyStruct tStruct = {0};


the same as:
MyStruct tStruct;memset(&tStruct,0,sizeof(MyStruct));

?

Lazzar

No. That just sets the structure's first member to zero.
When initializing a structure like that you can freely choose how many of it's members that you want to set.
There's not even any guarantee that the data type's representation of zero is a binary zero (i.e. for floating-point values and pointers on odd systems).
Also note that it kinda works for static instances ("The default initializer for a structure with static storage is the recursive default for each component; a structure with automatic storage has none.") although they would've been initialized to zero anyway.
i thoght so :(

but how can i avoid the following Warning then?

"myfile.c", line 195.1: 1506-412 (I) Referenced variable "tMyStruct", which was not initialized in its declaration.

Is there any other way to initialize ALL fields of Structure as soon as i create one?

In c not c++.

Lazzar
---------------------------------------------------------if god gave us the source code, we could change the world!
Quote:Original post by LazzarIs there any other way to initialize ALL fields of Structure as soon as i create one?
The easies way would be to create an uninitialized static instance, that's guaranteed to initialized all fields to proper zero values. It might be a better idea to explicitly initialized all of them manually though, possibly even through a reset function (i.e. a construtor).

I'm not exactly sure why your complier is issuing a warning though, there's nothing fundamentally bad with uninitialized values. There's even cases where you just don't care what the initial value is and still base your calculations on it. Maybe you could just disable the warning?
Quote:Original post by doynax
Quote:Original post by Lazzar
i always wonder if ....
MyStruct tStruct = {0};


the same as:
MyStruct tStruct;memset(&tStruct,0,sizeof(MyStruct));

?

Lazzar

No. That just sets the structure's first member to zero.


No it doesn't. It sets all members to zero. If you miss off excess elements at the end of the initialiser list it sets the remaining elements to a zero of the appropriate type. Even if they are non-statically allocated structs.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
After a bit more research, initialising local struct's isn't allowed in ISO C at all (only static and global), although all compilers I have used have supported this, and they have initialised every element to zero with struct s = {0}.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
According to the C++ standard, section 8.5.1 paragraph 7 (ISO 14882:1998, I don't have 14882:2003 handy):
Quote:
If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default initialized (8.5).


And default initialization for a scalar type is equivalent to assigning 0 to it.
Which is why I posted my original message.
Then I read the OP's remark that he was using C.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Quote:Original post by doynax
I'm not exactly sure why your complier is issuing a warning though, there's nothing fundamentally bad with uninitialized values. There's even cases where you just don't care what the initial value is and still base your calculations on it. Maybe you could just disable the warning?


We are working in a big Project.
Many different programmers are hacking in the same CVS Tree.

And unitialized variables (return values, pointers, Strucs!) can sometimes cause several hours of debugging for our Support Team.

Static Variables are not an option, i usualy try to avoid them.

Quote:No it doesn't. It sets all members to zero. If you miss off excess elements at the end of the initialiser list it sets the remaining elements to a zero of the appropriate type. Even if they are non-statically allocated structs


And thats exactly what i wanted to hear :)


Thanks
Lazzar
---------------------------------------------------------if god gave us the source code, we could change the world!
Don't take my word for it though, as I said, it shouldn't really be allowed in vanilla C, but most compilers treat it like the C++ standard says it should.
Look at the values in the debugger or put some assert's in your code to be sure.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement