1

Sample codes like this:

#include <iostream>

using namespace std;

int main()
{
    int* addr = (int*)malloc(100);
    cout << "Hello World" << endl; 
    printf("addr#1=%p\n",addr);
    free(addr);
    printf("addr#2=%p\n",addr);

   return 0;
}

I know the addr#1 and addr#2 would print the same address. But is there a way to identify address that it is free to use?

Somebody said it's not possible unless you reassign NULL or nullptr to addr to identify that. Ok, so if the pointer is pointed to an object, and that object is deleted, the result was the same?

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
naive231
  • 1,360
  • 1
  • 11
  • 28
  • 2
    Possible duplicate of [How to check if a pointer is freed already in C?](https://stackoverflow.com/questions/8300853/how-to-check-if-a-pointer-is-freed-already-in-c) – Retired Ninja Aug 11 '17 at 02:39
  • Ok, so if the pointer is pointed to an object, and that object is deleted, the result was the same? – naive231 Aug 11 '17 at 02:57
  • If this were C (as tagged), `printf("addr#2=%p\n",addr);` invokes *Undefined Behavior* attempting to access `addr` after `free (addr)`. – David C. Rankin Aug 11 '17 at 04:30

3 Answers3

0

you can wrap free and always take care about initialising the pointers.

void myfree(void **ptr)
{
  free(*ptr);
  *ptr = NULL;
}

so every free will assign the value NULL. You can check in program if the pointer is NULL. it is only some kind of workaround

0___________
  • 60,014
  • 4
  • 34
  • 74
0

I know the addr#1 and addr#2 would print the same address. But is any way can identified a address that it is free to use?

The ideal way to determine if a piece of memory allocated by malloc is to set the pointer pointing to that piece of memory to be NULL, including all other pointers that may potentially point to the original piece of memory.

Ok, so if the pointer is pointed to an object, and that object is deleted, the result was the same?

Just because of piece of memory was freed doesn't mean what was originally at that memory location will be erased instantly. You may call it and see what you had originally, some random stuff, or your program might crash; it's undefined behavior. When you say "delete," I assume you are implying freeing a previous allocated piece of memory.

Somebody said it's not possible unless you reassign NULL or nullptr to addr to identify that

There is an interesting answer by kennytm found here How to check if a pointer is freed already in C? via the usage of mcheck and mprobe where these function hook onto malloc and preform constancy checks on the heap. You could potently craft a series of checks to determine if a variable has been freed, more info here, but these functions are purely for debugging and NULLing passed-allocated memory is the safe-way to go.

Miket25
  • 1,895
  • 3
  • 15
  • 29
0

I know the addr#1 and addr#2 would print the same address. But is any way can identified a address that it is free to use?

Not easily or portably. This is specific to the behaviour of the C runtime library, so depends entirely on implementation details. Furthermore, this is undefined behaviour (use-after-free).

For example, you would need to use a heap checking routine such as mprobe with GCC to query the state at runtime.

Somebody said it's not possible unless you reassign NULL or nullptr to addr to identify that.

No, that only changes your perspective on the use or state of that particular memory. It says nothing about the state of the system according to the runtime library which actually owns and manages this info.

Ok, so if the pointer is pointed to an object, and that object is deleted, the result was the same?

Yes, whether a pointer to an object or a pointer to a POD (as in your example), the result would be the same.

Conventional wisdom says that if you follow certain rules and best practices to do with memory management, you should never to worry about this sort of problem.

This whole question is a great motivating example for the use of smart pointers! Then the whole problem goes away.

gavinb
  • 19,278
  • 3
  • 45
  • 60