Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Uninitialised variables

2 views
Skip to first unread message

Albert

unread,
Aug 5, 2009, 2:35:30 AM8/5/09
to
If I declare a variable and then write

if ([variable name])

is the result *guarunteed* to be false?

user923005

unread,
Aug 5, 2009, 2:51:13 AM8/5/09
to

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.

Nick Keighley

unread,
Aug 5, 2009, 3:05:18 AM8/5/09
to

no. Why would you think so?

David Mathog

unread,
Aug 5, 2009, 11:41:28 AM8/5/09
to

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

Albert

unread,
Aug 7, 2009, 7:11:45 PM8/7/09
to
user923005 wrote:
> 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);

?

Richard Bos

unread,
Aug 7, 2009, 7:19:53 PM8/7/09
to
Albert <albert.xt...@gmail.com> wrote:

> user923005 wrote:
> > 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

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

James Kuyper

unread,
Aug 7, 2009, 7:35:25 PM8/7/09
to

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.

Albert

unread,
Aug 8, 2009, 3:03:44 AM8/8/09
to
James Kuyper wrote:
> 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

unread,
Aug 8, 2009, 7:02:02 AM8/8/09
to

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.

Keith Thompson

unread,
Aug 8, 2009, 1:44:19 PM8/8/09
to
James Kuyper <james...@verizon.net> writes:
[...]

> 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 */

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Barry Schwarz

unread,
Aug 8, 2009, 2:17:05 PM8/8/09
to
On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper
<james...@verizon.net> wrote:

>Albert wrote:
>> James Kuyper wrote:
>>> 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.

--
Remove del for email

Keith Thompson

unread,
Aug 8, 2009, 3:29:06 PM8/8/09
to
Barry Schwarz <schw...@dqel.com> writes:
> On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper
> <james...@verizon.net> wrote:
[...]

>>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".
[...]

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

James Kuyper

unread,
Aug 9, 2009, 8:04:54 AM8/9/09
to
Barry Schwarz wrote:
> On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper
> <james...@verizon.net> wrote:
...

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

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

0 new messages