Thread: How to tell if a pointer has had memory allocated or not

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29

    How to tell if a pointer has had memory allocated or not

    Hi all,

    This is probably quite a newbie question.

    I have a function that takes in a pointer as a paramter. How can I tell if the pointer has previously called malloc()?

    In addition to this, if all I want to do is to change the value of say a char*, could I get around this problem by just calling realloc() (I think that was the name of the function).

    Anyway, I guess my original question still stands. How can you tell programatically if the pointer you are given as a parameter to a function has already had malloc called upon it.

    I don't think an if statement like the following quite cuts it...

    Code:
    if (pointer)
    {
       printf("The pointer has had memory allocated.\n");
    }
    Anyway, what advice to you have?

    Thanks

    Eddie

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This If-statement is as close you can get to what you want.
    Fact is you cannot tell if a pointer is valid or not. The only thing you can do is initialize every pointer to 0 and use your if-statement. But this way you still cannot tell if the memory was already released.
    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > How can you tell programatically if the pointer you are given as a parameter to a function has already had malloc called upon it.
    You can't.
    There is no way to tell the difference between these two calls.
    Code:
    void foo ( char *buff ) {
      /* no idea whether buff is an array or a malloc */
      /* nor is there any idea of how many chars there are, simply from looking at buff */
    }
    
    int main ( ) {
      char array[10];
      char *p = malloc( 10 );
      foo ( array );
      foo ( p );
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    hmmm, I was hoping that it would be able to tell whether a pointer has been 'initialised' or not -- oh well.

    Thank you for your responses.

    Eddie

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    As said above, it's impossible to tell.

    However, the canonical practice I believe is to get into the habit of initialising all pointers to NULL when declared (and hopefully your compiler will warn if you use the pointer before initialising it), and then have your functions check if the pointer is still not NULL when passed. If it's not NULL, assume it's been malloc'd or pointing to some other variable. It's not perfect, but probably better than nothing.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    Thanks cwr.

    Eddie

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    Hi all.

    Incidentally, my initial theory of using...

    Code:
    if (pointer)
    {
       printf("The pointer has had memory allocated.\n");
    }
    isn't reliable at all.

    For instance, the following code, when run, claimed that the pointer was initialised.... basically, it's not true... obviously it just pointed to a memory address (which is non zero).

    Code:
    	char* char_ptr;
    
    	if ( char_ptr )
    	{
    		printf("The char pointer was initialised.\n");
    	}
    	else
    	{
    		printf("The char pointer was not initialised.\n");
    	}
    I guess we should follow the advice of cwr.

    Thanks guys

    Eddie

  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by emeyer
    For instance, the following code, when run, claimed that the pointer was initialised.... basically, it's not true... obviously it just pointed to a memory address (which is non zero).

    Code:
    	char* char_ptr;
    
    	if ( char_ptr )
    	{
    		printf("The char pointer was initialised.\n");
    	}
    	else
    	{
    		printf("The char pointer was not initialised.\n");
    	}
    The above is true, but if you invoke the right magic with gcc (no, this obviously won't work in all circumstances):
    Code:
    gcc -o ptr ptr.c -Wuninitialized -O
    ptr.c: In function `main':
    ptr.c:5: warning: `char_ptr' might be used uninitialized in this function

  9. #9
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    Yes, I understand.

    Thanks cwr.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. memory question (pointer related)
    By cjschw in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2004, 01:09 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM