Thread: Basic dynamic memory allocation problem

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    Basic dynamic memory allocation problem

    I'm trying to handle the basics of memory allocation but I've already got a problem.

    Here's my code, all I want it to do is return the correct results.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    
    	int *pointer = NULL;
    	
    	printf("The size of the pointer is: %d \n", sizeof(pointer));
    	
    	pointer = (int *) realloc(pointer, 100*sizeof(int));
    	
    	printf("The size of the pointer is now: %d \n", sizeof(pointer));
    	
    	getchar();
    	
    }
    The output is that the pointer is pointing to 4 bytes of memory both times. I don't understand why this is and it's making my coursework impossible right now.

    Can someone give me a tip please?

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    A few things, sizeof(pointer) give you the size of the variable only. On your platform, 4 bytes.

    You shouldn't call realloc without first calling calloc or malloc, on some platforms it is possible to call malloc with size 0, you can then realoc that 0 space. Not that useful, and a bit dangerous, but it's the closest to what you are now doing. Initializing a pointer to NULL, just means that it points nowhere.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look at your code... what are you asking printf() to display?

    Pointer is a pointer... in 32 bit compilers pointers are 4 bytes. Thus sizeof(Pointer) will return 4.

    If you want to know the size of your reallocated memory you do that separately...

    Code:
    int bs = 0;      // block size
    int *ptr = NULL;   // memory pointer
    
    // calc size
    bs = 100 * sizeof(int);
    
    // call realloc
    ptr = realloc(ptr,bs);
    
    If (!ptr)  // 0 = error
      { printf("Allocation failed");
         bs = 0; }
    else
      printf("%d bytes allocated at %X",bs,ptr);
    It is up to you to keep track of how much memory you allocate. C does not do it for you.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Size of pointer does not change. That's correct. It's an address which points to a chunk of memory. That memory can hold 100 integers... or whatever amount.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You shouldn't call realloc without first calling calloc or malloc, on some platforms it is possible to call malloc with size 0, you can then realoc that 0 space. Not that useful, and a bit dangerous, but it's the closest to what you are now doing. Initializing a pointer to NULL, just means that it points nowhere.
    from man 3 malloc
    calloc() allocates memory for an array of nmemb elements of size bytes
    each and returns a pointer to the allocated memory. The memory is set
    to zero. If nmemb or size is 0, then calloc() returns either NULL, or
    a unique pointer value that can later be successfully passed to free().

    malloc() allocates size bytes and returns a pointer to the allocated
    memory. The memory is not cleared. If size is 0, then malloc()
    returns either NULL, or a unique pointer value that can later be suc‐
    cessfully passed to free().

    realloc() changes the size of the memory block pointed to by ptr to
    size bytes. The contents will be unchanged to the minimum of the old
    and new sizes; newly allocated memory will be uninitialized. If ptr is
    NULL, then the call is equivalent to malloc(size), for all values of
    size; if size is equal to zero, and ptr is not NULL, then the call is
    equivalent to free(ptr). Unless ptr is NULL, it must have been
    returned by an earlier call to malloc(), calloc() or realloc(). If the
    area pointed to was moved, a free(ptr) is done.
    It's defined by C standard.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So is gets(), google: "Zero-sized heap allocations vulnerability"

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The point is it's defined by standard. not platform specific.
    Edit: Even though it's a good idea not to rely on it :=|
    Last edited by Bayint Naung; 03-22-2011 at 08:26 AM.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, it is. A call to malloc(0) can return either a NULL pointer or a valid pointer. Both are accepted as you can see in the man page section above. Which of the two it is, is platform specific.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I don't know why C standard committee does not just decide to return NULL!

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's conflicting. A returned NULL normally indicates a failure. If you ask for malloc(0), it's allocating 0 bytes which should succeed always.

    I don't see malloc(0) being the same as free() although the documentation treats is as such.
    In my opinion, asking for 0 bytes, although useless, should still allocate whatever structures / pointers / linked lists required internally by the memory pool manager. Possibly some 32-byte header if I recall from some implementations. That's way different from freeing memory. So a malloc(0) should succeed and report as much by returning some non-NULL pointer. But as a programmer you should not store any data through that pointer.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Where does malloc(0) even come into play with the original code? He's effectively doing realloc(NULL, 100 * sizeof(int)), which is perfectly valid. There's no need to change it.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    ... because for realloc()... "If ptr is NULL, then the call is equivalent to malloc(size), ..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation problem
    By Demonoid in forum C Programming
    Replies: 1
    Last Post: 03-06-2011, 02:56 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. Dynamic Memory Allocation
    By fr0ggs in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 03:34 AM
  5. memory allocation newbie problem
    By larry in forum C++ Programming
    Replies: 11
    Last Post: 10-08-2001, 08:58 AM