I was initializing array (unsigned short int) of size 100000000 in C 4.3.2 , and other two int array of size 1000000. But while submiting on Online judge, it was giving SIGSEGV error .
Therefor i decided to initialize my array dynamically with a default value 0, as adding value by loop takes much time.
My question is how to initialise array dynamically with a default value ?
Best Answer
You can use
void *calloc(size_t nmemb, size_t size);
function to initialize memory with0
,or you can also use
memset()
explicitly to initialize memory allocated bymalloc()
call.Note:
calloc()
isn't magic either - it will also use a loop somewhere to replace the garbage with all zeroes.See also: Why
malloc()
+memset()
is slower thancalloc()
?