Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Status of pointer variables in a struct after using malloc

Here is my struct definition:-

struct Node{
    void *data;
    struct Node *left;
    struct Node *right;
};

If I allocate memory for this struct using malloc, will the pointer variables be NULL?

int main(int argc, char** argv){
    Node *node = malloc(sizeof(struct Node));
    if(node->left == NULL){
        //Is it guaranteed to be NULL?
    }
    return 0;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Section 7.22.3.4 The malloc Function

The standard states:

#include <stdlib.h>
void *malloc(size_t size);

Description

The malloc function allocates space for an object whose size is
specified by size and whose value is indeterminate.

It means that the content of the memory allocated by malloc cannot be determined.

   //Is it guaranteed to be NULL?

No, it is not and assuming it may invoke undefined behaviour.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading