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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:03:53+00:00 2026-05-26T03:03:53+00:00

A function (Say fun()) allocates memory and returns the pointer to allocated memory. How

  • 0

A function (Say “fun()”) allocates memory and returns the pointer to allocated memory.
How should I make sure I that this memory is released. I can’t release it immediately in function “fun()” as it is returned to caller.
And what if fun() is part of library? Whose responsibility it is to free memory.
In case of fopen(), the memory is released by fclose(). But in my case, “fun()” is called repeatedly. So I can not wait till end to release the memory.

  • 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-26T03:03:54+00:00Added an answer on May 26, 2026 at 3:03 am

    The following is the answer for C, posted before the OP confessed to using C++. In that language, use RAII and smart pointers, as recommended by others.

    If a function returns allocated memory, then the caller is responsible for deallocation and this must be stated in the function’s documentation.

    If more cleanup is needed then is offered by free, or such cleanup may to be needed in future versions of the library, then you should offer a cleanup function (like stdio does with fclose) that does the deallocation. If you can’t predict whether extra cleanup may be necessary in the future, then it’s a good idea to assume it will be at some point. Wrapping free is cheap.

    Think of it as a form of symmetry: if the client gets a resource (object) from the library, then it is eventually responsible for handing it back to the library for disposal:

    void use_the_foo_library()
    {
        Foo *f = make_foo();
        if (f == NULL)
            ERROR();
    
        foo_do_bar(f);
        foo_do_baz(f);
    
        foo_destroy(f);
    }
    

    where in foolib 1.0, foo_destroy is just

    void foo_destroy(Foo *p)
    {
        free(p);
    }
    

    but in version 2.0, it may have grown to

    void foo_destroy(Foo *p)
    {
        fclose(p->logfile);
        free(p);
    }
    

    etc. This style is consistent with the opaque pointer design pattern. It also gives you the freedom of replacing malloc and free at any point with a special purpose memory allocator, such as a pool allocator, without having to change any client code.

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

Sidebar

Related Questions

I have a class that has a function, lets say class.php: class fun {
Say a function takes a void pointer as an argument, like so: int func(void
Say I have a function func(i) that creates an object for an integer i,
Say I have a recursive function that I want to know how many times
If I have a function (say messUp that does not need to access any
Say somewhere in my Vim buffer that I have the following: function foo() {
Hallo all. I got a javascript object with some propeties let's say function Animal()
How do convert effort estimate say in function points to a specific web framework
I am trying to model a transfer function block for say 1/(s+1). What is
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment

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.