ZarakshR
ZarakshR

Reputation: 1573

Is a null check required for array initialization in C? Can array initialization in C fail?

I know that in C it is good practice to check for NULL pointer every time malloc() or calloc() is called. Do I have to do the same for array initialization? For example:

int sigcheck[5];
if (sigcheck == NULL) {return;}

Is line 2 necessary? If I'm not wrong, array initialization works like calling calloc() under the hood, does this under-the-hood functioning take the possibility of NULL into account or is it necessary/good practice for us to do it ourselves.

Upvotes: 1

Views: 825

Answers (3)

John Bode
John Bode

Reputation: 123448

Memory for arrays is allocated like memory for any other variable type - there's no separate calloc or similar call under the hood. After the line

int sigcheck[5];

what you wind up with in memory is

          +---+
sigcheck: |   | sigcheck[0]
          +---+ 
          |   | sigcheck[1]
          +---+ 
           ...
          +---+
          |   | sigcheck[4]
          +---+

So there's no need to perform a NULL check against sigcheck in this case.

Where people get confused is that under most conditions the expression sigcheck will be converted, or "decay", from type "5-element array of int" to type "pointer to int", and the value of the expression will be the address of the first element of the array. This concept often gets garbled to where people think sigcheck is a pointer object separate from the array itself, but it isn't.

When you allocate memory dynamically through malloc or calloc, such as

int *sigcheck = calloc( 5, sizeof *sigcheck );

then (assuming the request succeeds) what you wind up with in memory is

          +---+
sigcheck: |   | ---+
          +---+    |
            +------+
            |  
            V
          +---+
          |   | sigcheck[0]
          +---+
          |   | sigcheck[1]
          +---+ 
           ...
          +---+
          |   | sigcheck[4]
          +---+

In this case the sigcheck is a separate object from the array elements. And because malloc, calloc, and realloc will return NULL if the memory request cannot be satisfied, then you do need to make a NULL check on sigcheck:

int *sigcheck = calloc( 5, sizeof *sigcheck );
if ( sigcheck )
{
  // do stuff
}
else
{
  // memory allocation failed, handle as appropriate
}

Upvotes: 3

Lundin
Lundin

Reputation: 213458

it is good practice to check for NULL pointer every time malloc() or calloc() is called

Yes, because those are functions documented to return NULL upon failure. That's the sole reason why.

do I have to do the same for array initialization

No.

Is line 2 necessary?

No, it doesn't make any sense. The array can't have address null, it always holds a memory position somewhere - where it ends up depends on something that C calls storage duration. In this case it either has static storage duration or automatic storage duration, depending on if it was allocated outside a function or inside a function.

If I'm not wrong, array initialization works like calling calloc() under the hood

You are wrong. calloc is a special case used for explicit memory allocation by the programmer, so called allocated storage duration. malloc family of functions is never called implicitly or silently. (Unless you call a function which claims to call malloc in turn, such as strdup.)

You might find this question interesting: A program uses different regions of memory for static objects, automatic objects, and dynamically allocated objects

Upvotes: 7

K  D
K D

Reputation: 314

From the documentation of malloc:

If the function failed to allocate the requested block of memory, a null pointer is returned.

So you should check whether malloc returns NULL precisely because it might fail to allocate you the requested chunk (although this is usually unlikely).

Static allocation does not call calloc under the hood, since that would allocate your array on the heap and not on the stack. The space needed for static allocations is determined at compile time, and if sufficient amount of memory cannot be allocated your program will fail to load. Take a look at this question.

Upvotes: 1

Related Questions