Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a pointer is allocated memory or not

Tags:

c

pointers

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

like image 602
codingfreak Avatar asked Oct 16 '09 05:10

codingfreak


People also ask

Is there a way to find out how much memory a pointer was allocated?

It is impossible to know how much memory was allocated by just the pointer. doing sizeof (p) will get the size of the pointer variable p which it takes at compile time, and which is the size of the pointer. That is, the memory the pointer variable takes to store the pointer variable p .

Is memory allocated for referenced pointers?

Memory is allocated when a pointer is defined. A reference however, is a name alias & hence no memory is allocated for it( Is it correct? ). 2. Reference is bound to be initialized at the time of definition because, a reference is implemented with a constant pointer & hence cannot be made to point to the other object.

Does pointer have memory?

Yes, a declared pointer has its own location in memory.


2 Answers

You cannot check, except some implementation specific hacks.

Pointers have no information with them other than where they point. The best you can do is say "I know how this particular compiler version allocates memory, so I'll dereference memory, move the pointer back 4 bytes, check the size, makes sure it matches..." and so on. You cannot do it in a standard fashion, since memory allocation is implementation defined. Not to mention they might have not dynamically allocated it at all.

You just have to assume your client knows how to program in C. The only un-solution I can think of would be to allocate the memory yourself and return it, but that's hardly a small change. (It's a larger design change.)

like image 104
GManNickG Avatar answered Oct 11 '22 11:10

GManNickG


The below code is what I have used once to check if some pointer tries to access illegal memory. The mechanism is to induce a SIGSEGV. The SEGV signal was redirected to a private function earlier, which uses longjmp to get back to the program. It is kind of a hack but it works.

The code can be improved (use 'sigaction' instead of 'signal' etc), but it is just to give an idea. Also it is portable to other Unix versions, for Windows I am not sure. Note that the SIGSEGV signal should not be used somewhere else in your program.

#include <stdio.h> #include <stdlib.h> #include <setjmp.h> #include <signal.h>  jmp_buf jump;  void segv (int sig) {   longjmp (jump, 1);  }  int memcheck (void *x)  {   volatile char c;   int illegal = 0;    signal (SIGSEGV, segv);    if (!setjmp (jump))     c = *(char *) (x);   else     illegal = 1;    signal (SIGSEGV, SIG_DFL);    return (illegal); }  int main (int argc, char *argv[]) {   int *i, *j;     i = malloc (1);    if (memcheck (i))     printf ("i points to illegal memory\n");   if (memcheck (j))     printf ("j points to illegal memory\n");    free (i);    return (0); } 
like image 26
Peter Avatar answered Oct 11 '22 11:10

Peter