malloc function

This is a discussion on malloc function within the C Programming forums, part of the General Programming Boards category; I can allocate whatever size i want using malloc. Now it has to be freed after i use it. What ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    malloc function

    I can allocate whatever size i want using malloc. Now it has to be freed after i use it. What happens if i dont free the memory allocated using malloc. Say i were to write programs continuously using malloc but never free the allocated memory. Would my OS get affected by it and start behaving abnormally.

    I read there is this phenomenon called memory leak which comes into play if we dont free the allocated memory. What is implied by it and what are its disadvantages ?

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    12

    Memory Leaks

    Mallocing space continually would eventually make your system run out of memory.
    Since every time you malloc you request the operating system for some more memory. And if you do not free it, then the operating system thinks that you
    are using that memory.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    Say i were to write programs continuously using malloc but never free the allocated memory. Would my OS get affected by it and start behaving abnormally.
    No. All the program memory is freed by the OS (or should be) at program termination, no matter what. So if you malloc something that stays in use until the end of the program, you actually do not have to free it.

    A memory leak is when you do something like this:
    Code:
    char *ptr;
    for (i=0;i<13;i++) {
        ptr = malloc(666);
        [do things with ptr]
    }
    Every time malloc is called, the address in ptr changes. However, the previously malloc'd memory is still protected and cannot be overwritten. But, notice, you do not have a ptr to it anymore, which means you cannot use it or free it now! So it will just continue to uselessly exist for the duration of the program.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    When an application terminates, the OS will take back all the memory it allocated. So your OS will not be affected by an application that didn't free memory before terminating.

    The leaky application only takes up resources while it is running.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    So even if i do not use free it would be no issue. But then i suppose its a good practice to deallocate all the memory that i tried allocating to it.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Yes, it is a good practice.

    Also if you application runs for awhile, you do not want it to chew up system resources that could be used by other applications.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If your system runs out of memory, bad things MAY happen. Most OS's keep some memory in reserve so that system critical functions can still allocate memory.

    If your application uses up all the memory that is available then it will probably fail/crash - which is never a good thing [ok, it may not be a terrible thing, but it's certainly not a nice thing].

    --
    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.

  8. #8
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by matsp View Post
    ok, it may not be a terrible thing, but it's certainly not a nice thing
    It's nice to watch how the OS handles such a situation by swapping everything else out.

    Screenshot of me trying 7z-compression with a 64mb dictionarysize in ultra mode on a 512MB notebook: http://img269.imageshack.us/img269/4...foreafterj.jpg (left: before, right: after)

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by roaan View Post
    I read there is this phenomenon called memory leak which comes into play if we dont free the allocated memory. What is implied by it and what are its disadvantages ?
    Not so much anymore. Back in the old DOS days if you malloc'ed memory and didn't free it, it was gone. They _TRIED_ to correct this with the new memory manager (that also allowed access to more than 640KB) but it still wasn't all that great.

    Now days, though, the only real issues with memory leaks are in Kernel code. For example, in the Linux kernel when a process irresponsibly uses kmalloc() for dynamic memory allocation, the memory is forever lost and can only be reclaimed by a reboot.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Any leaks are bad. I would not downplay the issues.
    Example: Why does Firefox consume so much memory?
    Answer: Because a lot of the plug-ins leak memory!

    If you start leaking memory, 2 things may happen:
    1) The OS will start paging out memory of other processes, hurting their performance.
    2) Suddenly you've reached the allocation roof and when you try to allocate more, the OS says no, and the program usually malfunctions or crashes.

    Don't leak memory. Play nice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. malloc & cover function
    By Max in forum C Programming
    Replies: 4
    Last Post: 10-04-2002, 08:51 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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