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 9063789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:05:36+00:00 2026-06-16T16:05:36+00:00

I want to use realloc to increase memory size while keeping the pointer unchanged

  • 0

I want to use realloc to increase memory size while keeping the pointer unchanged (because the callers uses it). realloc does not always do that; sometimes it returns a different pointer and frees the old one. I would like to “try” to realloc memory and if it is not possible, fallback to a different method using the original pointer – but realloc has already destroyed that!

Is there a way to try to increase malloc’ed memory without destroying (as realloc does) the old pointer if it is not possible?

E.g.

void *pold;
void *pnew = realloc(pold, newsize);
if (pnew != pold)
{
     free(pnew);
     DoDifferently(pold); // but pold is freed already
}

P.S. I don’t care about portability (linux only, thus the tag).

  • 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-16T16:05:37+00:00Added an answer on June 16, 2026 at 4:05 pm

    You should take a look at the source code of realloc() from the libc you are using. From there, it should be easy to see the path followed when it can increase the size in place, and the else case where a new pointer will be returned instead. Then, use that to code your own tryrealloc() function.

    For example, this is the realloc() source code from uclibc : http://cristi.indefero.net/p/uClibc-cristi/source/tree/nptl/libc/stdlib/malloc/realloc.c

    24  void *
    25  realloc (void *mem, size_t new_size)
    26  {
    ...
    57    if (new_size > size)
    58    /* Grow the block.  */
    59    {
    60        size_t extra = new_size - size;
    61  
    62        __heap_lock (&__malloc_heap_lock);
    63        extra = __heap_alloc_at (&__malloc_heap, base_mem + size, extra);
    64        __heap_unlock (&__malloc_heap_lock);
    65  
    66        if (extra)
    67          /* Record the changed size.  */
    68          MALLOC_SET_SIZE (base_mem, size + extra);
    69        else
    70          /* Our attempts to extend MEM in place failed, just
    71             allocate-and-copy.  */
    72        {
    73          void *new_mem = malloc (new_size - MALLOC_HEADER_SIZE);
    74          if (new_mem)
    75            {
    76              memcpy (new_mem, mem, size - MALLOC_HEADER_SIZE);
    77              free (mem);
    78            }
    79          mem = new_mem;
    80        }
    81      }
    ...
    

    I have removed some parts for clarity. But you can see that at line 66, it check if it can simply increase the memory for the current pointer. This is the part you want to keep. The else case starting at line 69 is to handle the case where the old memory will be freed and a new pointer will be returned. This is the part you want to kick out and handle it differently. From what you are saying, I guess you will only want to remove line 77, where it does the free.

    If you go this way, remember that you will have to manually either free the old pointer or the new one, as both will now be valid (and you don’t want a memory leak).

    Also, this is for uclibc. If you are already using a different libc, you should base your new tryrealloc() function on the realloc() function of that libc.

    EDIT: You must be careful if you use this approach. You will be basing your solution on the internals of the memory manager, so things can change and be different between the various libc implementations, but also between different versions of the same libc. So do that with the appropriate care and warning in mind.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some time ago a friend of mine told me not to use realloc because
I want use BYTE_ORDER macro in my Xcode project but i can't because i
I want to use the git's malloc and realloc wrappers in my code for
Want use special charecters ex : (£, ¥) which not there in keyboard to
The average man does not want to be free. He simply wants to be
I use fstat to get the file size. I want to use this size
I want use task_for_pid() and attach to another process and then change its memory
I want use this 1 for using Bar code or QR code scanner. I
I want use javascript setInterval function to achieve a box rotate animate effect, I
I want use a single php file to handle all of my voting requests.

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.