2

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 ?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 7
    100000000 really ? – P0W Jul 27 '13 at 14:15
  • at that size, it probably makes sense to request memory from the OS directly via `mmap()` for UNIX-like systems or `VirtualAlloc()` on Windows; conveniently, these functions already zero the memory for you... – Christoph Jul 27 '13 at 14:58

2 Answers2

9

You can use void *calloc(size_t nmemb, size_t size); function to initialize memory with 0,

The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

 calloc(number of elements, sizeof(type));

or you can also use memset() explicitly to initialize memory allocated by malloc() 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 than calloc()?

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 7
    +1 Probably it would be worth mentioning that `calloc()` isn't magic either - it will **also** use a loop somewhere to replace the garbage with all zeroes. –  Jul 27 '13 at 14:14
  • 1
    @GrijeshChauhan You are getting better and better – Coffee_lover Jul 27 '13 at 14:20
  • 2
    @Coffee_lover I am unemployed these days so I am getting time :P :D Thanks! – Grijesh Chauhan Jul 27 '13 at 14:21
  • 3
    @GrijeshChauhan Now **that** is a reason! :D –  Jul 27 '13 at 14:23
  • 1
    @GrijeshChauhan; Especially improving `pointers` tagged posts :) – haccks Jul 27 '13 at 14:46
  • please provide citation always, otherwise people like me will be confused. I read a little about calloc and it does do magic for larger memory allocation http://stackoverflow.com/questions/2688466/why-mallocmemset-is-slower-than-calloc . ? – yeyo Jul 27 '13 at 14:47
  • 2
    @GrijeshChauhan; Yes. Your way to deal with pointer is easy to understand (and also different from others on SO). Keep it up. – haccks Jul 27 '13 at 14:58
  • 1
    @haccks, furthermore Grijesh is Mr.ASCII-Man :P, I love his answers!! – David Ranieri Jul 27 '13 at 15:07
  • 2
    @DavidRF ha ha...yes I like arcii arts, I have given a link of asciiflow in my profile....Thanks very much to all of you! – Grijesh Chauhan Jul 27 '13 at 15:55
0

You can't eliminate the initialization time, no matter what you do. The best you can do is to attempt to optimize it. 2 techniques come to mind:

  • Efficiently use your HW. Use native size memory accesses to initialize the buffer. E.g., over 32-bit architecture loop and write in 4-byte chunks.
  • Unroll the loop to eliminate control overhead.

Unrolling will probably be applied anyway by optimizing compiler.

You can also try to move the initialization overhead to a non-critical section. E.g., you can use static variables that will be zero-initialized upon program start. Or, in contrast, you can delay the initialization till first use (lazy initialization).

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85