Lets say I have the following:
CHARLINK * _init_link(CHARLINK **link)
{
short i;
(*link)->cl = (CHARLINK **) calloc(NUM_CHARS, sizeof(CHARLINK *));
for (i = 0; i < NUM_CHARS; i++)
(*link)->cl[i] = NULL;
ret开发者_高级运维urn (*link);
}
Is the loop to initialize each element to NULL necessary or are they automatically NULL from calloc?
Yes, the assignment to NULL in the loop is necessary. calloc
initialises to all bits 0. But a null pointer may not be represented like that. It is implementation dependent. Thus the assignment is necessary.
That depends a bit on your system, but in the vast majority of cases it's ok. calloc()
returns you a buffer filled with zeros. However, the null pointer on your machine might not be a bit pattern of 0. On a machine where the null pointer is non-zero, you might end up in trouble.
No, calloc initializes its buffers to 0's.
精彩评论