Thread: Calloc() problem on ARM code/compiler

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    32

    Calloc() problem on ARM code/compiler

    Hello All,

    All I am trying here writting wrapper for calloc() (IAR ARM compiler ) so that I can place size of allocated memory in the first 4 bytes of the memory and use the save while freeing the memory. This is to monitor heap size!

    I'm facing a strange problem in Calloc() Wrapper which I tried. belwo is the code:

    Code:
    void *MyCalloc(unsigned long bytes)
    {
      bytes = ((bytes + 3) & ~0x3); //dont know why !?
      bytes = bytes+4;
      printf("bytes = %ld\n", bytes);
      unsigned int *p = (unsigned int *)calloc(1, bytes);
    
     printf("MyCallc @p == %p\n", p);
     if (p == NULL)
     {
       printf(" MyCalloc: unable to allocate memory\n");
       return NULL;
     }
     *((unsigned long*)p) = 0; 
     *((unsigned long*)p) = bytes; 
    return p+4; 
    }
    output:

    bytes = 264
    MyCallc @p == 70128470
    bytes = 16
    MyCallc @p == 70128580
    bytes = 16
    MyCallc @p == 00000000
    MyCalloc: unable to allocate memory


    I have lot of heap memory left , dont know why its returning NULL.

    below code is working fine:
    Code:
         void *ptr = (void *)calloc(1, (size_t)((bytes + 3) & ~0x3));
    Thanks in advance .

    -Ashok
    Last edited by ashok449; 02-15-2013 at 06:08 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Why does myCalloc have a different interface to calloc?
    This just makes it harder to drop-in replace one with the other.

    > unsigned int *p = (unsigned int *)calloc(1, bytes);
    > *((unsigned long*)p) = 0;
    Why the different casts?
    If int and long are different sizes, this code is all screwed up.

    Code:
    void *myCalloc(size_t nmemb, size_t size) {
        size_t new_size = (nmemb*size);
        size_t *result = calloc(1,new_size + sizeof(size_t));
        if ( result ) {
             *result++ = new_size;
        }
        return result;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    Hello Salem,

    Thanks for the response.

    Code:
    void *MyCalloc(size_t size) { 
        size_t new_size = ((size + 3) & ~0x3); 
        size_t *result = calloc(1,new_size + sizeof(size_t)); 
        if ( result ) { 
          printf("MyCalloc addr: %p\n", result);
             *result = (new_size + sizeof(size_t));
             result = result + sizeof(size_t);
        } 
    return result;
    }

    I have added extra sizeof(size_t) here to store the size of allocated memory. This is to monitor heap Size.

    but code giving problems sometimes. especially when its MyCalloc(12), for the first time its okay, but next time its returning NULL.

    I'm running this on ARM board here, looks like there is problem in memory alignment for ARM, can't figure it out

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > result = result + sizeof(size_t);
    This is wrong - read my version.

    And read up on pointer arithmetic.
    Pointer addition automatically takes into account the size of the thing being pointed to.
    So if you're pointing at a size_t, then to point to the next size_t, you add 1 to the pointer (or ++), not add sizeof(size_t).

    And post your main() wrapper you're using to test this code.
    For all I know, you're doing something stupid with the returned memory and there's nothing wrong with the wrapper.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with code::blocks compiler
    By iceddragon117 in forum C++ Programming
    Replies: 11
    Last Post: 04-23-2012, 12:06 PM
  2. malloc ,calloc code problem..
    By transgalactic2 in forum C Programming
    Replies: 37
    Last Post: 10-26-2008, 11:43 AM
  3. Calloc Problem
    By CrymsonSoul in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2006, 02:15 AM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM