Home:ALL Converter>How to know pointer is pointing to dynamically or static allocated memory

How to know pointer is pointing to dynamically or static allocated memory

Ask Time:2015-08-20T19:04:50         Author:atulya

Json Formatter

Is there any way to know if pointer is pointing to dynamically allocated memory or static allocated memory?

Array are passed as pointers to functions

void func (int* p)
{
  if( p )
  {
    cout << p[0] << p[1] ;
   ...
   // func has a responsibility to deallocate what p is pointing
  }
}

int main()
{
  int a[] = {10, 20, 30, 50};
  func(a);
  ...
  return 0;
}

If ownership for deallocation is transferred to func. How func p would know whether 'p' is pointing to dynamically allocated memory or static allocated memory ?

Author:atulya,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32116556/how-to-know-pointer-is-pointing-to-dynamically-or-static-allocated-memory
Lectem :

First things first, it is the caller's responsibility to use the function with suitable arguments, and the coder's to make it explicit that this function would try do deallocate the pointer passed as argument.\n\nSecondly, this is not a good practice to have some algorithm function managing memory. The main reason is that you might want to use it on a subset of an input. For example if you compute the sum of an array, you might want to be able to compute the sum of the subarray starting at index 3.\n\nIt is the caller's job to manage the memory, not yours. Just don't put any deallocation in a function that does something else.\n\nAnyway, you can detect if the address is on the stack or not, see 1\nHowever please note that not being on the stack doesn't mean that it was allocated on the heap, it could be pointing to some static data such as a string literal.",
2015-08-20T11:17:42
Bathsheba :

You can't tell.\n\nYou should release memory in the \"same sort of place\" that you allocate it. Broadly this means that if the caller to a function allocates memory then that caller should release it. Similarly, if you provide a function to allocate memory, then provide another one to release it.\n\nSome folk like to allocate memory in a function and make it the caller's responsibility to release it. But that can cause problems especially if the function is compiled separately to the caller; such as in a dll or shared object.\n\nWhat you are proposing: deleting memory in a function that was allocated in the caller is the worst of all worlds. Don't do it.",
2015-08-20T11:10:37
David Haim :

not only that you can't really know it, you can't even tell is a pointer is valid one. \n\nint* x = 0xDEADBEEF;\n\nclearly, most of the chances the pointer doesn't even point to a valid memory address. \n\nbut not all is lost. you can do all kinds of tricks to achieve this goal at least partialy.\nsome of them include:\n\n\ncreate a memory pool and force anything dynamic to be allocated from there. then , with simple pointer arithmetics you can find out if the memory address is there\noverload global new and delete. unless some of your classes overload new and delete operators, you can catch most of the dynamic allocations. this will not work if you are using malloc.\nif you are on windows , and you're a winapi expert you might try hooking HeapAlloc and thus track some of memory addresses. \n",
2015-08-20T11:15:42
HelloWorld :

You can't know this. This is by definition of your function. Prefer using smart pointers or you have to make pretty clear in your functions documentation that it takes over the ownership of your passed object or array.",
2015-08-20T11:07:31
fuz :

There is no portable way to find out if a pointer is pointing to dyanmically allocated memory or to statically allocated memory. It might be possible to deduct this information by carefully checking if the pointer points to one of the data or bss segments of any of the binary and shared libraries you have loaded in your program. Even then, the line between statically and dynamically allocated memory is blurry: Is memory that is part of a shared library loaded later on dynamic or static in your opinion?",
2015-08-20T11:10:16
yy