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
}