0

I'm a bit baffled with whether I'm printing my dynamic array correctly. I'm parsing an input file. Say the input file is:

x 6 9 15,

then my goal is to store 1, 2 and 3 into an array called x. It's doing exactly this. I've set the size of the array using:

arr = malloc(sizeof(int)*noValues);

(I also have int *arr declared as a global variable in my file)

where noValues is equal to the number of values encountered in the input file (in this case 3).

I then print the array using:

for (i = 0; i < noValues; i++) {
    printf("arr[%d]: %d\n", j, arr[j]);
} 

and get the following output:

arr[0]: 6
arr[1]: 9
arr[2]: 15

However, when I change the "noValues" in the for loop to 10, I get the following:

and get the output:

arr[0]: 6
arr[1]: 9
arr[2]: 15
arr[3]: 0
arr[4]: 0
arr[5]: 0
arr[6]: 49
arr[7]: 0
arr[8]: 17060496
arr[9]: 0

Why am I getting some non 0 values? Shouldn't they all be 0? Any clear-up would be appreciated. Is this normal C behaviour?

3 Answers3

2

You are accessing "elements" out of the boundary of arr, this is undefined-behavior; that's why you get values that are seemingly random (but the application could just as well crash).

Community
  • 1
  • 1
Xiaotian Pei
  • 3,210
  • 21
  • 40
  • What's the best way to print an array, ensuring I don't go out of boundary? –  Oct 12 '15 at 00:02
  • The way you are printing the values is okay. Just don't exceed the size of the array. – fvgs Oct 12 '15 at 00:04
  • If the values are random, why are alot of them 0? –  Oct 12 '15 at 00:06
  • There isn't a way to "check the size" of a dynamic array. You had to specify a size when you allocated it; it's up to you to keep track of that size for as long as you need it. And the values aren't actually *random* — the computer isn't rolling dice to produce them. They're just whatever bytes happen to be in memory after the array. – Wyzard Oct 12 '15 at 00:07
  • 1
    I suggest that "random" is the wrong word. The values are *arbitrary* (or *garbage*) not truly random. – Keith Thompson Oct 12 '15 at 00:09
  • @user2832891 It just happens that you have a lot of them 0, but you generally cannot make any assumptions about what you'll find past known boundaries. – coredump Oct 12 '15 at 00:11
  • The thing I'm worried about is that my code is allocating more size than it needs because of so many 0 values. From my experience, the numbers are normally larger. Is there any way I can print the array using a while loop to make sure it's storing exactly 3 values and the rest are random? –  Oct 12 '15 at 00:12
  • @user2832891 don't try to find regularity in random garbage values! It's an undefined behaviour – Rahul Jha Oct 12 '15 at 00:15
  • Can I be certain that the "0" values outputted in the array are also garbage? –  Oct 12 '15 at 00:16
  • No, you cannot rely on any particular values outside the array boundaries. You need to keep track of the size of your array, as others suggested, and use that size when printing out the values. – void_ptr Oct 12 '15 at 01:38
1

You're accessing past the end of the array, which is undefined behavior. Technically anything can happen when you do that, but there are two things that typically happen in practice: either the program will crash, or you'll just get the value of whatever bytes are located in memory past the end of the array. (But this isn't something that you can, or should, rely on.)

Wyzard
  • 33,849
  • 3
  • 67
  • 87
1

The uninitialized array behavior is not fix. The value of uninitialized array is zero ,garbage possible. if you want to get value zero of all the elements in the array then use bzero or memset function.

Jay Dabhi
  • 58
  • 6