Thread: An uninitialized array value = 4198445?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    Question An uninitialized array value = 4198445?

    I'm attempting to learn C at my school, and I'm going through Stephen Kochan's Programming in C book. I just recently tried to compile 7.1, but I got some definately odd results.

    Here's the code itself:


    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
     int values[10];
     int index;
    
     values[0] = 197;
     values[2] = -100;
     values[5] = 350;
     values[3] = values[0] + values[5];
     values[9] = values [5] / 10 ;
     --values[2];
    
     for ( index = 0; index < 10; ++index )
     printf ("values[%i] = %i\n", index, values[index]);
    
     return 0;
     }
    And my output:

    Code:
    values[0] = 197
    values[1] = 4198445
    values[2] = -101
    values[3] = 547
    values[4] = 37814120
    values[5] = 350
    values[6] = 37814124
    values[7] = 895
    values[8] = 37814176
    values[9] = 35
    Shouldn't the uninitialized values (1, 4 and 6-8) be zero? Could somebody explain this to me please?

  2. #2
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    I believe if an array element is not explicitely initialized it can be anything, typically what was already in that memory address.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Shouldn't the uninitialized values (1, 4 and 6-8) be zero?
    no! they should be 'undefinded'--which it looks like they are.....

    in other words, if you don't initialize them, they equal whatever is stored in that memory location. in other other words, garbage.

    initialize your array, then do your work. you'll find that better in the long run.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Shouldn't the uninitialized values (1, 4 and 6-8) be zero?
    If you want them to be zero, you must initialize them yourself.
    Code:
     int values[10] = {0};

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Oh, okay. The book doesn't make a big point of explaining what was going on there. Thanks for letting me know what was going on.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Always initialize all your variables, so you don't end up with some obscure bug that is hard to find.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Global and static variables (and memory allocated with calloc()) are automatically set to zero; auto variables (the normal type declared in blocks) are undefined.
    Always initialize all your variables, so you don't end up with some obscure bug that is hard to find.
    Agreed, but you can easily find uninitialized variables by enabling the appropriate compiler warning. Try -W -Wall for gcc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Note that whilst
    double myarray[5] = { 0 };
    Will always guarantee that your array is filled with 5 doubles equal to 0.0

    This does not
    double *myarray = calloc ( 5, sizeof *myarray );

    See
    http://c-faq.com/malloc/calloc.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    but do realize that

    double a[5];

    and

    double *a = malloc(5 * sizeof *a);

    is 2 completely different things. The first gets destroyed automatically when it goes out of scope, the second has to be freed somewhere.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the point. He didn't say malloc. He said calloc. Pay attention. Read link. Be enlightened. Learn something.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The question was about initialisation, not object lifetime.
    Besides, if the array was global (that was never mentioned either), then it lasts at least as long as the allocated memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Um.... 'Kay. A lot of this is going to be helpful in the future, I'm assuming? A lot of this is out of my knowledge thus far....

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One piece of information is very useful:
    you can easily find uninitialized variables by enabling the appropriate compiler warning. Try -W -Wall for gcc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM