2

Example:

void *a = malloc(4);

...

free(a);

...
*a = 5;

In c for example is there a way of catching this error using some kind of system call or something? (assuming this is set up such that it can only be checked at runtime)

if(isalloc (a)) maybe?

I'm wondering because c++ has figured it out somehow, except they have exceptions baked in. Can this be done in c? Perhaps a simple function exists, but I can't find it.

haelmic
  • 541
  • 4
  • 18
  • `because c++ has figured it out somehow` ? How? Undefined behavior is undefined behavior, there's nothing you can do. – KamilCuk Mar 21 '19 at 23:22
  • 1
    Is it relly that hard to keep track of allocated memory? – Fredrik Mar 21 '19 at 23:31
  • No it's not possible – Mitch Mar 21 '19 at 23:31
  • @KamilCuk https://stackoverflow.com/questions/7749066/how-to-catch-out-of-memory-exception-in-c shows how you'd do this in c++ and since this exists, I was wondering if it's possible to do this with just the address or if a tracker or something is required and if anybody had already done it. kind of thing. – haelmic Mar 21 '19 at 23:40
  • 1
    No you cannot, unless you replace the malloc() and free() (and all other *alloc()-functions) - functions and track which memory blocks have been allocated. Incidentally, there are tools that do precisely that - valgrind e.g. In C++, you can provide your own allocators , IMHO. But unfortunately, I am not that deep into C++... – Michael Beer Mar 21 '19 at 23:40
  • Set the pointer to NULL when you have freed what it was pointing to? – Dipstick Mar 21 '19 at 23:42
  • @haelmic The question you reference only deals with how to figure out whether an allocation was successful, doesn't it? You can easily check this in C by checking whether the return value of the *alloc()-functions is NULL. – Michael Beer Mar 21 '19 at 23:45
  • Even the exception mechanism in C++ simply says you whether it's an allocation failure or not. It won't necessarily allow you to query if a block pointed to by a certain pointer has already been allocated or not unless you have your own custom allocation strategy. – aep Mar 21 '19 at 23:49
  • @haelmic The question you referenced is about catching out of memory exception, not invalid access via an invalid pointer. You check for out of memory in C, by comparing the return of *alloc function to NULL. You can't check for access to a memory via invalid pointer. Accessing memory via invalid pointer is undefined behavior. – KamilCuk Mar 21 '19 at 23:50
  • @KamilCuk It appears I am mistaken https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why but some os's will kill the process if it attempts to access particular blocks, I'd have though that would have been a thing that could be caught. Perhaps I'm wrong. – haelmic Mar 21 '19 at 23:58
  • `will kill the process` - no. It may kill the process. It may format your hard drive. Nothing may happen. It is undefined what will happen. That's why it's called undefined behavior. You can't expect something to happen. – KamilCuk Mar 22 '19 at 00:07

1 Answers1

1

You can create such facility yourself.

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

char _unallocated[] = "";
#define free(p) free(p); p = _unallocated
#define isalloc(p) (p && p != _unallocated)

int main() {
  void* a = _unallocated;
  printf("%d\n", isalloc(a));

  a = malloc(4);
  printf("%d\n", isalloc(a));

  free(a);
  printf("%d\n", isalloc(a));
}
niry
  • 3,238
  • 22
  • 34