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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:33:11+00:00 2026-05-11T18:33:11+00:00

In C, which is the better practice when it comes to freeing memory returned

  • 0

In C, which is the better practice when it comes to freeing memory returned from functions:

  • Provide a “destructor” function that encapsulates the call to free().
  • Require users to free() the returned pointer themselves.

For example, to open and close a file we do:

FILE* f = fopen("blah", "w");
fclose(f);

Is this preferable to:

FILE* f = fopen("blah", "w");
fclose(f);
free(f);

Warning: Don’t call free() on a FILE pointer. I only use it a hypothetical implementation here.

And what about cases where local variables are pointed to the returned memory? Is free() harmful here? (or perhaps this should never be done)

FILE f = &fopen("blah", "w");
fclose(&f);
  • 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-11T18:33:12+00:00Added an answer on May 11, 2026 at 6:33 pm

    You should never free a file – fclose handles releasing the resources properly. In general, only free pointers that were allocated directly by malloc. Most other pointers will have their own resource-cleanup functions.

    That being said, as to your initial question:

    I find that providing a destructor function is usually better practice, for three reasons.

    1) There are many cases where free is inappropriate, and this might not be obvious to your end user. FILE* is a good case of that – you shouldn’t call free(f); above…

    2) If you’re using this in a DLL, depending on the runtimes, having the free functionality encapsulated can work around many, many subtle bugs from mixing runtimes, especially on the Windows platform. Trying to use a DLL compiled in VS2005 from VS2008 can cause problems if you happen to free memory in one platform’s code which was allocated in another. Having a “wrapper” function to handle the memory management works around that significant issue.

    3) Many of the C API functions work this way – such as FILE* using fopen/fclose. This will not be surprising to a user of your library.

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

Sidebar

Ask A Question

Stats

  • Questions 98k
  • Answers 98k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is a complex subject with lots of possible solutions… May 11, 2026 at 7:37 pm
  • Editorial Team
    Editorial Team added an answer The wording is actually a system message, not one from… May 11, 2026 at 7:37 pm
  • Editorial Team
    Editorial Team added an answer It is very difficult to test multi-threaded code, so you… May 11, 2026 at 7:37 pm

Related Questions

What do you consider best practice when it comes to error handling errors in
This is a long text. Please bear with me. Boiled down, the question is:
In the past I've worked with -Wall and other switches for gcc to eliminate
I'm working on some code that generates a lot of ignoring return value of
I'm writing a DSL in Ruby to control an Arduino project I'm working on;

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.