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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:49:12+00:00 2026-05-27T10:49:12+00:00

While I rarely use C anymore, I was thinking about the rule I was

  • 0

While I rarely use C anymore, I was thinking about the rule I was always told that “if you call malloc() (or new), you must call free() (or delete)”. It brought me to wondering if GCC (or another C compiler) attempts to perform any kind of memory and warn the user of a potential issue. I never heard about it, so I suspected it wasn’t the case, but I wanted to find out.

Here’s the example I used:

#include <stdlib.h>

int main() {
  int* first = malloc(sizeof(int) *  10);
  int* second = malloc(sizeof(int) * 10);

  int i = 0;
  for (; i < 10; i++) {
    first[i] = i * 2;
    second[i] = i * 10;
  }

  free(first);  /* Forgot to free second */
  return 0;
}

When compiling with gcc -Wall free_test.c no warnings were generated. While I can see why the compiler cannot provide a perfect answer because you’re dealing with heap memory and managing this at run time, why does the compiler not appear to attempt to provide a warning that there could be a memory leak?

Some of my thoughts on why include:

  1. The compiler may not have a perfect way of telling when memory is freed. For example, if one of those pointers was passed into a function and freed from within the function, the compiler may not be able to tell that.
  2. If a different thread takes ownership of the memory, the compiler would not have any way to tell that somebody else could be freeing 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-27T10:49:13+00:00Added an answer on May 27, 2026 at 10:49 am

    Cases that can’t be detected via static analysis (let alone via trivial static analysis) vastly outnumber those that can. The compiler authors presumably decided the benefits of adding this extra complexity to GCC was outweighed by the costs.

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

Sidebar

Related Questions

Very easy question. I have been using CSS for a while but rarely use
I have observed for a while that C# programmers tend to use int everywhere,
I have observed that in C++ namespaces are very rarely used while in .Net
I had the habit for a while to call malloc on anything. Then it
So I have two works computer, one that I use mainly for development, the
When it comes to WYSIWYG editors WYSI rarely WYG. The problem I always have
I have made a simple, but relatively computationally complex, UDF that queries a rarely
I constantly hear how bad reflection is to use. While I generally avoid reflection
I've read a lot here about misusage of UIAlertView and the fact that Apple
We have a new SQL 2008 R2 instance that's part of a cluster (active/passive)

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.