2

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?

Community
  • 1
  • 1
xph
  • 937
  • 3
  • 8
  • 16
  • 1
    Well, you didn't initialize, so you get uninitialized values. Mostly zeros, but never guaranteed. – stefan Apr 11 '15 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). – stefan Apr 11 '15 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 ;-) – xph Apr 11 '15 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! – stefan Apr 11 '15 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! – xph Apr 11 '15 at 20:01
  • Ah, I see. Well this makes learning a bit harder, in my opinion ;-) – stefan Apr 11 '15 at 20:03
  • Yes, agreed. It's not going to teach some "proper" usage of C - but that's not really the point, to me. This board is designed to build and learn about *robotics* and i'm mainly focused on sensors, servos and so on. Of course, code is needed to get things done. At least a reason to "get in touch" with C, a language i've always avoided until now. But the IDE is... not really good. If i'm looking for new adventures, i'll try to get rid of it. – xph Apr 11 '15 at 20:09

7 Answers7

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

Ishay Peled
  • 2,783
  • 1
  • 23
  • 37
2

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};
c-smile
  • 26,734
  • 7
  • 59
  • 86
  • Thank you! Now i know the mistake i made - only if *some* values are defined, the rest would be zero. Will do that! – xph Apr 11 '15 at 19:45
1

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

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • 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 Apr 11 '15 at 19:33
  • @xph Yes, that's exactly where you're wrong. Uninitialized means garbage values. – stefan Apr 11 '15 at 19:34
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.

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
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.

user3629249
  • 16,402
  • 1
  • 16
  • 17
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.

Rabbiya Shahid
  • 422
  • 3
  • 14
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.

jjjjjj
  • 1,152
  • 1
  • 14
  • 30