0

Is the code down below possible?

int *a_p = malloc(sizeof(int));
int *b_p = a_p;
free(b_p);         //Free a_b by using b_p
a_p = b_p = NULL;

I'm very confused because two pointers point same memory...

If that code is impossible, could you teach me why, please?

5
  • 2
    Yes, it is possible. Commented Dec 6, 2020 at 7:29
  • Thank you so so so much! Appreciate you! Commented Dec 6, 2020 at 7:36
  • 1
    A few links that provide basic discussions of pointers may help. Difference between char pp and (char) p? and Pointer to pointer of structs indexing out of bounds(?)... (ignore the titles, the answers discuss pointer basics) Commented Dec 6, 2020 at 7:47
  • Some function for exemple with a void ** parameter can allocate memory you must free. Commented Dec 6, 2020 at 8:32
  • Yes, it is possible, both pointers point to the same allocated buffer, so any of them (but not both) can be used to free() the allocated buffer. Commented Dec 7, 2020 at 17:52

2 Answers 2

3

What you have is perfectly fine.


  1. You create a_p, allocate a memory block, and assign the block's address to a_p.
    After int *a_p = malloc(sizeof(int));:

    int *a_p                  int @ 0x1234
    +------------+           +------------+
    | 0x1234     |           | ???        |
    +------------+           +------------+
    
  2. You create b_p with the same value as a_p.
    After int *b_p = a_p;:

    int *a_p                  int @ 0x1234
    +------------+           +------------+
    | 0x1234     |           | ???        |
    +------------+           +------------+
    
    int *b_p
    +------------+
    | 0x1234     |
    +------------+
    

    Notably, this didn't allocate any memory other than b_p itself.

  3. You free the memory block you previously allocated.
    After free(b_p);:

    int *a_p
    +------------+
    | 0x1234     |
    +------------+
    
    int *b_p
    +------------+
    | 0x1234     |
    +------------+
    
  4. You overwrite the addresses.
    After a_p = b_p = NULL;:

    int *a_p
    +------------+
    | NULL       |
    +------------+
    
    int *b_p
    +------------+
    | NULL       |
    +------------+
    

The two remaining blocks (a_p and b_p) have automatic storage duration, so they will be automatically freed when the function returns.

Sign up to request clarification or add additional context in comments.

Comments

1

Pointers are values. They can be copied via assignment.

In this example, b_p is set to the value of a_p and then free(b_p); is called.

At that moment, the value in b_p is one in a_p, which is a pointer returned from malloc() and not yet freed. Therefore, this is valid.

Another possibility is that malloc() failed and NULL is returned, but free(NULL); is valid and defined to do nothing, so the code is valid also in this case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.