M
is uninitialized!
Read M
before creating the array a
or, make a
big enough to contains all possible input sizes and use M
to limit the access to a
. The latter this is quite common in competitive programming: create a static array big enough and then use only a portion of it based on other information, like M
in this case.
Anyway, the error would be easily spotted enabling compiler warnings Wall -Wextra
:
Here is what gcc
, clang
and icc
complains about.
gcc -Wall -Wextra -g -O3 -Wshadow test.c
test.c: In function ‘main’:
test.c:7:1: warning: ‘M’ is used uninitialized in this function [-Wuninitialized]
int N, M, a[M], i, j, fake;
^~~
clang -Wall -Wextra -g -O3 -Wshadow test.c
test.c:7:13: warning: variable 'M' is uninitialized when used here
[-Wuninitialized]
int N, M, a[M], i, j, fake;
^
test.c:7:9: note: initialize the variable 'M' to silence this warning
int N, M, a[M], i, j, fake;
^
= 0
1 warning generated.
icc -Wall -Wextra -g -O3 -Wshadow test.c
test.c(7): warning #592: variable "M" is used before its value is set
int N, M, a[M], i, j, fake;
solved I cant make my code to run and i dont know what is wrong [closed]