A
Albert
If I declare a variable and then write
if ([variable name])
is the result *guarunteed* to be false?
if ([variable name])
is the result *guarunteed* to be false?
If I declare a variable and then write
if ([variable name])
is the result *guarunteed* to be false?
If I declare a variable and then write
if ([variable name])
is the result *guarunteed* to be false?
Albert said:If I declare a variable and then write
if ([variable name])
is the result *guarunteed* to be false?
user923005 said:What is it exactly that you are trying to accomplish.
Albert said:Suppose I had an array of type integer that stored the number of
factors the array index has. If I had a function to work out the
number of factors a positive integer has, could I write
Albert said:user923005 said:What is it exactly that you are trying to accomplish.
Suppose I had an array of type integer that stored the number of
factors the array index has. If I had a function to work out the
number of factors a positive integer has, could I write
if (!factors[index]) calcfactors(index);
James said:If you want to rely upon the values stored in factors[], you'd best
actually make sure that values are stored there. In other words, it
needs to be initialized.
Albert said:James said:If you want to rely upon the values stored in factors[], you'd best
actually make sure that values are stored there. In other words, it
needs to be initialized.
Okay. Thanks.
James Kuyper said:Also if you define an aggregate object (an array or a struct), and you
don't provide enough initializers to fill the whole thing, then the
remaining parts of the object are zero initialized. Therefore, you
could define:
long factors[MAX_FACTORS] = {0};
The 0 would initialize the first factor, and the remaining factors
would all be zero-initialized by default.
Albert said:James said:If you want to rely upon the values stored in factors[], you'd best
actually make sure that values are stored there. In other words, it
needs to be initialized.
Okay. Thanks.
I'd like to point out, in case you were unaware of the fact, that
initialization can happen by default. If an object has static storage
duration, either because it is defined at file scope, or because it's
declared inside a function body with the 'static' keyword, then it is
guaranteed to be zero-initialized, even if you don't provide any
explicit initializer.
Also if you define an aggregate object (an array or a struct), and you
don't provide enough initializers to fill the whole thing, then the
remaining parts of the object are zero initialized. Therefore, you could
define:
long factors[MAX_FACTORS] = {0};
The 0 would initialize the first factor, and the remaining factors would
all be zero-initialized by default.
[...]Barry Schwarz said:On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper
Of course you meant "only", not "even".
Barry said:On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper
Of course you meant "only", not "even".
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.