Thread: What happens when C exits after a pointer was overriden

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    3

    What happens when C exits after a pointer was overriden

    Hi,
    Suppose pA is a pointer to dynamic allocated memory using calloc. Now I override pA, so pA no longer holds the address of the allocated memory (I know there is a memory leak, but that is not the point). After the program exits (free was not used), does it free the allocated memory made earlier ?

    In order that upon exit, the allocated memory be free, must pA hold the address to the allocated memory ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    3
    Quote Originally Posted by Salem View Post
    Thanks, that answered part of my question. The part that still needs an answer is :
    pA was overridden before there was a chance to use free. upon exit, the OS clears the memory, even if free was not used. In order that the OS can free the memory must pA hold the address to the allocated memory ?
    Last edited by David20132013; 07-02-2013 at 02:19 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, it's not necessary. The OS gives the program a block of memory. Think of it as one large chunk. When the program terminates, ALL that large chunk of memory, is available (free'd), to the OS, for use by any other program.

    Memory leaks are really a problem when the program leaking memory, runs for a long(er) time. Eventually, the chunk of memory available to the program runs low, causing the system to run very slowly as it starts using available swap space on the hard drive, as RAM (very slow). Finally, the program will crash.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    3
    Quote Originally Posted by Adak View Post
    No, it's not necessary. The OS gives the program a block of memory. Think of it as one large chunk. When the program terminates, ALL that large chunk of memory, is available (free'd), to the OS, for use by any other program.

    Memory leaks are really a problem when the program leaking memory, runs for a long(er) time. Eventually, the chunk of memory available to the program runs low, causing the system to run very slowly as it starts using available swap space on the hard drive, as RAM (very slow). Finally, the program will crash.
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function exits alone
    By xixonga in forum C Programming
    Replies: 2
    Last Post: 12-04-2010, 01:17 PM
  2. Calling the overriden method from the base class
    By DavidP in forum C# Programming
    Replies: 2
    Last Post: 02-19-2010, 02:55 PM
  3. How to prevent a function from being overriden?
    By thomas_nibu in forum C++ Programming
    Replies: 18
    Last Post: 11-13-2005, 09:55 PM
  4. How do I do something when a program exits?
    By ojschubert in forum C Programming
    Replies: 2
    Last Post: 05-25-2005, 11:27 AM
  5. Multiple exits
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 04-24-2003, 06:37 PM