Initializing struct member of type struct pointer to null












0















I'm having an issue with assigning a null value to a pointer to a struct member (that is also a pointer to a struct). The following code correctly sets the headNode->right variable equal to null, but not the headNode->left variable.



typedef struct node {
char *key;
int frequency;
struct node *left;
struct node *right;
struct node *parent;
} node;


void addKey(char key) {
extern node *headNode;
node *newNode;

if (headNode != NULL) {
printf("Head node is initializedn");
if (headNode->left != NULL) printf(" Left is not nulln");
if (headNode->right != NULL) printf(" Right is not nulln");
}


newNode = malloc(sizeof(node*));
newNode->key = malloc(sizeof(char) * (strlen(key) + 1));
newNode->left = malloc(sizeof(node*));
newNode->right = malloc(sizeof(node*));
newNode->parent = malloc(sizeof(node*));


newNode->left = newNode->right = NULL;
newNode->frequency = 1;
newNode->right = NULL;
strcpy(newNode->key, key);

// If this is the first node, assign it as the root
if (headNode == NULL) {
newNode->parent = NULL;
headNode = newNode;
return;
}
}


However, if I add the following two lines right before the return statement, it works correctly.



if (headNode->left == NULL) printf("L is nulln");
else printf("L is NOT nulln");


I don't understand how an if statement can make a difference. There is nowhere else that assigns or changes the value of this variable anywhere else in my code.










share|improve this question

























  • if I add the following two lines right before the last return statement – There is only one return statement that is within an if that establishes that headNode == NULL!?

    – Swordfish
    Nov 25 '18 at 10:23













  • I try to avoid this sort of syntax: newNode->left = newNode->right = NULL because the evaluation order makes a difference and it's confusing. And why do you set newNode->right = NULL two lines later?

    – Paul
    Nov 25 '18 at 10:23
















0















I'm having an issue with assigning a null value to a pointer to a struct member (that is also a pointer to a struct). The following code correctly sets the headNode->right variable equal to null, but not the headNode->left variable.



typedef struct node {
char *key;
int frequency;
struct node *left;
struct node *right;
struct node *parent;
} node;


void addKey(char key) {
extern node *headNode;
node *newNode;

if (headNode != NULL) {
printf("Head node is initializedn");
if (headNode->left != NULL) printf(" Left is not nulln");
if (headNode->right != NULL) printf(" Right is not nulln");
}


newNode = malloc(sizeof(node*));
newNode->key = malloc(sizeof(char) * (strlen(key) + 1));
newNode->left = malloc(sizeof(node*));
newNode->right = malloc(sizeof(node*));
newNode->parent = malloc(sizeof(node*));


newNode->left = newNode->right = NULL;
newNode->frequency = 1;
newNode->right = NULL;
strcpy(newNode->key, key);

// If this is the first node, assign it as the root
if (headNode == NULL) {
newNode->parent = NULL;
headNode = newNode;
return;
}
}


However, if I add the following two lines right before the return statement, it works correctly.



if (headNode->left == NULL) printf("L is nulln");
else printf("L is NOT nulln");


I don't understand how an if statement can make a difference. There is nowhere else that assigns or changes the value of this variable anywhere else in my code.










share|improve this question

























  • if I add the following two lines right before the last return statement – There is only one return statement that is within an if that establishes that headNode == NULL!?

    – Swordfish
    Nov 25 '18 at 10:23













  • I try to avoid this sort of syntax: newNode->left = newNode->right = NULL because the evaluation order makes a difference and it's confusing. And why do you set newNode->right = NULL two lines later?

    – Paul
    Nov 25 '18 at 10:23














0












0








0








I'm having an issue with assigning a null value to a pointer to a struct member (that is also a pointer to a struct). The following code correctly sets the headNode->right variable equal to null, but not the headNode->left variable.



typedef struct node {
char *key;
int frequency;
struct node *left;
struct node *right;
struct node *parent;
} node;


void addKey(char key) {
extern node *headNode;
node *newNode;

if (headNode != NULL) {
printf("Head node is initializedn");
if (headNode->left != NULL) printf(" Left is not nulln");
if (headNode->right != NULL) printf(" Right is not nulln");
}


newNode = malloc(sizeof(node*));
newNode->key = malloc(sizeof(char) * (strlen(key) + 1));
newNode->left = malloc(sizeof(node*));
newNode->right = malloc(sizeof(node*));
newNode->parent = malloc(sizeof(node*));


newNode->left = newNode->right = NULL;
newNode->frequency = 1;
newNode->right = NULL;
strcpy(newNode->key, key);

// If this is the first node, assign it as the root
if (headNode == NULL) {
newNode->parent = NULL;
headNode = newNode;
return;
}
}


However, if I add the following two lines right before the return statement, it works correctly.



if (headNode->left == NULL) printf("L is nulln");
else printf("L is NOT nulln");


I don't understand how an if statement can make a difference. There is nowhere else that assigns or changes the value of this variable anywhere else in my code.










share|improve this question
















I'm having an issue with assigning a null value to a pointer to a struct member (that is also a pointer to a struct). The following code correctly sets the headNode->right variable equal to null, but not the headNode->left variable.



typedef struct node {
char *key;
int frequency;
struct node *left;
struct node *right;
struct node *parent;
} node;


void addKey(char key) {
extern node *headNode;
node *newNode;

if (headNode != NULL) {
printf("Head node is initializedn");
if (headNode->left != NULL) printf(" Left is not nulln");
if (headNode->right != NULL) printf(" Right is not nulln");
}


newNode = malloc(sizeof(node*));
newNode->key = malloc(sizeof(char) * (strlen(key) + 1));
newNode->left = malloc(sizeof(node*));
newNode->right = malloc(sizeof(node*));
newNode->parent = malloc(sizeof(node*));


newNode->left = newNode->right = NULL;
newNode->frequency = 1;
newNode->right = NULL;
strcpy(newNode->key, key);

// If this is the first node, assign it as the root
if (headNode == NULL) {
newNode->parent = NULL;
headNode = newNode;
return;
}
}


However, if I add the following two lines right before the return statement, it works correctly.



if (headNode->left == NULL) printf("L is nulln");
else printf("L is NOT nulln");


I don't understand how an if statement can make a difference. There is nowhere else that assigns or changes the value of this variable anywhere else in my code.







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 10:26







Jake

















asked Nov 25 '18 at 10:17









JakeJake

77212




77212













  • if I add the following two lines right before the last return statement – There is only one return statement that is within an if that establishes that headNode == NULL!?

    – Swordfish
    Nov 25 '18 at 10:23













  • I try to avoid this sort of syntax: newNode->left = newNode->right = NULL because the evaluation order makes a difference and it's confusing. And why do you set newNode->right = NULL two lines later?

    – Paul
    Nov 25 '18 at 10:23



















  • if I add the following two lines right before the last return statement – There is only one return statement that is within an if that establishes that headNode == NULL!?

    – Swordfish
    Nov 25 '18 at 10:23













  • I try to avoid this sort of syntax: newNode->left = newNode->right = NULL because the evaluation order makes a difference and it's confusing. And why do you set newNode->right = NULL two lines later?

    – Paul
    Nov 25 '18 at 10:23

















if I add the following two lines right before the last return statement – There is only one return statement that is within an if that establishes that headNode == NULL!?

– Swordfish
Nov 25 '18 at 10:23







if I add the following two lines right before the last return statement – There is only one return statement that is within an if that establishes that headNode == NULL!?

– Swordfish
Nov 25 '18 at 10:23















I try to avoid this sort of syntax: newNode->left = newNode->right = NULL because the evaluation order makes a difference and it's confusing. And why do you set newNode->right = NULL two lines later?

– Paul
Nov 25 '18 at 10:23





I try to avoid this sort of syntax: newNode->left = newNode->right = NULL because the evaluation order makes a difference and it's confusing. And why do you set newNode->right = NULL two lines later?

– Paul
Nov 25 '18 at 10:23












2 Answers
2






active

oldest

votes


















3














Take a step back. There are a number of serious mistakes here.



You declare a pointer to a node:



node *newNode;



So far, so good. However, when you try to allocate memory for this new node, you only allocate enough to store a pointer, instead of the node itself:



newNode = malloc(sizeof(node*));



You already have storage for the pointer: newNode.



Change this line to:



newNode = malloc(sizeof(node));



A little later you make three more unnecessary calls to malloc:



newNode->left = malloc(sizeof(node*));
newNode->right = malloc(sizeof(node*));
newNode->parent = malloc(sizeof(node*));


If you had requested enough memory (i.e. sizeof(node)) in the previous call, there would be no need to allocate any more memory for these internal fields. You would already have memory for them; it would already exists at the address pointed to by newNode.



Worse still, you then go on to leak memory on the next line:



newNode->left = newNode->right = NULL;



Here you are throwing away the addresses of the memory you allocated unnecessarily.



I think you need to read up on memory management further before we can troubleshoot this problem for you.






share|improve this answer



















  • 1





    Thank you for the detailed reply. I should have realized that I needed to allocate enough memory for the struct rather than the pointer for newNode. I remember figuring that out before, I just completely blanked. As for the three unnecessary malloc calls, that was from me trying to figure it out based off of other SO posts. Your explanation makes sense why that was unnecessary though. In regards to the memory leak; how would I deal with that? I need to have those values set to NULL until the nodes they will eventually point to are created. Thank you so much again for your response.

    – Jake
    Nov 25 '18 at 10:37








  • 1





    @Jake no problem. Sorry if my reply sounded harsh. It looks like you're not comfortable with pointers yet - that's cool, they're one of C's trickier concepts. But they're also really dangerous in the wrong hands, so it's worth making sure this stuff is second-nature before trying to build trees and lists etc.

    – linguamachina
    Nov 25 '18 at 10:40











  • Sorry, I posted that early by accident and added an edit onto it. And no worries, I definitely know I need to get a better understanding of them.

    – Jake
    Nov 25 '18 at 10:42






  • 1





    Regarding the memory leak, if you simply use malloc(sizeof(node)) in the first place, you can set newNode->left and newNode->right to NULL just as you have done. Those values will simply be zeros in the block of memory pointed to by newNode. Later, when you want to call free(newNode) you'll need to check if newNode->left and newNode->right are non-NULL, and deal with them first (if they are non-NULL they will point to different blocks of memory that need to be freed separately. This stuff should be wrapped up in a deleteNode function.

    – linguamachina
    Nov 25 '18 at 10:43













  • Got it. Thank you again, that was really helpful!

    – Jake
    Nov 25 '18 at 10:45



















4














All the malloc(sizeof(node*)) should be malloc(sizeof(node)). You're only alloocating enough space for a pointer, not the whole structure. This is causing undefined behavior with all the following code that indirects through these pointers.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466525%2finitializing-struct-member-of-type-struct-pointer-to-null%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Take a step back. There are a number of serious mistakes here.



    You declare a pointer to a node:



    node *newNode;



    So far, so good. However, when you try to allocate memory for this new node, you only allocate enough to store a pointer, instead of the node itself:



    newNode = malloc(sizeof(node*));



    You already have storage for the pointer: newNode.



    Change this line to:



    newNode = malloc(sizeof(node));



    A little later you make three more unnecessary calls to malloc:



    newNode->left = malloc(sizeof(node*));
    newNode->right = malloc(sizeof(node*));
    newNode->parent = malloc(sizeof(node*));


    If you had requested enough memory (i.e. sizeof(node)) in the previous call, there would be no need to allocate any more memory for these internal fields. You would already have memory for them; it would already exists at the address pointed to by newNode.



    Worse still, you then go on to leak memory on the next line:



    newNode->left = newNode->right = NULL;



    Here you are throwing away the addresses of the memory you allocated unnecessarily.



    I think you need to read up on memory management further before we can troubleshoot this problem for you.






    share|improve this answer



















    • 1





      Thank you for the detailed reply. I should have realized that I needed to allocate enough memory for the struct rather than the pointer for newNode. I remember figuring that out before, I just completely blanked. As for the three unnecessary malloc calls, that was from me trying to figure it out based off of other SO posts. Your explanation makes sense why that was unnecessary though. In regards to the memory leak; how would I deal with that? I need to have those values set to NULL until the nodes they will eventually point to are created. Thank you so much again for your response.

      – Jake
      Nov 25 '18 at 10:37








    • 1





      @Jake no problem. Sorry if my reply sounded harsh. It looks like you're not comfortable with pointers yet - that's cool, they're one of C's trickier concepts. But they're also really dangerous in the wrong hands, so it's worth making sure this stuff is second-nature before trying to build trees and lists etc.

      – linguamachina
      Nov 25 '18 at 10:40











    • Sorry, I posted that early by accident and added an edit onto it. And no worries, I definitely know I need to get a better understanding of them.

      – Jake
      Nov 25 '18 at 10:42






    • 1





      Regarding the memory leak, if you simply use malloc(sizeof(node)) in the first place, you can set newNode->left and newNode->right to NULL just as you have done. Those values will simply be zeros in the block of memory pointed to by newNode. Later, when you want to call free(newNode) you'll need to check if newNode->left and newNode->right are non-NULL, and deal with them first (if they are non-NULL they will point to different blocks of memory that need to be freed separately. This stuff should be wrapped up in a deleteNode function.

      – linguamachina
      Nov 25 '18 at 10:43













    • Got it. Thank you again, that was really helpful!

      – Jake
      Nov 25 '18 at 10:45
















    3














    Take a step back. There are a number of serious mistakes here.



    You declare a pointer to a node:



    node *newNode;



    So far, so good. However, when you try to allocate memory for this new node, you only allocate enough to store a pointer, instead of the node itself:



    newNode = malloc(sizeof(node*));



    You already have storage for the pointer: newNode.



    Change this line to:



    newNode = malloc(sizeof(node));



    A little later you make three more unnecessary calls to malloc:



    newNode->left = malloc(sizeof(node*));
    newNode->right = malloc(sizeof(node*));
    newNode->parent = malloc(sizeof(node*));


    If you had requested enough memory (i.e. sizeof(node)) in the previous call, there would be no need to allocate any more memory for these internal fields. You would already have memory for them; it would already exists at the address pointed to by newNode.



    Worse still, you then go on to leak memory on the next line:



    newNode->left = newNode->right = NULL;



    Here you are throwing away the addresses of the memory you allocated unnecessarily.



    I think you need to read up on memory management further before we can troubleshoot this problem for you.






    share|improve this answer



















    • 1





      Thank you for the detailed reply. I should have realized that I needed to allocate enough memory for the struct rather than the pointer for newNode. I remember figuring that out before, I just completely blanked. As for the three unnecessary malloc calls, that was from me trying to figure it out based off of other SO posts. Your explanation makes sense why that was unnecessary though. In regards to the memory leak; how would I deal with that? I need to have those values set to NULL until the nodes they will eventually point to are created. Thank you so much again for your response.

      – Jake
      Nov 25 '18 at 10:37








    • 1





      @Jake no problem. Sorry if my reply sounded harsh. It looks like you're not comfortable with pointers yet - that's cool, they're one of C's trickier concepts. But they're also really dangerous in the wrong hands, so it's worth making sure this stuff is second-nature before trying to build trees and lists etc.

      – linguamachina
      Nov 25 '18 at 10:40











    • Sorry, I posted that early by accident and added an edit onto it. And no worries, I definitely know I need to get a better understanding of them.

      – Jake
      Nov 25 '18 at 10:42






    • 1





      Regarding the memory leak, if you simply use malloc(sizeof(node)) in the first place, you can set newNode->left and newNode->right to NULL just as you have done. Those values will simply be zeros in the block of memory pointed to by newNode. Later, when you want to call free(newNode) you'll need to check if newNode->left and newNode->right are non-NULL, and deal with them first (if they are non-NULL they will point to different blocks of memory that need to be freed separately. This stuff should be wrapped up in a deleteNode function.

      – linguamachina
      Nov 25 '18 at 10:43













    • Got it. Thank you again, that was really helpful!

      – Jake
      Nov 25 '18 at 10:45














    3












    3








    3







    Take a step back. There are a number of serious mistakes here.



    You declare a pointer to a node:



    node *newNode;



    So far, so good. However, when you try to allocate memory for this new node, you only allocate enough to store a pointer, instead of the node itself:



    newNode = malloc(sizeof(node*));



    You already have storage for the pointer: newNode.



    Change this line to:



    newNode = malloc(sizeof(node));



    A little later you make three more unnecessary calls to malloc:



    newNode->left = malloc(sizeof(node*));
    newNode->right = malloc(sizeof(node*));
    newNode->parent = malloc(sizeof(node*));


    If you had requested enough memory (i.e. sizeof(node)) in the previous call, there would be no need to allocate any more memory for these internal fields. You would already have memory for them; it would already exists at the address pointed to by newNode.



    Worse still, you then go on to leak memory on the next line:



    newNode->left = newNode->right = NULL;



    Here you are throwing away the addresses of the memory you allocated unnecessarily.



    I think you need to read up on memory management further before we can troubleshoot this problem for you.






    share|improve this answer













    Take a step back. There are a number of serious mistakes here.



    You declare a pointer to a node:



    node *newNode;



    So far, so good. However, when you try to allocate memory for this new node, you only allocate enough to store a pointer, instead of the node itself:



    newNode = malloc(sizeof(node*));



    You already have storage for the pointer: newNode.



    Change this line to:



    newNode = malloc(sizeof(node));



    A little later you make three more unnecessary calls to malloc:



    newNode->left = malloc(sizeof(node*));
    newNode->right = malloc(sizeof(node*));
    newNode->parent = malloc(sizeof(node*));


    If you had requested enough memory (i.e. sizeof(node)) in the previous call, there would be no need to allocate any more memory for these internal fields. You would already have memory for them; it would already exists at the address pointed to by newNode.



    Worse still, you then go on to leak memory on the next line:



    newNode->left = newNode->right = NULL;



    Here you are throwing away the addresses of the memory you allocated unnecessarily.



    I think you need to read up on memory management further before we can troubleshoot this problem for you.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 10:31









    linguamachinalinguamachina

    2,08311318




    2,08311318








    • 1





      Thank you for the detailed reply. I should have realized that I needed to allocate enough memory for the struct rather than the pointer for newNode. I remember figuring that out before, I just completely blanked. As for the three unnecessary malloc calls, that was from me trying to figure it out based off of other SO posts. Your explanation makes sense why that was unnecessary though. In regards to the memory leak; how would I deal with that? I need to have those values set to NULL until the nodes they will eventually point to are created. Thank you so much again for your response.

      – Jake
      Nov 25 '18 at 10:37








    • 1





      @Jake no problem. Sorry if my reply sounded harsh. It looks like you're not comfortable with pointers yet - that's cool, they're one of C's trickier concepts. But they're also really dangerous in the wrong hands, so it's worth making sure this stuff is second-nature before trying to build trees and lists etc.

      – linguamachina
      Nov 25 '18 at 10:40











    • Sorry, I posted that early by accident and added an edit onto it. And no worries, I definitely know I need to get a better understanding of them.

      – Jake
      Nov 25 '18 at 10:42






    • 1





      Regarding the memory leak, if you simply use malloc(sizeof(node)) in the first place, you can set newNode->left and newNode->right to NULL just as you have done. Those values will simply be zeros in the block of memory pointed to by newNode. Later, when you want to call free(newNode) you'll need to check if newNode->left and newNode->right are non-NULL, and deal with them first (if they are non-NULL they will point to different blocks of memory that need to be freed separately. This stuff should be wrapped up in a deleteNode function.

      – linguamachina
      Nov 25 '18 at 10:43













    • Got it. Thank you again, that was really helpful!

      – Jake
      Nov 25 '18 at 10:45














    • 1





      Thank you for the detailed reply. I should have realized that I needed to allocate enough memory for the struct rather than the pointer for newNode. I remember figuring that out before, I just completely blanked. As for the three unnecessary malloc calls, that was from me trying to figure it out based off of other SO posts. Your explanation makes sense why that was unnecessary though. In regards to the memory leak; how would I deal with that? I need to have those values set to NULL until the nodes they will eventually point to are created. Thank you so much again for your response.

      – Jake
      Nov 25 '18 at 10:37








    • 1





      @Jake no problem. Sorry if my reply sounded harsh. It looks like you're not comfortable with pointers yet - that's cool, they're one of C's trickier concepts. But they're also really dangerous in the wrong hands, so it's worth making sure this stuff is second-nature before trying to build trees and lists etc.

      – linguamachina
      Nov 25 '18 at 10:40











    • Sorry, I posted that early by accident and added an edit onto it. And no worries, I definitely know I need to get a better understanding of them.

      – Jake
      Nov 25 '18 at 10:42






    • 1





      Regarding the memory leak, if you simply use malloc(sizeof(node)) in the first place, you can set newNode->left and newNode->right to NULL just as you have done. Those values will simply be zeros in the block of memory pointed to by newNode. Later, when you want to call free(newNode) you'll need to check if newNode->left and newNode->right are non-NULL, and deal with them first (if they are non-NULL they will point to different blocks of memory that need to be freed separately. This stuff should be wrapped up in a deleteNode function.

      – linguamachina
      Nov 25 '18 at 10:43













    • Got it. Thank you again, that was really helpful!

      – Jake
      Nov 25 '18 at 10:45








    1




    1





    Thank you for the detailed reply. I should have realized that I needed to allocate enough memory for the struct rather than the pointer for newNode. I remember figuring that out before, I just completely blanked. As for the three unnecessary malloc calls, that was from me trying to figure it out based off of other SO posts. Your explanation makes sense why that was unnecessary though. In regards to the memory leak; how would I deal with that? I need to have those values set to NULL until the nodes they will eventually point to are created. Thank you so much again for your response.

    – Jake
    Nov 25 '18 at 10:37







    Thank you for the detailed reply. I should have realized that I needed to allocate enough memory for the struct rather than the pointer for newNode. I remember figuring that out before, I just completely blanked. As for the three unnecessary malloc calls, that was from me trying to figure it out based off of other SO posts. Your explanation makes sense why that was unnecessary though. In regards to the memory leak; how would I deal with that? I need to have those values set to NULL until the nodes they will eventually point to are created. Thank you so much again for your response.

    – Jake
    Nov 25 '18 at 10:37






    1




    1





    @Jake no problem. Sorry if my reply sounded harsh. It looks like you're not comfortable with pointers yet - that's cool, they're one of C's trickier concepts. But they're also really dangerous in the wrong hands, so it's worth making sure this stuff is second-nature before trying to build trees and lists etc.

    – linguamachina
    Nov 25 '18 at 10:40





    @Jake no problem. Sorry if my reply sounded harsh. It looks like you're not comfortable with pointers yet - that's cool, they're one of C's trickier concepts. But they're also really dangerous in the wrong hands, so it's worth making sure this stuff is second-nature before trying to build trees and lists etc.

    – linguamachina
    Nov 25 '18 at 10:40













    Sorry, I posted that early by accident and added an edit onto it. And no worries, I definitely know I need to get a better understanding of them.

    – Jake
    Nov 25 '18 at 10:42





    Sorry, I posted that early by accident and added an edit onto it. And no worries, I definitely know I need to get a better understanding of them.

    – Jake
    Nov 25 '18 at 10:42




    1




    1





    Regarding the memory leak, if you simply use malloc(sizeof(node)) in the first place, you can set newNode->left and newNode->right to NULL just as you have done. Those values will simply be zeros in the block of memory pointed to by newNode. Later, when you want to call free(newNode) you'll need to check if newNode->left and newNode->right are non-NULL, and deal with them first (if they are non-NULL they will point to different blocks of memory that need to be freed separately. This stuff should be wrapped up in a deleteNode function.

    – linguamachina
    Nov 25 '18 at 10:43







    Regarding the memory leak, if you simply use malloc(sizeof(node)) in the first place, you can set newNode->left and newNode->right to NULL just as you have done. Those values will simply be zeros in the block of memory pointed to by newNode. Later, when you want to call free(newNode) you'll need to check if newNode->left and newNode->right are non-NULL, and deal with them first (if they are non-NULL they will point to different blocks of memory that need to be freed separately. This stuff should be wrapped up in a deleteNode function.

    – linguamachina
    Nov 25 '18 at 10:43















    Got it. Thank you again, that was really helpful!

    – Jake
    Nov 25 '18 at 10:45





    Got it. Thank you again, that was really helpful!

    – Jake
    Nov 25 '18 at 10:45













    4














    All the malloc(sizeof(node*)) should be malloc(sizeof(node)). You're only alloocating enough space for a pointer, not the whole structure. This is causing undefined behavior with all the following code that indirects through these pointers.






    share|improve this answer




























      4














      All the malloc(sizeof(node*)) should be malloc(sizeof(node)). You're only alloocating enough space for a pointer, not the whole structure. This is causing undefined behavior with all the following code that indirects through these pointers.






      share|improve this answer


























        4












        4








        4







        All the malloc(sizeof(node*)) should be malloc(sizeof(node)). You're only alloocating enough space for a pointer, not the whole structure. This is causing undefined behavior with all the following code that indirects through these pointers.






        share|improve this answer













        All the malloc(sizeof(node*)) should be malloc(sizeof(node)). You're only alloocating enough space for a pointer, not the whole structure. This is causing undefined behavior with all the following code that indirects through these pointers.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 10:27









        BarmarBarmar

        431k36254354




        431k36254354






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466525%2finitializing-struct-member-of-type-struct-pointer-to-null%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ottavio Pratesi

            Tricia Helfer

            15 giugno