If I set a pointer to a struct to point to a block of memory via malloc, will all the members initialize to their respective default values? Such as int's to 0 and pointer's to NULL? I'm seeing that they do based on this sample code I wrote, so I just wanted someone to please confirm my understanding. Thanks for the input.
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct node
{
bool value;
struct node* next[5];
}
node;
int main(void)
{
node* tNode = malloc(sizeof(node));
printf("value = %i\n", tNode->value);
for (int i = 0; i < 5; i++)
{
if (tNode->next[i] == NULL)
printf("next[%i] = %p\n", i, tNode->next[i]);
}
}
malloc()
function never initializes the memory region. You have to use calloc()
to specifically initialize a memory region to zeros. The reason you see initialized memory is explained here.