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

  • Home
  • SEARCH
  • 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 585387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:59:51+00:00 2026-05-13T14:59:51+00:00

I have a function which is called multiple times during the program’s execution. In

  • 0

I have a function which is called multiple times during the program’s execution. In said function, I have a dynamic character pointer which I resize numerous times.

My question is: do I need to free this pointer before the end of the function?

void functionName()
{
 char *variable = (char *) malloc(0);
    //variable is resized with realloc x number of times

 //should free be called here?
 return;
}

I should also note that I have tried to free the pointer, however, gdb gives me warnings when I do so.

  • 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-13T14:59:51+00:00Added an answer on May 13, 2026 at 2:59 pm

    Yes, you have to free it or you’ll leak the memory. Your code should look something like this:

    void function(void) 
    {
        char *variable = (char *)malloc(0);
    
        variable = realloc(variable, 10);
        // do something with new memory
    
        variable = realloc(variable, 20);
        // do something with more new memory
    
        free(variable);  // clean up
    }
    

    Calling malloc(0) is a little weird, I think.

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

Sidebar

Related Questions

so I have this function which gets called multiple times during my program. //global
I have a function, which is called sometimes with regular, sometimes dynamic arrays. If
I have function called buildRay which returns a 1x4 matrix. I call it multiple
hi i have function which is called by tinker listbox so i cannot return
I have a function which calculates distance from database records. This function is called
I am using jQuery(Latest release) and have a function called calculate() which gets called
I have a Function in CFC file, which will be called from .cfm file
I have a function: def create(sender, **kw): [...] Which should be called when the
I have a function called PreProcessSource, which allocates a boost::wave::context and does some preprocessing;
I have this code which is called at an onChange event of an function

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.