How much will free(x) free?

This is a discussion on How much will free(x) free? within the C Programming forums, part of the General Programming Boards category; How does free(x) know when to stop? I'm not sure if my question even makes sense, cause I don't know ...

  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 07:51 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04:27 PM
  2. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21