Jump to content
  • Advertisement
Sign in to follow this  
Lazzar

Initializing Structures

This topic is 4982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i always wonder if ....
MyStruct tStruct = {0};
is the same as:
MyStruct tStruct;
memset(&tStruct,0,sizeof(MyStruct));
? Lazzar

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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}.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!