4

I'm new to programming in C and i found something i don't understand:

When initializing an array without given values, i thought all elements would be zero. I wrote this few lines of code...

int main()                    
{     
    int obj[10][4];

    for (int a = 0; a < 10; a++)
    {
        print("%d\t%d\t%d\t%d\n", obj[a][0], obj[a][1], obj[a][2], obj[a][3]);
    }            
}

...and was pretty confused by its output:

0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       7661
7960    2697    2260    7960
1551630361      -2130960380     146780176       -2130960380

I don't understand why some of the values are zero and some are not. I was even more confused that this numbers change when i add more code. For example, i changed the previous example and added another print()...

int main()                    
{     
    int obj[10][4];

    print("start\tstop\tcenter\tdist\n\n");

    for (int a = 0; a < 10; a++)
    {
        print("%d\t%d\t%d\t%d\n", obj[a][0], obj[a][1], obj[a][2], obj[a][3]);
    }            
}

...getting this:

start   stop    center  dist

0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       7673
7972    2709    2272    7972
1551630361      -2130960380     146780176       -2130960380

Using a bigger array, this numbers are not only found at its end. The first few values are always zero, but then "something" happens.

I've found a solution here at SO, using memset(), that works for me, but... what's going on here?

Could someone explain this, using words a C-newbie would understand?

7
  • 1
    Well, you didn't initialize, so you get uninitialized values. Mostly zeros, but never guaranteed. Commented Apr 11, 2015 at 19:32
  • A tip for further development: If you get unexpected behavior with truly weird values, try a valgrind run. This may give hints to you where the bug is (uninitialized variables are fairly well detected by valgrind). Commented Apr 11, 2015 at 19:37
  • @stefan: Thanks for this information - but it's a little too much right now. I looked up valgrind and bookmarked some sites i might understand after using C a little longer ;-) Commented Apr 11, 2015 at 19:48
  • 1
    Yes, I know that this is very much in the beginning, trust me, I know beginners ;-) But it's always helpful to know what to look for. Two steps: Add -g to your compiler invocation (You probably type gcc -Wall -std=c99 my_file.c -o my_binary right now, so this becomes gcc -g -Wall -std=c99 my_file.c -o my_binary). Second step: instead of executing like this: ./my_binary, you do valgrind ./my_binary. And that's it. If there is "... uninitialized value...", there's also the file and line listed there and you can look into the problem. Just try it with the code of your question! Commented Apr 11, 2015 at 19:52
  • @stefan: Well, the thing is... i'm not coding the "usual" way. I write in C for a small Propeller board, using the IDE built for exactly that little computer. I don't even use a normal compiler, it's an all-built-in-solution to get some code on the board in the most convenient way, aka "Click this button when done". If i'm going to stick with it, i'd have a deeper look at it. Maybe i can use the tools of my choice to program this computer, and then this'll be very handy! Commented Apr 11, 2015 at 20:01

7 Answers 7

4

You have a declaration of variable but no initialization.

If you want your array to be zero initialized simply put this:

int obj[10][4] = {0};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Now i know the mistake i made - only if some values are defined, the rest would be zero. Will do that!
4

Simply put, you can not assume the array is initialized to 0 unless you explicitly made the initialization yourself. The C standard does not guarantee anything about the content of the array, just that you'll have memory allocated for it.
A great reference on initializing and arrays in C general can be found here:http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

1 Comment

...so i did get that wrong. Thanks for the explanation and the link!!
1

Initial values of anything are undefined. Your code just defines an array - it never assigns any value to what's in it.

2 Comments

Yes - but the examples i've come across suggest that "no value" means "zero, then". Did i get that wrong and "no value" just means "whatever"..?
@xph Yes, that's exactly where you're wrong. Uninitialized means garbage values.
1

storage class type of int obj[10][4] is auto and it is stored on stack. auto variables don't get initialized values, i.e. they have garbage values.If you want array to initialize with zeroes then make it global i.e. declare array outside the main() function.

Comments

0

the stack contains trash until the program places specific values into variables defined on the stack. The startup code does not initialize the stack contents.

Note: variables in the global space/file global space are initialized by the startup code. where is no specific initializer is specified, then the memory is set to 0x00.

Comments

0

Initially, the array will have some garbage values or any unpredictable values. There is no telling what these values will be or where will they be located in the array.

Whenever you declare an array in C, initialize it before using. i.e., run a loop to initialize all the array locations to zero or any number that you want to store. That way you will know for certain what the array contains and in further processing, your array will not show undefined behaviour.

Comments

0

Not an expert but a hypothesis from experience:

If you initialize an array as a static int myArray[1000], then C allocates new memory filled with 1000 zeros for the duration of the program. Initializing with int myArray[1000] just sets aside 1000 registers, some of which may contain old values from a previous use of the register.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.