claf
claf

Reputation: 9263

C : static array

I need to have a static void* array[1024]; in a library and i need to have it set to NULL for every entries.

My question is about the best way to set the entire array to NULL, is a memset (array, NULL, sizeof (void*) * 1024) the best solution?

Upvotes: 5

Views: 16412

Answers (5)

paxdiablo
paxdiablo

Reputation: 882756

static void* array[1024] = {0};

or, as kmkaplan points out in the comment, just:

static void* array[1024];

although I prefer the first solution just to remind me that it's set to zeros (the compiler shouldn't generate any code for this unless it's brain-dead).

You don't need to memset it at all since file-level variables are initialized to zero anyway.

If you need to reset them to NULLs at some point after startup, use:

memset(array, 0, sizeof(array));

This works on most platforms (every one I've ever seen and that's quite a lot) since the null pointer is usually zero bits. But the standard doesn't guarantee this so, on those obscure platforms, it's safer to do:

for (i = 0; i < sizeof(array) / sizeof(void*); i++)
    array[i] = NULL;

Upvotes: 9

Gal Goldman
Gal Goldman

Reputation: 8879

All you need is:


static void* array[1024];

because static variables are initialized to 0 anyways, so you don't really need to initiate this array, it will happen automatically.

Upvotes: 1

Christoph
Christoph

Reputation: 169833

static pointers are automatically initialized to NULL. See C99:TC3 6.7.8, §10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules;
  • if it is a union, the first named member is initialized (recursively) according to these rules.

Also, contrary to what others have written,

memset(array, 0, sizeof(array));

isn't guaranteed to result in null pointers. On some systems, a pointer with all its bits set to 0 might actually be valid. For mainstream systems, that's not the case, but with embedded systems, one never knows.

The safe way to set all entries to a null pointer is to either loop over the entries yourself and set them to NULL, or to use an uninitialized static array which you can memcpy() over the array you want to set to NULL.

Upvotes: 26

kgiannakakis
kgiannakakis

Reputation: 104196

memset will do it in runtime.

static void* array[1024] = {0};

suggested by Pax will do it in compile time and it will increase the size of the executable.

For a library I believe memset is more appropriate.

Upvotes: 3

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422310

Nope, it's wrong. You aren't considering the size of each element.

memset(array, 0, sizeof(array)); // this works only on static arrays.

Generally you should use:

memset(array_reference, 0, size_of_array * sizeof(array_element_type));

Upvotes: 0

Related Questions