Martin
Martin

Reputation: 123

Why do I have values in my array that I didn't assign?

#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;
}

Why do I have values in the unassigned elements of 1, 4, and 6-8? I didn't assign any values. How can I make it assign automatically 0 when it's empty?

Upvotes: 0

Views: 324

Answers (4)

Gilbert
Gilbert

Reputation: 3776

While Oli Charlesworth's post shows the solution, let me introduce a little more "why". Such undefined values are very obvious for "automatic" variables allocated on the applications stack.

While operating systems tend to zero memory before loading programs into it main() is not where C programs actually start. The actual starting location is buried in some implementation dependent place making your code in main() just the last call in a series of function calls during program start up. All of these earlier internal calls scribble in the stack leaving their mess behind them.

As each function call inherits the mess from prior calls it's really unknown what will be seen on the stack of a fresh call. If the stack grows large enough you will eventually find the operating system's zeros in unused memory, which may help debugging in lager programs, but code must never depend on such zeros.

Both stack and heap (e.g., malloc()) memory are subject to unknown values.

Upvotes: 3

effeffe
effeffe

Reputation: 2881

From C99 you can also initialize only the subobjects you need and the rest will be initialized as data with static storage duration (0 for int):

int values[10] =
{
    [0] = 197,
    [2] = -100,
    [5] = 350,
    [3] = values[0] + values[5],
    [9] = values[5] / 10
};

More complicated initializations are impossible/unclear to do this way. Things like:

--values[2];

Cannot be done in the initialization because if there are more initializers for the same element, only the last will be used and the first ones could not even be evaluated, causing a decrement on an uninitialized value. From C11 standard:

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; 151)

151) Any initializer for the subobject which is overridden and so not used to initialize that subobject might not be evaluated at all.

(I think it deserves more than a foot-note...)

However, I think that's just an example since there is no point to do so, you can initialize it to 350 - 1 in this case.

Upvotes: 3

fge
fge

Reputation: 121740

In C, automatic variables are never zeroed. What you want is to memset() your array to 0 to avoid that.

Upvotes: 4

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272537

You didn't assign them, so you just get whatever rubbish was already in memory at those locations.

To zero-initialize, you can do this:

int values[10] = {0};

Alternatively, use memset.

Upvotes: 6

Related Questions