Public Group

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

## Recommended Posts

i always wonder if ....
MyStruct tStruct = {0};

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

? Lazzar

##### Share on other sites
Quote:
 Original post by Lazzari 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 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 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 on other sites
Quote:
Original post by doynax
Quote:
 Original post by Lazzari 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 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 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 on other sites
Which is why I posted my original message.
Then I read the OP's remark that he was using C.

##### Share on other sites
Quote:
 Original post by doynaxI'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 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.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631777
• Total Posts
3002306
×