Uninitialised variables

A

Albert

If I declare a variable and then write

if ([variable name])

is the result *guarunteed* to be false?
 
U

user923005

If I declare a variable and then write

if ([variable name])

is the result *guarunteed* to be false?

Assumption-> [variable name] is an auto or allocated via malloc():

It's guaranteed to be undefined behavior, unless [variable name] is an
unsigned char.
If you want it to contain a value, it's not difficult to poke one into
the variable.

Assumption: [variable name] is of static scope or higher:
It's guaranteed to be initialized to zero if numeric, NULL if a
pointer, etc. {unless you initialize it to something else}.
So (assuming that "if ([variable name]) make sense in context) then
the result will be false.
However, it could be a compound object for which the if() check is
merely a syntax error.

What is it exactly that you are trying to accomplish. You have not
given enough information to give a truly sensible answer.
 
D

David Mathog

Albert said:
If I declare a variable and then write

if ([variable name])

is the result *guarunteed* to be false?

Does these two examples answer your question?

int i;
printf("the value of i is undefined, and happens to be: %d\n",i);

vs.

int i=0;
if(!i)printf("the value of i is always zero\n");

Regards,

David Mathog
 
A

Albert

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);

?
 
R

Richard Bos

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

That is not an answer to Dann's question.

Dann's question is more important than fiddling about with the details,
as you are trying to do.

At the very least, show _real_ code. Not snippets; not pseudo-code; a
real, compilable program that shows _why_ you are asking this question.

Richard
 
J

James Kuyper

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);

Yes, you could write that, and it violates no syntax rules or
constraints, so the compiler isn't required to warn you about that line
of code.

However, If factors[] is unitialized, as implied by your subject line,
than the value stored in factors[index] is indeterminate, and (unless it
has a type prohibited from having trap representations) it could be
contain a trap representation. In that case, the behavior of your
program is undefined. Even if it's not a trap representation, it's not
guaranteed to be zero, and it's also not guaranteed to be non-zero.

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

Albert

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

James Kuyper

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

Keith Thompson

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.

To be precise, this happens if you provide at least one initializer
but not enough to fill the whole thing.

long factors[MAX_FACTORS] = {0}; /* filled with 0s */
long factors[MAX_FACTORS]; /* filled with garbage */
 
B

Barry Schwarz

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.

Of course you meant "only", not "even".
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.
 
K

Keith Thompson

Barry Schwarz said:
On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper


Of course you meant "only", not "even".
[...]

Perhaps, but a static object is guaranteed to be zero-initialized
either if there's no explicit initializer or if it has an explicit
initializer that happens to set it to zero (where "zero" is defined
recursively for arrays, structures, and unions).
 
J

James Kuyper

Barry said:
On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper


Of course you meant "only", not "even".

Actually, my mistake was more complicated than that. What I wanted to
say would have been more complicated than what I did say, and it sounds
a bit clumsier:

"... guaranteed to be initialized, even if you don't provide any
explicit initializer, in which case it will be zero-initialized."
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top