Thread: Quick Question Regarding Pointers

  1. #1
    charash
    Guest

    Quick Question Regarding Pointers

    OK i've just got a quick question regarding pointers using new and delete operators. I tried to look it up in one book and several sites so far, but haven't really found an answer to this...

    If i dynamically allocate memory using new, when my program exits will that memory be freed automatically by the operating system?

    Perhaps an example would help...

    Code:
    int main() {
         int * num;
         num = new int;
         *num = 5;
         cout << "Value of number is: " << *num << endl;
         return 0;
    }
    (This isn't my real situation hehe)
    Anyway, in the above, would the memory 'num' is pointing to be freed upon termination of my program? Or would it create a memory leak where no application can access that memory???

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It depends on the operating system. It's better to make sure you delete your allocated memory.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    charash
    Guest

    What about windows?

    Well if it depends on the operating system (which i shouldve thought of before.. =x), do you (or anyone else) know specifically the behavior of windows in this kind of situation??

    I guess this should've been on Windows board, but i was thinking its more of a general c++ question when i first posted.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    I believe Windows releases the memory your application has allocated when it closes. However, other resources may not be released so it is a good idea to always explicitly release what you have allocated. For a more in-depth discussion on this subject, see Is it a memory leak.
    - lmov

  5. #5
    charash
    Guest

    Thanks

    Thanks for all your help guys. I guess i was a bit ambiguous. I'm just trying to make a set of classes that make windows development a bit easier (i know there are many out there but i find it a lot more educational to write my own =)). Basically, this is what a program would look like:

    Code:
    Window * Form1;
    BEGIN_MAIN
         Form1 = new Window;
         Form1->show();
    END_MAIN
    I can't call delete on the pointer right after i show because it hasnt entered the message loop yet. Cleaning up the acutal components isn't an issue, since i have all that code in my destructors. After reading your replies and the posts on the link you gave me, i think i should be ok since my class destructor should at least be called, right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about pointers in C
    By Kilua in forum C Programming
    Replies: 3
    Last Post: 06-05-2009, 02:33 AM
  2. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 07:33 AM
  3. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  4. Quick help with pointers
    By Leeman_s in forum C++ Programming
    Replies: 8
    Last Post: 12-21-2001, 08:59 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM