Checking if a pointer is allocated memory or not

Viewed 108776

Can we check whether a pointer passed to a function is allocated with memory or not in C?

I have wriiten my own function in C which accepts a character pointer - buf [pointer to a buffer] and size - buf_siz [buffer size]. Actually before calling this function user has to create a buffer and allocate it memory of buf_siz.

Since there is a chance that user might forget to do memory allocation and simply pass the pointer to my function I want to check this. So is there any way I can check in my function to see if the pointer passed is really allocated with buf_siz amount of memory .. ??

EDIT1: It seems there is no standard library to check it .. but is there any dirty hack to check it .. ??

EDIT2: I do know that my function will be used by a good C programmer ... But I want to know whether can we check or not .. if we can I would like to hear to it ..

Conclusion: So it is impossible to check if a particular pointer is allocated with memory or not within a function

19 Answers

Well, I don't know if somebody didn't put it here already or if it will be a possibility in your programme. I was struggling with similar thing in my university project.

I solved it quite simply - In initialization part of main() , after I declared LIST *ptr, I just put that ptr=NULL. Like this -

int main(int argc, char **argv) {

LIST *ptr;
ptr=NULL;

So when allocation fails or your pointer isn't allocated at all, it will be NULL. SO you can simply test it with if.

if (ptr==NULL) {
  "THE LIST DOESN'T EXIST" 
} else {
  "THE LIST MUST EXIST --> SO IT HAS BEEN ALLOCATED"
}

I don't know how your programme is written, but you surely understand what am I trying to point out. If it is possible to check like this your allocation and then pass your arguments to you function, you could have a simple solution.

Of course you must be careful to have your functions with allocating and creating the structure done well but where in C you don't have to be careful.

I'm not sure how fast msync is, but this is a linux only solution:

// Returns 1 if the ponter is mapped
int pointer_valid (void *p)
{
  size_t pg_size = sysconf (_SC_PAGESIZE);
  void *pg_start = (void *) ((((size_t)p) / pg_size) * pg_size);
  return msync (pg_start, pg_size, MS_ASYNC) == 0;
}

There is almost never "never" in computers. Cross platform is way over anticipated. After 25 years I have worked on hundreds of projects all anticipating cross platform and it never materialized.

Obviously, a variable on the stack, would point to an area on the stack, which is almost linear. Cross platform garbage collectors work, by marking the top or (bottom) of the stack, calling a little function to check if the stack grows upwards or downwards and then checking the stack pointer to know how big the stack is. This is your range. I don't know a machine that doesn't implement a stack this way (either growing up or down.)

You simply check if the address of our object or pointer sits between the top and bottom of the stack. This is how you would know if it is a stack variable.

Too simple. Hey, is it correct c++? No. Is correct important? In 25 years I have seen way more estimation of correct. Well, let's put it this way: If you are hacking, you aren't doing real programming, you are probably just regurigating something that's already been done.

How interesting is that?

Related