There is probably no need to do a memset : you cannot use colors[k] before setting it with something valid later. For example, your code sets colors[i] to the highlighted highlighted color pointer, so you don't need to set colors[i] to NULL .
But even if you want to โzero it out so that everything is fine,โ or you really need the new pointers to be NULL : the C standard does not guarantee that all-bits-zero is a constant of a null pointer (i.e. NULL ), so memset() in any case is not the right decision.
The only portable thing you can do is set each pointer to NULL in a loop:
size_t k; for (k=COLORCOUNT; k < i+1; ++k) colors[k] = NULL;
The main problem is that your call to realloc() incorrect. realloc() returns a pointer to the modified memory; it (optionally) does not change its size.
So you should do:
rgb_t **tmp = realloc(colors, (i+1) * sizeof *tmp); if (tmp != NULL) { colors = tmp; } else { }
If you really want to know what the memset() call should be, you need to set zero memory starting with colors+COLORCOUNT and set the i+1-COLORCOUNT members to zero:
memset(colors+COLORCOUNT, 0, (i+1-COLORCOUNT) * sizeof *colors);
But, as I said above, all 0 bytes are not guaranteed to be a NULL pointer, so your memset() useless. You should use a loop if you want NULL pointers.
Alok singhal
source share