Jump to content
  • Advertisement
Sign in to follow this  
Plasmator

Initialization

This topic is 4419 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

Hello everyone, I've got a question... Look at this code,
struct BLA
{
int a;
double b;
char c;
};

//in main
BLA bla = {0};


From what I understand this will initialize the first variable to 0 (int a), but it seems that this is also true for b, and c! Also I've noticed that this works for arrays, like, char bla[256] = {0}; is this a valid way of setting an objects member variables, or is it just my compiler that's nulling everythiing because I'm in running in debug mode? Thanks in advance! P.S Also, if this is indeed a valid method of setting values to all of an objects members, is there any way to do this when you're using dynamic allocation? (malloc / new)? Thanks again...

Share this post


Link to post
Share on other sites
Advertisement
From what I can tell, all other veriables must be specifically set, otherwise they will all be filled with the value you specify first. bla={0} would make them all 0, but bla={0, 1, 2} would set a equal to 0, b equal to 1, and c equal to 2.

Share this post


Link to post
Share on other sites
Quote:
Original post by Plasmator
Hello everyone, I've got a question...
From what I understand this will initialize the first variable to 0 (int a), but it seems that this is also true for b, and c! Also I've noticed that this works for arrays, like, char bla[256] = {0}; is this a valid way of setting an objects member variables, or is it just my compiler that's nulling everythiing because I'm in running in debug mode?


Is your code in C? If yes, you should be doing struct Bla bla; instead.

What you are intializing is an aggregated structure. Initializing just one member of this structure, or an array (e.g.: array = {0}), will initialize the rest aswell.

Quote:

P.S
Also, if this is indeed a valid method of setting values to all of an objects members, is there any way to do this when you're using dynamic allocation? (malloc / new)?

Thanks again...


I don't think so unless, you, for example use a function such as calloc (in C).

- xeddiex

Share this post


Link to post
Share on other sites
Quote:
Original post by programwizard
From what I can tell, all other veriables must be specifically set, otherwise they will all be filled with the value you specify first. bla={0} would make them all 0, but bla={0, 1, 2} would set a equal to 0, b equal to 1, and c equal to 2.


Not quite. When you initialize a struct or class or array like that, the members not explicitly initialized are set to 0. For example, in this code
BLA bla = { 2 }; 
bla.a is 2 and bla.b and bla.c are 0.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!