c - malloc initializing allocated array to zero -


here 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;     (i = 0; < sz; ++i) {         if (arr[i] != 0) {             printf("ok\n");             break;         }     }      free(arr);     return 0; } 

the program doesn't print ok. malloc isn't supposed initialize allocated memory zero. why happening?

the man page of malloc says:

the malloc() function allocates size bytes , returns pointer allocated memory. the memory not initialized. if size 0, malloc() returns either null, or unique pointer value can later passed free().

so, malloc() returns uninitialized memory, contents of indeterminate.

 if (arr[i] != 0) 

in program, have tried access content of memory block, invoked undefined behavior.


Comments

Popular posts from this blog

html - How to custom Bootstrap grid height? -

javascript - pass values from mssql to views in node -

javascript - Understanding ExtJS Framework Syntax -