Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Uninitialised variables

Reply
Thread Tools

Uninitialised variables

 
 
Albert
Guest
Posts: n/a
 
      08-05-2009
If I declare a variable and then write

if ([variable name])

is the result *guarunteed* to be false?
 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      08-05-2009
On Aug 4, 11:35*pm, Albert <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      08-05-2009
On 5 Aug, 07:35, Albert <(E-Mail Removed)> wrote:

> If I declare a variable and then write
>
> if ([variable name])
>
> is the result *guarunteed* to be false?


no. Why would you think so?
 
Reply With Quote
 
David Mathog
Guest
Posts: n/a
 
      08-05-2009
Albert wrote:
> 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
 
Reply With Quote
 
Albert
Guest
Posts: n/a
 
      08-07-2009
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);

?
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      08-07-2009
Albert <(E-Mail Removed)> 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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      08-07-2009
Albert 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
>
> 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.
 
Reply With Quote
 
Albert
Guest
Posts: n/a
 
      08-08-2009
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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      08-08-2009
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.

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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-08-2009
James Kuyper <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
uninitialised variable but NO error geek.arnuld@gmail.com C++ 2 02-04-2007 11:05 AM
uninitialised variable but NO error geek.arnuld@gmail.com C++ 2 02-04-2007 08:19 AM
Calling free() on an uninitialised pointer? santosh C Programming 13 12-05-2005 09:05 PM
value of an uninitialised variable Andy Fish XML 7 01-10-2005 04:37 PM
C++: Uninitialised Variable Passed as a Parm Gene Wirchenko C++ 22 12-19-2003 02:26 PM



Advertisments