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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:29:20+00:00 2026-05-25T19:29:20+00:00

Can I do this in C? Valgrind complains that realloc produces an invalid free?

  • 0

Can I do this in C? Valgrind complains that realloc produces an invalid free?

int main(){
    void* mem;
    // Do stuff
    recurfunc(123, mem);
    // Do stuff
    if(mem)
        free(mem);
    return 0;
}

void recurfunc(int x, void* mem){
     .....
     // Do stuff
     mem = realloc(mem, newsize);
     // Do stuff
     recurfunc(x, mem);
     .....
}
  • 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-05-25T19:29:20+00:00Added an answer on May 25, 2026 at 7:29 pm

    Yes, it does indeed complain, because the void * you’re passing in as mem is a copy of the pointer. Changes to the pointer within the function will not be reflected back to main.

    If you want to change mem in the function and have it reflected back, you need to pass a pointer to a pointer.

    The following code illustrates this:

    #include <stdio.h>
    #include <stdlib.h>
    
    static void fn (void *p1, void **p2) {
        p1 = realloc(p1, 200);
        *p2 = realloc (*p2, 200);
        printf ("    In call: %p %p\n", p1, *p2);
    }
    
    int main (void) {
        void *x1 = malloc (100);
        void *x2 = malloc (100);
        printf ("Before call: %p %p\n", x1, x2);
        fn (x1, &x2);
        printf (" After call: %p %p\n", x1, x2);
        return 0;
    }
    

    which outputs:

    Before call: 0x896f008 0x896f070
        In call: 0x896f0d8 0x896f1a8
     After call: 0x896f008 0x896f1a8
                        ^
                        +--- Note this bit.
    

    and you can see that the first value is retained in main despite it being changed within the function.

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

Sidebar

Related Questions

Can anyone explain me why on that c++ simple code valgrind returns this .
Can this be simplified to a one liner? Feel free to completely rewrite it
How can this test fail? [TestMethod] public void Get_Code() { var expected = new
How can this code compile? The code below in the operator int CAN access
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char*
This answer to this question says that there's no way to attach Valgrind to
I get this Valgrind output (that's the only error I get): ==20627== Conditional jump
Can this MS-Access query be improved? i.e. the SELECT TOP 1 col FROM Table2
Can this be done quickly in jQuery before I start looping through them? Or
Can this be done? It seems like this should be possible. In general, I

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.