Thread: Uninitialised char[N]

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    21

    Uninitialised char[N]

    Hi,

    After coming across some dubiously safe code I've been scanning the C++ standard but cannot find any definitive reference to what the value of an uninitialised char[N] will be. Is it undefined?

    I can see that with gcc 4.6 at least [0] is initialised as unsigned(0) - which leaves methods like strcat safe to use immediately... but if it is undefined then the code works only by fluke of the compiler implementation detail.

    Cheers,

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    That depends on where the array is defined. If defined at file scope, its elements will be zero-initialized. If defined locally inside a function, its elements will have random values. But there is always an easy way to make sure an array is properly initialized:

    Code:
    // local array
    void func()
    {
        char localCharArray[ 100 ] = {};
    }
    
    // at file scope
    char arrayAtFileScope[ 100 ] = {};
    
    // now a char array that is a class member
    class MyClass
    {
    public:
        MyClass() : m_memberArray()
        {
        }
    
    private:
        char m_memberArray[ 100 ];
    };
    P.S.: Relevant excerpt from the C++ standard:

    3.6.2 Initialization of non-local objects
    1) Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization
    takes place. Zero-initialization and initialization with a constant expression are collectively called static
    initialization; all other initialization is dynamic initialization.
    Last edited by antred; 06-06-2012 at 07:41 PM.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    21
    If defined at file scope, its elements will be zero-initialized. If defined locally inside a function, its elements will have random values
    Can you tell me where to find this within the ISO standard?

    The definition is within a class member function in this particular case. I do understand how to correctly initialise it, and I always do, this isn't my code. I was just wanting to make certain that my understanding that it will default (by ISO standards) to undefined is correct before I object to this code.

    [edit:]

    I found the definition for static storage definitions, if that is what you were meaning by file scope:
    The storage for objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.
    Last edited by scarecrow; 06-06-2012 at 07:50 PM.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    21
    [edit]: I solved this - my test was broken and you were correct in your assertion about free store and static initialisation values. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-25-2011, 08:30 AM
  2. Use of uninitialised value of size 4
    By dot_pro in forum C Programming
    Replies: 3
    Last Post: 10-26-2010, 04:32 AM
  3. Replies: 3
    Last Post: 05-23-2010, 07:32 AM
  4. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM