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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:22:20+00:00 2026-05-13T20:22:20+00:00

int main() { int x = 0; free(x); } This compiles and appears to

  • 0
int main()
{
    int x = 0;
    free(x);
}

This compiles and appears to be a no-op. What actually happens? is this behavior defined?

Thanks!

  • 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-13T20:22:20+00:00Added an answer on May 13, 2026 at 8:22 pm

    No, the behavior is not defined. Moreover, the code is not supposed to compile.

    Firstly, the code is not supposed to compile because it contains a constraint violation. The expression you are passing as an operand to free has int type. The parameter of free has void * type. The only case when an int value can be implicitly converted to void * type is when the int value is an Integral Constant Expression (ICE) with value 0. In your case x is not an ICE, meaning that it is not implicitly convertible to void *. The only reason your code compiles is that for historical reasons (to support legacy code) your compiler quietly overlooks the constraint violation present in the free(x) call. I’m sure that if you elevate the level of warnings in your compiler, it will complain (at least with a warning). A pedantic compiler will immediately issue an error for free(x) call. Try Comeau Online, for example in C89/90 mode:

    "ComeauTest.c", line 6: error: argument of type "int" is incompatible with parameter
              of type "void *"
          free(x); 
               ^
    

    (Also, did you remember to include stdlib.h before calling free?)

    Secondly, let’s assume that the code compiles, i.e. it is interpreted by the compiler as free((void *) x). In this case a non-constant integral value x is converted to pointer type void *. The result of this conversion is implementation defined. Note, that the language guarantees that when an ICE with value of 0 is converted to pointer type, the result is a null pointer. But in your case x is not an ICE, so the result of the conversion is implementation-defined. In C there’s no guarantee that you will obtain a null pointer by converting a non-ICE integer with value 0 to pointer type. On your implementation it probably just happened that (void *) x with non-ICE x equal to 0 produces a null pointer value of type void *. This null pointer value, when passed to free, results in a no-op, per the specification of free.

    In general case though, passing such a pointer to free will result in undefined behavior. The pointers that you can legally pass to free are pointers obtained by previous calls to malloc/calloc/realloc and null pointers. Your pointer violates this constraint in general case, so the behavior is undefined.

    This is what happens in your case. But, again, your code contains a constraint violation. And even if you override the violation, the behavior is undefined.

    P.S. Note, BTW, that many answers already posted here make the same serious mistake. They assume that (void *) x with zero x is supposed to produce a null pointer. This is absolutely incorrect. Again, the language makes absolutely no guarantees about the result of (void *) x when x is not an ICE. (void *) 0 is guaranteed to be null pointer, but (void *) x with zero x is not guaranteed to be null pointer.

    This is covered in C FAQ http://c-faq.com/null/runtime0.html . For those interested in better understanding of why it is so it might be a good idea to read the entire section on null pointers http://c-faq.com/null/index.html

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It may be possible to run Facebook application locally on… May 15, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer What about existsCount = int.Parse(cmd.Parameters["successCount"].Value.ToString()); May 15, 2026 at 8:19 am
  • Editorial Team
    Editorial Team added an answer For over a decade now C++ provides the constexpr keyword… May 15, 2026 at 8:19 am

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.