Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9304275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:40:17+00:00 2026-06-18T23:40:17+00:00

Using below code I’m trying to write a wrapper for calloc() so that I

  • 0

Using below code I’m trying to write a wrapper for calloc() so that I can keep trace of allocated heap memory by storing the size in 1st 2/4bytes of the allocated memory. When I tested this alone seems its okay. But when I replace this as my system calloc() then its creating problems.. means some times its returning NULL, even though lot of heap available.

I’m running this on ARM board using IAR compiler:

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;
}

Any idea why this is causing problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:40:18+00:00Added an answer on June 18, 2026 at 11:40 pm

    There are several problems with your code:

    1. When writing heap functions, or heap wrappers, just don’t use pointer arithmetic for storing heap headers. Use structures. That’s what they’re for.

    2. You’ve introduced a number of integer overflow bugs into your code. If someone asks your calloc() for 0xfffffffe bytes, you’ll return them 4 bytes. If they write more than 4 bytes to that allocation, there’ll be a heap overflow.

    3. Your calloc() doesn’t have the same signature as calloc(). Depending on how you’re swapping out calloc(), this is likely to become a problem.

    4. calloc() and malloc() naturally return aligned pointers. In x86 they need to return a pointer to the application that is aligned to at least 8 bytes, and in x64 they need to return a pointer that is at least 16-byte aligned.

      In your code, you’re using the real calloc to do the "heavy lifting" (i.e. as the "raw allocator"), which is fine and that will return an 8 or 16-byte aligned pointer, but when you return a pointer 4-bytes into that structure, your calloc ends up returning a non-aligned pointer to the caller, which is likely to cause problems to people calling your calloc replacement.

    Try some code a bit more like this:

     typedef struct 
     {
        size_t cbSize;
     } MyAwesomeHeapHeader;
     // TODO: ensure that MyAwesomeHeapHeader is 8-byte aligned on x86 and 16-byte aligned on x64 (or just 16-byte aligned on both).
    
     void* MyAwesomeMalloc(size_t cbSize)
     {
        MyAwesomeHeapHeader* header;
        void* internalAllocatorPtr;
        size_t cbAlloc;
        // TODO: Maybe I want a heap footer as well?
    
        // TODO: I should really check the following for an integer overflow:
        cbAlloc = sizeof(MyAwesomeHeapHeader) + cbSize; 
        internalAllocatorPtr = MyAwesomeRawAllocator(cbAlloc); // at the moment you're using the real calloc for this, but you could use malloc or write your own raw allocator
        // TODO: Check for null
    
        header = (MyAwesomeHeapHeader*)internalAllocatorPtr;
        header->heapSize = cbSize;
        // TODO: other fields here.
    
        return (uint8_t*)(internalAllocatorPtr) + sizeof(MyAwesomeHeapHeader);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to generate photo gallery from a folder. How can
I can get the list of country names using below code, (copied from somewhere
Using below code I am trying to get result which should be like success
I'm using below code to play a midi file in ios, but how can
Using below code, I am trying to show a uialertview, it is being shown
how can I pass NSString value to webwhatsnew subview using below code . Thanks
I am using below code to upload excel and INSERT in mysql in php
I am using below code to get profile image of friends using Resfb. I
I'm using below code to check some form fields and render datatable table on
I am using below code to show jquery dialog, its working perfectly, Sub OpenDialog(ByVal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.