2

Got stuck in the simplest problem.

int *p= (int *)malloc(m*sizeof(int));
p={0}; // this is not correct.

How to set the whole array to the value 0 other than using loops?

Taitai
  • 584
  • 2
  • 7
  • 18
  • if you are really declaring an array and not a pointer, you can initialize the array to zeroes at declaration like this: `int p[m] = {0};`. – bruceg Sep 14 '18 at 01:14
  • 1
    Can't find a single canonical duplicate, but this is fully covered between [Zero an array in C code](https://stackoverflow.com/q/5636070/364696) and [Difference between malloc and calloc?](https://stackoverflow.com/q/1538420/364696). – ShadowRanger Sep 14 '18 at 01:21

2 Answers2

6

Either use calloc() rather than malloc() in the first instance to allocate the memory already zeroed, use or memset() after the allocation:

 int * p = calloc(m, sizeof(int));

OR

int * p = malloc(m * sizeof(int));
memset(p, 0, m * sizeof(int));

Obviously, the former is preferable.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • In the context of the OP's example, `memset(p, 0, n);` using `n` is not all that clear; `memset(p, 0, m * sizeof(int));` or `memset(p, 0, m * sizeof(*p));` would be correct in context. And `calloc` receives the element count and the element size separately, not as a single parameter. – ShadowRanger Sep 14 '18 at 01:15
  • `memset(p, 0, m);` --> too small. Suggest `memset(p, 0, sizeof *p * m);` – chux - Reinstate Monica Sep 14 '18 at 01:16
  • Looks about right now. – Havenard Sep 14 '18 at 01:18
  • 1
    Perfect time to discuss which one is faster. – Havenard Sep 14 '18 at 01:19
  • @Havenard Speed determination is problematic. Advanced `calloc()` will allocate quickly and only zero the data when its first read - lots of under the table possibilities. `memset(p, 0, m * sizeof(int))` happens right then. – chux - Reinstate Monica Sep 14 '18 at 01:22
  • @Havenard: Short version is: They're usually the same, unless you're allocating a huge amount of memory at once, in which case `calloc` could conceivably request already zeroed memory directly from the OS (which may have eagerly zeroed pages in the background, or may simply map the zero page as copy on write, either one of which will return control to your program faster than `malloc` + `memset`). – ShadowRanger Sep 14 '18 at 01:24
  • @chux Sounds far fetched to me, how is it going to test later on if the space is used to only zero it then? Specially if it's conditionally allocated, it's not something the compiler can determine at compile time. ShadowRanger theory makes sense though. – Havenard Sep 14 '18 at 01:26
  • @Havenard "how is it going to test later on" --> post as a question - answer too big for a comment. Also see [malloc not “using up” the memory](https://stackoverflow.com/q/19991623/2410359). – chux - Reinstate Monica Sep 14 '18 at 01:30
  • 2
    @chux That is different, the system won't initialize memory pages until the program actually attempts to use them, even if `malloc()` reserved the space. – Havenard Sep 14 '18 at 01:31
  • @Havenard Not saying that is the same - it is just related – chux - Reinstate Monica Sep 14 '18 at 01:40
  • @chux Well I guess if you look at it the way ShadowRanger put it it makes sense, by allocating over virgin memory pages, the system kernel will take care of automatically providing zero'ed space as you attempt to use it (unless you are using Windows 98 or older) and there is no need to go through the trouble of zeroing the space yourself. – Havenard Sep 14 '18 at 01:46
2

use calloc:

 int * p = calloc(m, sizeof(int))
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
Rafael
  • 7,605
  • 13
  • 31
  • 46