Uninitialised variables

Discussion in 'C Programming' started by Albert, Aug 5, 2009.

  1. Albert

    Albert Guest

    If I declare a variable and then write

    if ([variable name])

    is the result *guarunteed* to be false?
     
    Albert, Aug 5, 2009
    #1
    1. Advertising

  2. Albert

    user923005 Guest

    On Aug 4, 11:35 pm, Albert <> 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.
     
    user923005, Aug 5, 2009
    #2
    1. Advertising

  3. On 5 Aug, 07:35, Albert <> 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?
     
    Nick Keighley, Aug 5, 2009
    #3
  4. Albert

    David Mathog Guest

    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
     
    David Mathog, Aug 5, 2009
    #4
  5. Albert

    Albert Guest

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

    ?
     
    Albert, Aug 8, 2009
    #5
  6. Albert

    Richard Bos Guest

    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


    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
     
    Richard Bos, Aug 8, 2009
    #6
  7. Albert

    James Kuyper Guest

    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.
     
    James Kuyper, Aug 8, 2009
    #7
  8. Albert

    Albert Guest

    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.
     
    Albert, Aug 8, 2009
    #8
  9. Albert

    James Kuyper Guest

    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.
     
    James Kuyper, Aug 8, 2009
    #9
  10. James Kuyper <> 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 8, 2009
    #10
  11. On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper
    <> 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
     
    Barry Schwarz, Aug 8, 2009
    #11
  12. Barry Schwarz <> writes:
    > On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper
    > <> 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).

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Aug 8, 2009
    #12
  13. Albert

    James Kuyper Guest

    Barry Schwarz wrote:
    > On Sat, 08 Aug 2009 11:02:02 GMT, James Kuyper
    > <> 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."
     
    James Kuyper, Aug 9, 2009
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    7
    Views:
    565
    David Carlisle
    Jan 10, 2005
  2. Gene Wirchenko
    Replies:
    22
    Views:
    763
    Jeffrey Schwab
    Dec 19, 2003
  3. santosh

    Calling free() on an uninitialised pointer?

    santosh, Dec 4, 2005, in forum: C Programming
    Replies:
    13
    Views:
    447
    Keith Thompson
    Dec 5, 2005
  4. Replies:
    2
    Views:
    288
    Rolf Magnus
    Feb 4, 2007
  5. Replies:
    2
    Views:
    299
    John Ratliff
    Feb 4, 2007
Loading...

Share This Page