Why doesn't this object get set to NULL after the function call?

Bob John
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

struct Person{
    char* first_name;
    char* last_name;
};

struct Person* create_person(const char* f_name, const char* l_name)
{
    struct Person* new_person = malloc(sizeof(struct Person));
    new_person->first_name = malloc((1+strlen(f_name))*sizeof(char));
    new_person->last_name = malloc((1+strlen(l_name))*sizeof(char));

    strcpy(new_person->first_name, f_name);
    strcpy(new_person->last_name, l_name);

    return new_person;
}

void delete_person(struct Person* person)
{
    free(person->first_name);
    free(person->last_name);
    free(person);

    person->first_name = 0;
    person->last_name = 0;
    person = 0;

    if(person){
        printf("Person not deleted\n");
    }
    else{
        printf("Person deleted\n");
    }
}

int main()
{
    struct Person* person1 = create_person("John", "Doe");
    delete_person(person1);

    if(person1){
        printf("Person not deleted\n");
    }
    else{
        printf("Person deleted\n");
    }
}

Program output is:

Person deleted

Person not deleted

My question is: why doesn't person1 point to NULL? It seems like the memory is freed (because if I try to use it I get an error), but why is the pointer still pointing to something (apparently) valid? Why doesn't person = 0; get carried across the function call?

Jack

To parameter's value change be see outside your function (and not only locally, like you're seeing) you need to take a struct Person ** person (note **) instead of.

EDIT: note that you're accessing memory which you don't own anymore in:

person->first_name = 0;
person->last_name = 0;

Because you've already freed person

Try this:

void delete_person(struct Person ** person)
{
    struct Person *p = *person;

    free(p->first_name);
    free(p->last_name);
    free(*person);

    *person = 0;
}

And call using:

delete_person(&person1);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In function call, why doesn't nullptr match a pointer to a template object?

From Dev

Javascript doesn't work after call a function

From Dev

Why doesn't matlab find this function call?

From Dev

Why doesn't JS call a function on scroll?

From Dev

Assembly - Why this CALL function doesn't work?

From Dev

Why doesn't JS call a function on scroll?

From Dev

Why doesn't setTimeout wait to call a function?

From Dev

Call Get and Set Function inside Object Javascript

From Dev

Get object after outside function call

From Dev

Why doesn't this function get called?

From Dev

Why opening after deleting doesn't call the onupgradeneeded callback

From Dev

Jquery function doesn't work after Ajax call

From Dev

addClass() function doesn't work after ajax call

From Dev

Application state doesn't get updated after api call

From Dev

Why the mutable reference parameter of a closure doesn't outlive the function call?

From Dev

How to debug in Python: Why doesn't pdb descend into function call?

From Dev

Why the mutable reference parameter of a closure doesn't outlive the function call?

From Dev

Why input fields get disabled after ajax or javascript function call?

From Dev

Parameters set to null on call to function

From Dev

Call to a member function set() on null

From Dev

Parameters set to null on call to function

From Dev

Why can't I set an object to null in an extension method

From Dev

why can't we call a function on a const object?

From Dev

Why split function doesn't return a null at the first of this string?

From Dev

Why doesn't Haskell accept arguments after a function composition?

From Dev

Why doesn't Haskell accept arguments after a function composition?

From Dev

Why doesn't JavaScript require semicolons after function declarations?

From Dev

JavaScript object set and function call

From Dev

why doesn't 'this' of an arrow function change inside an nested object literal?

Related Related

  1. 1

    In function call, why doesn't nullptr match a pointer to a template object?

  2. 2

    Javascript doesn't work after call a function

  3. 3

    Why doesn't matlab find this function call?

  4. 4

    Why doesn't JS call a function on scroll?

  5. 5

    Assembly - Why this CALL function doesn't work?

  6. 6

    Why doesn't JS call a function on scroll?

  7. 7

    Why doesn't setTimeout wait to call a function?

  8. 8

    Call Get and Set Function inside Object Javascript

  9. 9

    Get object after outside function call

  10. 10

    Why doesn't this function get called?

  11. 11

    Why opening after deleting doesn't call the onupgradeneeded callback

  12. 12

    Jquery function doesn't work after Ajax call

  13. 13

    addClass() function doesn't work after ajax call

  14. 14

    Application state doesn't get updated after api call

  15. 15

    Why the mutable reference parameter of a closure doesn't outlive the function call?

  16. 16

    How to debug in Python: Why doesn't pdb descend into function call?

  17. 17

    Why the mutable reference parameter of a closure doesn't outlive the function call?

  18. 18

    Why input fields get disabled after ajax or javascript function call?

  19. 19

    Parameters set to null on call to function

  20. 20

    Call to a member function set() on null

  21. 21

    Parameters set to null on call to function

  22. 22

    Why can't I set an object to null in an extension method

  23. 23

    why can't we call a function on a const object?

  24. 24

    Why split function doesn't return a null at the first of this string?

  25. 25

    Why doesn't Haskell accept arguments after a function composition?

  26. 26

    Why doesn't Haskell accept arguments after a function composition?

  27. 27

    Why doesn't JavaScript require semicolons after function declarations?

  28. 28

    JavaScript object set and function call

  29. 29

    why doesn't 'this' of an arrow function change inside an nested object literal?

HotTag

Archive