scope of dynamic variabe after program termination

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babu198649
    New Member
    • Sep 2007
    • 22

    scope of dynamic variabe after program termination

    hi
    a variable is allocated in dynamic memory(using new and is not deleted) and the programs gets terminated .now does the memory allocated will become junk.

    Code:
    int main()
    {
    	int *i = new int;
    }
    if the above program is executed it just allocates 4 bytes in heap and the program gets terminated.

    does this allocated memory will never be free even after program termination.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Well think about that - if that were true, then every program ever run would keep its memory, and your disk would require ever-larger space, you could never "delete a file" - you would always need more and more space.

    So the compiler is nice enough to take care of this for us. Part of the process the compiler goes through when the program terminates is the freeing of all memory that was used during the program.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by sicarie
      So the compiler is nice enough to take care of this for us. Part of the process the compiler goes through when the program terminates is the freeing of all memory that was used during the program.
      A compiler has nothing to do with it; it just delivers compiled object files which
      are linked by a linker. The linker produces a certain format file that is condsidered
      an executable binary file ('elf' or 'coff' or what have you). When that image is
      loaded the instructions are executed; when that process has finished the OS will
      remove every trace of it including all memory used by that process. So, yes,
      dynamic memory will be lost for the posterity if the process didn't take care of
      it itself.

      kind regards,

      Jos

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Hm, didn't know it was the OS freeing the memory - thanks Jos!

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by sicarie
          Hm, didn't know it was the OS freeing the memory - thanks Jos!
          Yep, the OS is the guilty one: it manages the 'text' space (where the instructions
          are) as well as the stack space and the 'data' space (where your heap is stored
          normally). When the process is dead the OS reclaims those spaces again and
          simply ruins everything that was in there. All open files and other resourses are
          closed as well. Consider the OS the reaper of the dead process.

          kind regards,

          Jos

          Comment

          • babu198649
            New Member
            • Sep 2007
            • 22

            #6
            thank u for u r replys

            if a variable is newed twice then will the memory be allocated twice.

            Code:
            #include <iostream.h>
            int main()
            {
                int *i;
                i = new int; //newed first time
                cout<<&*i<<"   ";
                i = new int; //newed second time
                cout<<&*i; 
            }

            in the above code the variable i is newed twice and the address of newed location is different on both allocations.

            how to find a variable is already newed or not.

            Comment

            Working...