0

I am implementing my own free using a static array of 1000 bytes for a problem.Two problems that can occur is if they passed in a parameter that isn't a pointer (char a; free((&a));) since free takes in a void pointer(free(void*p)).

Also, when the user doesn't malloc being used char * a; free(a);. How would you suggest I check for these edge cases. I am aware that pointers have no information other than what they point too so I'm stumped on how I can differentiate whether something is a int being pointed to or just a regular int.

benny
  • 11
  • `&a` is a pointer; it just isn't a pointer to part of the array you're using to simulate the heap. – Jonathan Leffler Feb 11 '18 at 23:16
  • See http://tharikasblogs.blogspot.de/p/how-to-write-your-own-malloc-and-free.html and https://danluu.com/malloc-tutorial/ – Pablo Feb 11 '18 at 23:39
  • 1
    Ultimately, you can't. If you could then the real `free` would be doing it, and as you know, it does not. (it usually crashes) – user253751 Feb 12 '18 at 01:03
  • @immibis: There are `free` implementations that do this, notably for debugging. – Eric Postpischil Feb 12 '18 at 02:33
  • 1
    If you really want to wade into this, generally the allocation schemes usually use a bit of metadata before the allocated block either providing a *magic number* to identify it as an allocated block or a pointer within the metadata pointing to the memory itself. A poorly written, but reasonable in content explanation of one approach is [A Malloc Tutorial](http://www.inf.udec.cl/~leo/Malloc_tutorial.pdf) (if you want a good exercise, port the example to work with both 32 & 64 bit environments) – David C. Rankin Feb 12 '18 at 02:45
  • Possible duplicate of [Testing pointers for validity (C/C++)](https://stackoverflow.com/questions/551069/testing-pointers-for-validity-c-c) – Bo Persson Feb 12 '18 at 03:45
  • It's pretty hard. That's why the standard `free()` isn't required to make these checks, so you don't need to worry if you don't manage to, either. ('A' for effort, though.) – Steve Summit Feb 12 '18 at 03:53
  • Related: https://stackoverflow.com/q/16709946/1606345 – David Ranieri Feb 12 '18 at 07:08
  • Change the title to avoid being label as a [duplicate](https://stackoverflow.com/questions/551069/testing-pointers-for-validity-c-c). Maybe "How to check if an address valid from a user allocation?" – chux - Reinstate Monica Feb 12 '18 at 16:15

1 Answers1

2

The way to do this is:

  • In your allocation routine, remember the addresses you give out.

  • In your free routine, check the address being freed. If it is not one you gave out, report an error. If it is, remove it from your list of outstanding addresses.

A lighter weight solution is simply to check every address passed to your free routine to see if it is inside your array. This will catch attempts to free something that is outside your array, but it will not catch calls that free an address previously freed or a slightly modified address that is still in your array.

Note that comparing arbitrary pointers with < or > is not actually defined by C. (It is only defined if it is already known they point within the same array. However, common C implementations permit this, and it is tolerable for a learning exercise.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312