Here is the code I'm using:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int sz = 100000;
arr = (int *)malloc(sz * sizeof(int));
int i;
for (i = 0; i < sz; ++i) {
if (arr[i] != 0) {
printf("OK\n");
break;
}
}
free(arr);
return 0;
}
The program doesn't print OK
. malloc
isn't supposed to initialize the allocated memory to zero. Why is this happening?
int a; int b = a; int c = a; if (b==c) printf("yes");else printf("no");
is guaranteed to print "yes" by the standards? – Reinersa
is uninitialized. Its value indeterminate. The behavior is UB. If you did something likeint *a = source_of_entropy(); int b = *a; ...
then it would not be UB, ifsource_of_entropy
was well behaved with regard to the contents of the memory it returns. – Joub