Sign in to follow this  
Lazzar

Initializing Structures

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
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
Quote:
Original post by Paradigm Shifter
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}.

C does allow constant initialization of local structures. At least K&R claims that it is in at least two places.
It seems strange that different rules would apply to structure with fewer initializers than for for arrays. Again according to my copy of K&R excess elements of auto arrays are undefined. It even says that structure initializations are analgous to array intializations, unfortunately it doesn't explicitly state what's supposed to happend.

This would be so much easier if I could just find a free copy of the standard somewhere..

Share this post


Link to post
Share on other sites
i wondered why i can't find the complete ANSI C definitions online ... it is cause its not free, ANSI is living from selling standards heh.

anyway there ->
It is the DEC C definition, but compatible to ANSI C.

http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V40F_HTML/AQTLTBTE/DOCU_047.HTM#initializing_structures_sec

as SiCrane already said is also true in ansi c:

-- snip
If there are fewer initializers than members for a structure, the remaining members are initialized to 0. Listing too many initializers for the number of components in a structure is an error
-- snip

Lazzar

Share this post


Link to post
Share on other sites
Page 85 of K&R2 (swedish edition) clearly states that excessive uninitialized members of local auto vectors should be undefined, as an exception to static and global instances which should be zeroed. However on page 225 it goes on to say that they should, without exception, be set to zero.

So now I don't know what to believe anymore. Guess I've just discredited my only source of "reliable" information about the standard.

Share this post


Link to post
Share on other sites
I don't have a copy of the standard either. You could be right, although earlier C compilers (maybe pre-ISO standard) certainly only allowed initialisation with curly brackets of static or global arrays (I learnt C on one such compiler).
Then C++ allows initialisation with brackets, and initialises all uninitialised values to 0 of the appropriate type. (See SiCrane's post).
It is confusing. The thing to do is test it. I have never had problems with the s = {0}; thing not initialising members though, with the gnu C compiler anyway.

Share this post


Link to post
Share on other sites
Oops, looks like I skipped a post when reading through the thread.

Anyways, standard C guarantees the same thing except the verbage is different.
Quote:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

And pointers or arithmetic types with static storage duration are initialized to (positive) 0.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this