Jump to content
Alistair George

Initialise/initialize Variables At Start

Recommended Posts

Hi according to previous C I've used, char variables at declaration were default value 0.

It seems that this is not so with BoostC?

If not, whats the preferred method to initialise a bunch of variables at same time.

eg must one do v1=0;v2=0;v3=0;

not v1=v2=v3=0;

Cheers,

Al

PS understood that assigning variables at start can use memory, but only if some of the variables are not being used correct?

Share this post


Link to post
Share on other sites

If you declare a variable at 'static' it will be initialised to zero unless you tell the compiler otherwise (BoostC manual page 22).

 

If you wish to zero a bunch of variables to zero use the v1=0, v2=0 etc method. The other method does not zero all your variables, only the last one.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

No, it's the other way around. SourceBoost will initialise your variables to 0 unless you have

 

-Su

( disable initialization of uninitialized static variables)

 

on the BoostC command line.

Share this post


Link to post
Share on other sites
Isn't that what I said Ian ?

 

:-)

 

Okay to be clear:

 

if you don't explicitly initialise a variable, eg,

 

 

int my_variable;

 

it *will* be initialised to 0 unless you use the command line switch.

 

In a lot of cases it really doesn't matter what the initial value is, you can save memory by not forcing everything to zero.

 

cheers

Ian.

Share this post


Link to post
Share on other sites
Isn't that what I said Ian ?

 

:-)

 

Okay to be clear:

 

if you don't explicitly initialise a variable, eg,

 

 

int my_variable;

 

it *will* be initialised to 0 unless you use the command line switch.

 

In a lot of cases it really doesn't matter what the initial value is, you can save memory by not forcing everything to zero.

 

cheers

Ian.

Actually, I could stand to be corrected on this, but the reason I was initializing was due to some startup numbers not being at zero. Could always be me of course!

Cheers,

Al.

Share this post


Link to post
Share on other sites

Sorry Ian I agree with Reynard and Al.

 

A variable is only initialised to zero if it is declared static AND the -Su switch is not invoked. All other variables are left uninitialised.

 

Regards

 

davidb

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...