Why didn't clang issue a warning using an uninitialized array?
I was reading some code from a book and forgot to initialize the marble of the array. But I am not getting any warnings from clang. here is the code:
/* sum_arr2.c -- ๅฏนไธไธชๆฐ็ป็ๆๆๅ
็ด ๆฑๅ */
#include<stdio.h>
#define SIZE 10
int sump(int *start,int * end);
int main(void)
{
int marbles[SIZE];
long answer;
answer = sump(marbles,marbles + SIZE);
printf("The total number of marbles is %ld.\n",answer);
return 0;
}
/* ไฝฟ็จๆ้็ฎๆฏ */
int sump(int * start,int * end)
{
int total = 0;
while(start < end)
{
total +=*start; /* ๆๅผ็ดฏๅ ๅฐtotalไธ*/
start++; /* ๆๆ้ๅๅๆจ่ฟๅฐไธไธไธชๅ
็ด */
}
return total;
}
I have compiled the code with:
gcc -Wall sum_arr2.c
and got no warning. so i tried
clang -Wall sum_arr2.c
there is still no warning. when i executed the program
./a.out
The output is some random value.
so I want to ask if this is the correct behavior of the compiler, or a bug?
It seems gcc is just a name, not a gcc compiler:
gcc -v
Configured with: -- prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
clang -v
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
can anyone check it out on gcc?
source to share
Most compilers will only consider one function at a time when they generate warnings, and if you don't look inside the implementation sump
, you cannot know that the call inside is main
wrong. What if I sump
wrote to an array instead of reading from it? In this case, passing an uninitialized array won't be a problem.
int sump(int * start,int * end)
{
while(start < end)
{
*start = 42;
start++;
}
return 17;
}
source to share