简体   繁体   中英

If two pointers point to the same memory address, do you only need to use free(ptr) once or twice?

Say we have a struct...

struct node{
    int data;
    struct node *next;
};

struct node *new_node = malloc(sizeof(node));

struct node *temp_node = new_node;

If I use free...

free(temp_node);

Is new_node free as well (since the address no longer exists) or does new_node simply point to NULL (in which case I would need to free new_node as well)?

Cheers!

You don't free pointers but the memory block(s) that you allocated - whose address a pointer points to.

With malloc , it returns pointer to a chunk of memory allocated that looks like this:

             +--------+
             |        |
new_node --> | 0x1000 |
             |        |
             +--------+

If 0x1000 is the starting address of that block of memory, that's what new_node points to (ie, new_node == 0x1000 ).

When you assign new_node to temp_node , temp_node points to the same block of memory (ie, temp_node == 0x1000 ):

             +--------+
             |        |
new_node --> | 0x1000 | <-- temp_node
             |        |
             +--------+

But there's just one block of memory that you have allocated. So once you free it via either pointer, the other is automatically invalidated and you are no longer allowed access that block of via either pointer.

In the same way, you can assign it to any number of pointers but as soon as it's free'd via one pointer, it's done. That's why care is needed when copying pointers around (because if you free one, it may still be inadvertently used).

PS: Free'd pointer may or may not point to NULL afterwards - it's simply undefined behaviour to access free'd memory.

There are plenty of exceptions, but as a general rule, there should be exactly one call to free for every call to malloc .

When you assign one pointer variable to another, you are copying the pointer value -- you are not allocating more memory. So a pointer copy does not imply the need for a second call to free .

The way to think about is like: pointers point to a memory location. free() returns memory pointed to by a pointer back to the system. Thus if you have two pointers - once free is called the memory is returned to the system. You still have two pointers. And they still point to the same memory location. It's just now it is not your memory but system's:)

In short - free as many times as you malloc 'd.

Because you are freeing memory and I see just one malloc here, you should only free() once.

In general, for each malloc , there has to be one and only one free . Otherwise, you will get a double free error.

For one memory address, you only need to call free once.

Once you call free , that tells the operating system that the memory you allocated can be used again. You do not need to call free on it a second time (and shouldn't, because that's undefined behavior).

A few notes on the code

  • The declarations struct *next and struct *temp_node point to an unnamed struct that has no instances.
  • node is just a structure tag used to name the structure and cannot be used in an expression like this malloc(sizeof(node)) .

If this is what you originally meant:

struct node{
  int data;
  struct node *next;
};
struct node * new_node = malloc(sizeof(struct node));
struct node * temp_node = new_node;

Then the answers provided before this post are completely accurate.

#include<stdio.h>
#include<stdlib.h>

struct Node
{
    int data;
    struct Node * next;
}* first=NULL;

int main()
{
    first=(struct Node *)malloc(sizeof(struct Node));
    first->data=5;
    first->next=NULL;

    struct Node * t=first;

    printf("%d",t->data);

    free(first);

    printf(" %d ",t->data);
}

In this scenario though First is freed in main then too t prints the data. Which says that the allocated memory block can be further used for the different allocation but it's still there and can be used if a pointer is pointing to it.

But in case new allocation is made then that memory block may be allocated to the other variable. Hence after freeing the memory it's behaviour is undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM