Thread: initializing array - quick question

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    initializing array - quick question

    Hey, will this guarantee all the elements in my array will be declared zero? or should I got for a for loop instead?

    Code:
      int array[size] = {0};

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fcommisso View Post
    Hey, will this guarantee all the elements in my array will be declared zero?
    Yes.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Don't forget to compile as C99 if you're going to use variable-length arrays. Alternatively, you could use calloc to allocate the memory for array and initialize it to 0s.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Really? That would seem to only set one if you use size as an index. The most common way I have seen to init an array (to zero for instance) is this:
    Code:
    int myArray[size];
    memset(myArray, 0, sizeof(int)*size);
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jeffcobb View Post
    Really? That would seem to only set one if you use size as an index.
    Wrong.
    Code:
    int ray[ANY VALUE] = {0};
    Initializes the entire array to zero, and it is the most common technique. You do not have to use memset.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by MK27 View Post
    Wrong.
    Code:
    int ray[ANY VALUE] = {0};
    Initializes the entire array to zero, and it is the most common technique. You do not have to use memset.
    Interesting; changing your initializer to 1:

    Code:
        int myArray[10] = {1};
        for(int x = 0; x < 10; x++)
            printf("myArray[%d] = %d\n", x, myArray[x] );
    Produces:
    Code:
    myArray[0] = 1
    myArray[1] = 0
    myArray[2] = 0
    myArray[3] = 0
    myArray[4] = 0
    myArray[5] = 0
    myArray[6] = 0
    myArray[7] = 0
    myArray[8] = 0
    myArray[9] = 0
    On GCC in debug mode (which explains the other 0's). I would expect it to be garbage in release mode. The {} are only specifying one element. Is it possible that you are also running in debug mode (hence all vars are initialized, at least to zero) and being fooled by the output?

    J.
    Last edited by jeffcobb; 12-14-2009 at 02:16 PM. Reason: Forgot closing paren
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jeffcobb View Post
    Is it possible that you are also running in debug mode (hence all vars are initialized, at least to zero) and being fooled by the output?
    Nope.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Note: more portable than using memset as earlier mentioned.

    Code:
       static int myArray[10];
    If you specify anything other than 0, when you initialize then the first index is only initialized all others will be set to 0.

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    After playing with this a bit more it seems that {0} does work, with the caveats being you can only use 0 and only at array instantiation. Any other value at any other time will require stronger measures such if you need to *re*set an array or defer initialization or use a default value other than zero.

    But if you need to init the array once only and to zero only, this would seem to be an acceptable solution ^__^
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I was just flipping thru the new standard to find out if this is guaranteed. It is:

    Quote Originally Posted by 6.7.8 1682
    If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    Which means:
    1653 If an object that has static storage duration is not initialized explicitly, then:

    1654 — if it has pointer type, it is initialized to a null pointer;

    1655 — if it has arithmetic type, it is initialized to (positive or unsigned) zero;

    1656 — if it is an aggregate, every member is initialized (recursively) according to these rules;

    1657 — if it is a union, the first named member is initialized (recursively) according to these rules.
    That would explain why you can initialize the first few (or one) item to anything, and then everything else is zero, as jeffcobb points out.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    This would also be useful in resource-constrained embedded environments too....will keep it in mind.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  2. Yet another array question...
    By Raigne in forum C++ Programming
    Replies: 11
    Last Post: 11-13-2008, 01:55 PM
  3. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM
  4. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 03:04 AM
  5. array question
    By KAchE in forum C Programming
    Replies: 4
    Last Post: 02-18-2002, 06:33 PM