Thread: Initilizing global array to all zeroes

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    3

    Initilizing global array to all zeroes

    Hi, is it possible to inialize an array of integers(or chars or whatever) declared globally to all zeroes at compile time?

    Ie. Can I write something like this int a[100] = {0} and be sure that all elements of 'a' will initially be zero when I execute my program?

    Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Interesting, why is this? I thought that this wasn't the case for local var's because C++ didn't want to force you to do that (inefficient), but why would they do so for globals?

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by roktsyntst
    Interesting, why is this? I thought that this wasn't the case for local var's because C++ didn't want to force you to do that (inefficient), but why would they do so for globals?
    I guess because globals have their contents set at compile time so there wouldnt be any efficiency hit...the memory from the executable would just be mapped as is from the file

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    >>I guess because globals have their contents set at compile time
    Or runtime.

    3.6.2 Initialization of non-local objects [basic.start.init]
    The storage for objects with static storage duration shall be zero-initialized before any other initialization takes place...Objects of POD types (...) shall be initialized before any dynamic initialization takes place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob : Creating a global array?
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2008, 10:38 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. Increase size of global multidimensional array
    By 3saul in forum C Programming
    Replies: 4
    Last Post: 04-22-2006, 09:00 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM