Thread: How much will free(x) free?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    How much will free(x) free?

    How does free(x) know when to stop?
    I'm not sure if my question even makes sense, cause I don't know how free works (I've just learned that it needs to be used after we're done with anything that we malloc'ed).

    If you malloc(some number of bytes), will the system put some kind of marker at the end of the box of memory it reserved, so free() knows how much memory was associated?

    Thanks

    Also, if you have a block of memory, and a pointer (call it ptr) to somewhere in the middle of that block, what happens if you free(ptr)?

    [edit] silly typo, irrelevant though
    Last edited by Jang; 11-08-2010 at 08:51 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jang
    How does free(x) know when to stop?
    The information is available to it.

    Quote Originally Posted by Jang
    If you malloc(some number of bytes), will the system put some kind of marker at the end of the box of memory it reserved, so free() knows how much memory was associated?
    The method is implementation defined. A marker at the end probably won't work very well though, since the same series of bits could appear anywhere in between.

    Quote Originally Posted by Jang
    Also, if you have a block of memory, and a pointer (call it ptr) to somewhere in the middle of that block, what happens if you free(prt)?
    Undefined behaviour. The program will tend to "work" when you test it, but will mysteriously fail when you demonstrate the program to the customer in front of the boss.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Thanks for the swift reply.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Undefined behaviour. The program will tend to "work" when you test it, but will mysteriously fail when you demonstrate the program to the customer in front of the boss.
    Now ain't that the truth!!

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094

    Thumbs up

    Quote Originally Posted by laserlight View Post
    Undefined behaviour. The program will tend to "work" when you test it, but will mysteriously fail when you demonstrate the program to the customer in front of the boss.
    Excellent!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free() doesn't seem to work...
    By AlienJedi in forum C Programming
    Replies: 10
    Last Post: 01-29-2008, 05:27 PM
  2. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  3. How to free memory in this program
    By Coconut in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 10:57 PM