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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:11:40+00:00 2026-06-10T07:11:40+00:00

This a short snippet of code, with two calls to exit(3) in case of

  • 0

This a short snippet of code, with two calls to exit(3) in case of failure.
Do these calls deallocate memory allocated by malloc? Google search once says it does, and even more times, it doesn’t…

Should I add free()?

Also: which is better if (!word)(it would also work for eg. word == 0 which is different from word == NULL, so I guess it is wrong) or if (word == NULL) ?

char *word = NULL, *temp = NULL;
    word = (char *)malloc(sizeof(char) * size);

    if (!word) {            /* or maybe rather it should be (word == NULL)  */
        perror("malloc fail");
        if (fclose(fp)) {
            perror("fclose fail");
            exit(3);                            /* exit without free ? */
        }
        exit(3);                                /* exit without free ? */
    }

Thanks in advance!

  • 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-10T07:11:41+00:00Added an answer on June 10, 2026 at 7:11 am

    Yes, all memory is returned. BTW, what would you want to do with leftover memory after the exit anyway?
    Or are you worrying about a memory leak in exit()? If the memory weren’t reclaimed, it would leak a bit more with each exiting process, which is something no credible OS could afford. So, except for a buggy OS, stop worrying about memory and use exit() wherever you need it.

    To answer the questions in the comments of your code, whether to free, I’d say it’s proper software engineering to write a corresponding free with every malloc. If that appears hard, it is pointing to a structural problem in your code. The benefit of freeing all memory before exiting is that you can use powerful tools like valgrind to check for memory leaks in the rest of your code without false positives from the malloc you’ve shown us.

    Note that after a failed malloc there is no point in attempting to free the result–it’s a null pointer anyway.

    And third, I prefer if (pointer == NULL) over if (!pointer) but this is totally subjective and I can read and understand both 🙂

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

Sidebar

Related Questions

I have a short snippet of C# code like this: HtmlGenericControl titleH3 = new
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char*
To help me better understand lambda I wrote this short snippet that rotates and
in this short code: class X { private: class Y { public: typedef void
I've written this short piece of php code that requires 2 variables name and
In short: For this code: Encoding.ASCII.GetBytes(‚) I want the output to be 130, but
Consider the following short code snippet. namespace B { public class Foo { public
I have a short snippet of code to produce a depth-first search of an
Consider this code snippet: public static void main(String[] args) { int z1 = 0;
I have this short and simple code for sending an email notification to the

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.