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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:39:24+00:00 2026-05-14T09:39:24+00:00

I read somewhere that it is disastrous to use free to get rid of

  • 0

I read somewhere that it is disastrous to use free to get rid of an object not created by calling malloc, is this true? why?

  • 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-14T09:39:25+00:00Added an answer on May 14, 2026 at 9:39 am

    That’s undefined behavior – never try it.

    Let’s see what happens when you try to free() an automatic variable. The heap manager will have to deduce how to take ownership of the memory block. To do so it will either have to use some separate structure that lists all allocated blocks and that is very slow an rarely used or hope that the necessary data is located near the beginning of the block.

    The latter is used quite often and here’s how i is supposed to work. When you call malloc() the heap manager allocates a slightly bigger block, stores service data at the beginning and returns an offset pointer. Smth like:

    void* malloc( size_t size )
    {
          void* block = tryAlloc( size + sizeof( size_t) );
          if( block == 0 ) {
              return 0;
          }
          // the following is for illustration, more service data is usually written
          *((size_t*)block) = size;
          return (size_t*)block + 1;
     }
    

    then free() will try to access that data by offsetting the passed pointer but if the pointer is to an automatic variable whatever data will be located where it expects to find service data. Hence undefined behavior. Many times service data is modified by free() for heap manager to take ownership of the block – so if the pointer passed is to an automatic variable some unrelated memory will be modified and read from.

    Implementations may vary but you should never make any specific assumptions. Only call free() on addresses returned by malloc() family functions.

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

Sidebar

Related Questions

I read somewhere that each object's instance variables are unique. I'm not sure how
I read somewhere that with NSString in an object, one has to use copy
I think I've read somewhere that this is possible. Use case I want to
I read somewhere that calling ensureIndex() actually creates a collection if it does not
I read somewhere that, overriding is the means by which you get polymorphism. Polymorphism
I think I read somewhere that some modules only have object oriented interfaces (
I read somewhere that ruby threads/fibre block the IO even with 1.9. Is this
I read somewhere that you can stop Magento from requiring two product descriptions... this
I read somewhere that the upload limit for php is about 2MBs. Does this
I read somewhere that setInterval is CPU intensive. I created a script that uses

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.