Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if something was malloced

Tags:

c

malloc

Given a pointer to some variable.. is there a way to check whether it was statically or dynamically allocated??

like image 778
Aditya Mukherji Avatar asked Nov 09 '08 22:11

Aditya Mukherji


4 Answers

Quoting from your comment:

im making a method that will basically get rid of a struct. it has a data member which is a pointer to something that may or may not be malloced.. depending on which one, i would like to free it

The correct way is to add another member to the struct: a pointer to a deallocation function.

It is not just static versus dynamic allocation. There are several possible allocators, of which malloc() is just one.

On Unix-like systems, it could be:

  • A static variable
  • On the stack
  • On the stack but dynamically allocated (i.e. alloca())
  • On the heap, allocated with malloc()
  • On the heap, allocated with new
  • On the heap, in the middle of an array allocated with new[]
  • On the heap, within a struct allocated with malloc()
  • On the heap, within a base class of an object allocated with new
  • Allocated with mmap
  • Allocated with a custom allocator
  • Many more options, including several combinations and variations of the above

On Windows, you also have several runtimes, LocalAlloc, GlobalAlloc, HeapAlloc (with several heaps which you can create easily), and so on.

You must always release memory with the correct release function for the allocator you used. So, either the part of the program responsible for allocating the memory should also free the memory, or you must pass the correct release function (or a wrapper around it) to the code which will free the memory.

You can also avoid the whole issue by either requiring the pointer to always be allocated with a specific allocator or by providing the allocator yourself (in the form of a function to allocate the memory and possibly a function to release it). If you provide the allocator yourself, you can even use tricks (like tagged pointers) to allow one to also use static allocation (but I will not go into the details of this approach here).

Raymond Chen has a blog post about it (Windows-centric, but the concepts are the same everywhere): Allocating and freeing memory across module boundaries

like image 110
CesarB Avatar answered Sep 23 '22 20:09

CesarB


The ACE library does this all over the place. You may be able to check how they do it. In general you probably shouldn't need to do this in the first place though...

like image 26
Greg Rogers Avatar answered Sep 21 '22 20:09

Greg Rogers


Since the heap, the stack, and the static data area generally occupy different ranges of memory, it is possible with intimate knowledge of the process memory map, to look at the address and determine which allocation area it is in. This technique is both architecture and compiler specific, so it makes porting your code more difficult.

like image 23
Greg Hewgill Avatar answered Sep 21 '22 20:09

Greg Hewgill


Most libc malloc implementations work by storing a header before each returned memory block which has fields (to be used by the free() call) which has information about the size of the block, as well as a 'magic' value. This magic value is to protect against the user accidently deleting a pointer which wasn't alloc'd (or freeing a block which was overwritten by the user). It's very system specific so you'd have to look at the implementation of your libc library to see exactly what magic value was there.

Once you know that, you move the given pointer back to point at header and then check it for the magic value.

like image 29
Denis Hennessy Avatar answered Sep 20 '22 20:09

Denis Hennessy