Uninitialised variables 
Author Message
 Uninitialised variables

I read in the FAQ:

Quote:
>Variables with automatic duration (i.e. local variables without the
>static storage class) start out containing garbage, unless they are
>explicitly initialized. (Nothing useful can be predicted about the
>garbage.)

First question: is access to uninitialised variables 'safe'?

For example:

int main() {
    int i;
    if (i > 0) {
        return(1);
    } else {
        return(0);
    }

Quote:
}

Is this guaranteed to either return 1 or return 0? Or is it undefined?

Assuming the above is safe, second question:
How little is 'nothing useful'?

For example:

#include <limits.h>

int main() {
    int i;
    if (i <= INT_MAX) {
        return(0);
    } else {
        return(1);
    }

Quote:
}

Can this return 1?

-M-
--



Mon, 03 Mar 2003 03:00:00 GMT  
 Uninitialised variables

Quote:

> I read in the FAQ:

> >Variables with automatic duration (i.e. local variables without the
> >static storage class) start out containing garbage, unless they are
> >explicitly initialized. (Nothing useful can be predicted about the
> >garbage.)

> First question: is access to uninitialised variables 'safe'?

No.

Quote:
> For example:

> int main() {
>     int i;
>     if (i > 0) {
>    return(1);
>     } else {
>    return(0);
>     }
> }

> Is this guaranteed to either return 1 or return 0? Or is it undefined?

It is undefined.  Uninitialized variables may contain "trap
representations" that cause the program to stop or to fail.
There are certain exceptions for character types, but you
shouldn't use these exceptions.
--



Tue, 04 Mar 2003 03:00:00 GMT  
 Uninitialised variables

Quote:
> I read in the FAQ:

> >Variables with automatic duration (i.e. local variables without the
> >static storage class) start out containing garbage, unless they are
> >explicitly initialized. (Nothing useful can be predicted about the
> >garbage.)

> First question: is access to uninitialised variables 'safe'?

> For example:

> int main() {
>     int i;
>     if (i > 0) {
> return(1);
>     } else {
> return(0);
>     }
> }

> Is this guaranteed to either return 1 or return 0? Or is it undefined?

Aside from pointers (which seem to provoke undefined behaviour even when
accessing a non-NULL pointer that isn't initialized), it was my
understanding that - eventhough the contents of variable 'i' are garbage -
the variable 'i' is still of type int, and thus its contents will have 'int'
properties.

In general, I would thing the program returns either 0 or 1, unless the O.S.
or compiler has some way of knowing that the referred variable is not
initialized and act on that (this could be a de{*filter*} feature).

- Show quoted text -

Quote:
> Assuming the above is safe, second question:
> How little is 'nothing useful'?

> For example:

> #include <limits.h>

> int main() {
>     int i;
>     if (i <= INT_MAX) {
> return(0);
>     } else {
> return(1);
>     }
> }

> Can this return 1?

No

All this being very interesting... why would you want to use uninitialized
variables?

Pieter
--



Tue, 04 Mar 2003 03:00:00 GMT  
 Uninitialised variables
Quote:

>I read in the FAQ:

>>Variables with automatic duration (i.e. local variables without the
>>static storage class) start out containing garbage, unless they are
>>explicitly initialized. (Nothing useful can be predicted about the
>>garbage.)

>First question: is access to uninitialised variables 'safe'?

[-]
Yes/No/Maybe ...

int self_destruct;

if( 0 < self_destruct ) {
    kaboom();

Quote:
}
>For example:

>int main() {
>    int i;
>    if (i > 0) {
>    return(1);
>    } else {
>    return(0);
>    }
>}

>Is this guaranteed to either return 1 or return 0? Or is it undefined?

[-]
It does return 1 or 0.

Quote:
>Assuming the above is safe, second question:
>How little is 'nothing useful'?
[-]
>#include <limits.h>

>int main() {
>    int i;
>    if (i <= INT_MAX) {
>    return(0);
>    } else {
>    return(1);
>    }
>}

>Can this return 1?

[-]
Yes.

Still, although slightly OT, keep in mind tools like purify are
going to spit out warnings by the bucket when using unitialized
variables. Using a default value will at least guarantee a consistent
behaviour, say the application may fail always the same way - helps
in finding the problem.

Since compilers usually generate quite fast and pretty code for
statements like ...
int  i = 0;
long l = 0;
... you should do so IMHO.

Cheers,
Juergen

--
\ Real name     : Jrgen Heinzl         \       no flames      /

--



Tue, 04 Mar 2003 03:00:00 GMT  
 Uninitialised variables

Quote:

> int main() {
>     int i;
>     if (i > 0) {
>         return(1);
>     } else {
>         return(0);
>     }
> }
> Is this guaranteed to either return 1 or return 0? Or is it undefined?

The object with identifier "i" has to exist, but it might contain
a "trap representation", and reading a trap representation causes
undefined behavior.  For example, it might cause abnormal program
termination.

Quote:
> Assuming the above is safe, second question:

is moot.
--



Tue, 04 Mar 2003 03:00:00 GMT  
 Uninitialised variables

Quote:

> I read in the FAQ:

> >Variables with automatic duration (i.e. local variables without the
> >static storage class) start out containing garbage, unless they are
> >explicitly initialized. (Nothing useful can be predicted about the
> >garbage.)

> First question: is access to uninitialised variables 'safe'?

> For example:

> int main() {
>     int i;
>     if (i > 0) {
>         return(1);
>     } else {
>         return(0);
>     }
> }

> Is this guaranteed to either return 1 or return 0? Or is it undefined?

The value of i is indeterminate. Its use results in undefined behavior.

Quote:
> Assuming the above is safe, second question:

Since the assumption is invalid, the question is pointless.

--
Mage Grimau
....................................................................
So now you should spend the morning lying to your father quite amazed
about the Strange Unwashed and Happily Slightly Dazed
....................................................................

--



Tue, 04 Mar 2003 03:00:00 GMT  
 Uninitialised variables

Quote:

> I read in the FAQ:

> >Variables with automatic duration (i.e. local variables without the
> >static storage class) start out containing garbage, unless they are
> >explicitly initialized. (Nothing useful can be predicted about the
> >garbage.)

> First question: is access to uninitialised variables 'safe'?

No. Any use of indeterminate values invokes undefined behaviour.

Richard
--



Tue, 04 Mar 2003 03:00:00 GMT  
 Uninitialised variables
Hi Matthew,

Quote:

> First question: is access to uninitialised variables 'safe'?

> Is this guaranteed to either return 1 or return 0? Or is it undefined?

You can do no harm with.
But using uninitialized pointers is not save. You can expect to receive
an Segmentation Fault if you use one.

Quote:
> Assuming the above is safe, second question:
> How little is 'nothing useful'?

> For example:

> #include <limits.h>

> int main() {
>     int i;
>     if (i <= INT_MAX) {
>         return(0);
>     } else {
>         return(1);
>     }
> }

> Can this return 1?

It could, but I would not rely on it.

The value of an uninitialzied Variable is nothing you can count on.

Cheers
Frank
--



Tue, 04 Mar 2003 03:00:00 GMT  
 Uninitialised variables
# I read in the FAQ:

#>Variables with automatic duration (i.e. local variables without the
#>static storage class) start out containing garbage, unless they are
#>explicitly initialized. (Nothing useful can be predicted about the
#>garbage.)

# First question: is access to uninitialised variables 'safe'?

# For example:

# int main() {
#     int i;
#     if (i > 0) {
#       return(1);
#     } else {
#       return(0);
#     }
# }

# Is this guaranteed to either return 1 or return 0? Or is it undefined?

No and yes, respectively. The value of i is indeterminate until it is
initialized. Use of an undeterminate value is explicitly mentioned in
the Standard to have undefined behavior.

Regards,

        Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--



Tue, 04 Mar 2003 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Uninitialised variable use. (gcc warning messages)

2. Suggestion: Option for warning against uninitialised variables

3. compiler no longer detects uninitialised variables

4. uninitialised array elemens

5. uninitialised pointers

6. Aggregate VTable uninitialised

7. Uninitialised memory

8. Uninitialised vars in static lib

9. Help - I need to set variable A = value held in variable named in variable B

10. Global variable or not global variable

11. static local variables vs global variables

12. Interchanging values of variables without using a temporary variable

 

 
Powered by phpBB® Forum Software