Thread: Uninitialized numeric arrays in C++

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Uninitialized numeric arrays in C++

    I am using a numeric array as an example to my core question.

    What happens when an array declaration statement does not provide an initial value for each of the elements in a numeric array?

    I know that the compiler does not automatically initialise the elements (C++ standards and running on my compiler). What i dont know is how do I articulate what is in the uninitialised elements? is it:
    1) All array elements contain a numeric value; or
    2) All elements may contain garbage

    Point 1 is my de-facto understanding all along on what happens under the hood. Ive tested it time and time again with my compiler that the values in the uninitialized indices are numbers of some sort.

    Yet, I have seen on forums where people say that they "may contain garbage" and not numbers. My question is: surely point 1 and 2 are the same thing? If not, what is the difference? What does "garbage" mean?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    "Garbage" just means that the values are meaningless in whatever is the given context. The idea is that the memory allocated could have preexisting values that are then reinterpreted for your variables, and hence may make no sense at all in your context.

    So yes, for a numeric array both #1 and #2 are true, assuming that the array is not zero initialised due to say, having static storage duration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Thanks Laserlight that kinda makes sense. But which one of them is more specific as a description for the numeric arrays as an example? What I mean by that is, is there ever a case where the unitialised values are not be number? For me number 1 seems "more correct"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if your numbers were floating point numbers, then you would have the possibility that they could be NaN's. These could in turn be signalling NaN's, which cause an exception if you try to do anything with them. Your program crashes, or the machine reboots - take your pick.

    Whilst the average experience is that all bit patterns of integers are valid, the standard does permit integers to have trap representations (like NaNs).

    You're similarly screwed if you try and dereference garbage pointers.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Depends on the compiler. For Microsoft compilers debug builds, all "uninitialized" variables are set to hex 0xcc pattern, such as 0xcccccccc for 32 bit integers. For release builds, typically whatever happens to be in memory at program load time is what ends up in uninitialized variables.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    C++ explicitly disallows expressions that evaluate to indeterminate values, except for they types unsigned char and std::byte. You could say this is to allow other types to have trap representations, but actually the wording disallows such expressions even when they type is designated not to trap by numeric_limits<T>::traps.

    In the case of unsigned char and std::byte, there is a list of cases where using an indeterminate value is allowed. However, returning such values from a function, or applying arithmetic operations to them are not among them. So you can't do operations like "x -= x" on uninitialized x. What you can to is assign them. This allows you to copy uninitialized data, but only if you do it though an unsigned char or std::byte. Here are some allowed expressions:
    Code:
    unsigned char x;
    unsigned char y = x;
    x;
    true? y : x;
    x, 10;
    static_cast<std::byte>(x);
    (void) x;
    y = x;

    Therefore the second description is more correct; it is disallowed to use/evaluate uninitialized variables in c++.
    Last edited by King Mir; 04-29-2018 at 01:54 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. alpha-numeric to numeric program
    By johnhuge in forum C Programming
    Replies: 2
    Last Post: 03-24-2012, 12:08 AM
  2. Passing uninitialized 2D arrays as arguments in C
    By JennieEnglish in forum C Programming
    Replies: 10
    Last Post: 03-11-2012, 10:00 PM
  3. Uninitialized value of char *
    By joelem in forum C Programming
    Replies: 6
    Last Post: 07-17-2009, 10:04 AM
  4. How To Chk Uninitialized Pointer
    By u_peerless in forum C Programming
    Replies: 11
    Last Post: 06-19-2008, 10:11 AM
  5. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM

Tags for this Thread