Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

Weird behavior when printing array in C?

I am trying to print an array however I am not getting the desired output, weird numbers appear after the loop finishes printing the pre-defined array.

Code is:

#include <stdio.h>    
int main(){    
    int intArray[11] = {1,2,8,12,-13,-15,20,99,32767,10,31};
    int i=0;        
    for(i=0;i<sizeof(intArray);i++){
        printf("%d
",intArray[i]);
    }
}

Output:

1
2
8
12
-13
-15
20
99
32767
10
31
11
1629976468
2674040
2665720
1627423265
1
2665616
-2147417856
1629976534
1629976468
2674040
0
1627423172
1629976532
0
1629110043
0
0
0
0
0
0
0
0
0
0
0
1629976538
0
1629956432
2674276
0
1627407935
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The breaking condition in for loop is wrong! Which causes an index-out-of bound problem as i exceeds the maximum index value that is 10 because array length is just 11. The loop break condition should be < length of array( =11) but not < size of array. Value of sizeof(intArray) is equals to 11 * sizeof(int) (= 44).

To understand it read: sizeof Operator:

6.5.3.4 The sizeof operator, 1125:

When you apply the sizeof operator to an array type, the result is the total number of bytes in the array.

According to this when sizeof is applied to the name of a static array identifier (not allocated through malloc()/calloc()), the result is the size in bytes of the whole array rather then just address. That is equals to size of each elements multiply by the length of array.
In other words: sizeof(intArray) = 11 * sizeof(int) ( as intArray length is 11 ). So suppose if sizeof(int) is 4-bytes then sizeof(intArray) is equals to 44.

Below a code example and its output will help you to understand further(read comments):

int main(){
    int intArray[11] = {1, 2, 8, 12, -13, -15, 20, 99, 32767, 10, 31};
    int i = 0;
 
    printf("sizeof(intArray):  %d
", 
            sizeof(intArray)                       //1. Total size of array
    ); 
    printf("sizeof(intArray[0]):  %d
", 
            sizeof(intArray[0])                    //2. Size of one element
    ); 
    printf("length:  %d
", 
            sizeof(intArray) / sizeof(intArray[0]) //3. Divide Size
    );    
    return 0;
}

Output:

sizeof(intArray):  44    //1. Total size of array:  11 * 4 = 44
sizeof(intArray[0]):  4  //2. Size of one element:  4
length:  11              //3. Divide Size:          44 / 4 = 11 

One can check the working code @ideone, note: I am assuming size of int is 4.

Now notice as sizeof(intArray) is 44 that is more then length of array hence the condition is wrong and you have Undefined behavior in the code at runtime. To correct it replace:

for(i=0; i < sizeof(intArray); i++)
//           ^--replace-----^
//            wrong condition = 44

With:

for(i=0; i < sizeof(intArray) / sizeof(intArray[0]); i++)
          // ^------------------------------------^
          // condition Corrected = 11 


 

To calculate length of array, I simply divided total size of array by the size of one element and code is:

sizeof(intArray) / sizeof(intArray[0])   // 44 / 4 = 11
     ^                 ^
total size of       size of first element
array   
  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...