Thread: what will happen to this pointer when i did this...,

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    what will happen to this pointer when i did this...,

    CLASS* object;

    // initializing
    object = new CLASS;


    //shutting down....
    object = 0;


    take note i forgot to do this,

    delete object;

    i just immedietly set it to 0, so what happened there? it worked out fine here..,

    thanks,

  2. #2
    Davros
    Guest
    The memory reserved for your object will remain reserved. The pointer actually points to the memory reserved for it, so when you set it to NULL before calling delete you lose the opportunity to release the memory.

    If you place your code in loop, so that you keep reserving memory without deleting it - eventually you will run out. I.e.

    CLASS object;
    while(true)
    {
    object = new CLASS;
    object = 0;
    }

    As to what happens to 'leaked' memory when your application terminates - that's a good question. I'm not actually sure. I think it could depend on your operating system as to whether it will know that it should be deleted or whether it just stays lost.

    Does anyone know?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I think it could depend on your operating system as to whether
    >it will know that it should be deleted or whether it just stays lost.
    It depends on the OS, but all of the mainstream and good operating systems will reclaim memory from your program when it ends. Though don't rely on this, because you can't be sure. Always free memory that you allocate.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    ah oh yeah,

    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. file pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 03:52 PM