Thread: does C automaticlly free mem allocation?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    43

    does C automaticlly free mem allocation?

    hi,
    i'm having a difference of opinions with one of my instructors.
    when a whole program (not just a block) comes to an end, do the DMAs get freed by the OS or do the allocations live on?
    Where can I find reference for this officially (the course book is "the C book", but this doesnt appear there)
    thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I do wonder about questions like this.

    Why not just code up a little program that malloc's a good sized amount of memory, and then does NOT free it.

    Check your available free memory with your OS, and then run the above mentioned little memory hog program. Maybe run it a few times.

    Now see what the OS reports as available memory.

    I don't believe you can get any more "official", than that.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a DMA buffer, you'd better make DARN SURE that whatever/whoever owns that memory doesn't free it before it's been "used up" (read or written to completeness, or the DMA operation cancelled if that's an option).

    Generally, large OS's such as Linux or Windows let's the driver take care of holding on to buffers - the driver takes the user-buffer and "maps/locks" it into the driver space - now the application can do what it likes - the buffer is still there until the driver sees fit to free it [and hopefully the driver writer knows what he/she is doing, so it won't get freed until it's no longer needed]. [[1]]

    In the case of smaller/proprietary OS's, then it's up to the OS design - without AT LEAST knowing what OS you are talking about, we can't even GUESS what the situation is.

    [[1]] The way that the OS actually deals with memory is that memory blocks are reference-counted, so a driver and an application owning the same bit of memory means that there are two owners - when one of them frees the memory [for example the application quitting], then there is still one owner left, and the memory is not actually freed until number of owners is zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. Custom Allocation
    By SevenThunders in forum C Programming
    Replies: 17
    Last Post: 04-09-2007, 04:10 PM
  3. If my DLL return an array, where do I free it?
    By franziss in forum C++ Programming
    Replies: 8
    Last Post: 10-03-2006, 06:53 AM
  4. Free store vs. heap
    By CondorMan in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2006, 11:41 AM