Results 1 to 4 of 4

Thread: scope of dynamic variabe after program termination

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

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

    Qt Code:
    1. int main()
    2. {
    3. int *i = new int;
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.because the books say that the newed variables can made free only with delete.

    also,

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

    Qt Code:
    1. #include <iostream.h>
    2. int main()
    3. {
    4. int *i;
    5. i = new int; //newed first time
    6. cout<<&*i<<" ";
    7. i= new int; //newed second time
    8. cout<<&*i;
    9. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: scope of dynamic variabe after program termination

    Quote Originally Posted by babu198649 View Post
    does this allocated memory will never be free even after program termination.because the books say that the newed variables can made free only with delete.
    OS will free the memory after program execution stops but image what happens if one does this constantly during the program execution.

    how to find a variable is already newed or not.
    A common approach is to initialize pointers to null and set them back to null after deletion.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    babu198649 (6th February 2008)

  4. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: scope of dynamic variabe after program termination

    thanks jpn

    A common approach is to initialize pointers to null and set them back to null after deletion.
    what if a pointer takes the first value(NULL) in the heap while allocating a space for it

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: scope of dynamic variabe after program termination

    It can't. The heap starts with much higher addresses.

Similar Threads

  1. Just for fun game
    By vermarajeev in forum Qt-based Software
    Replies: 6
    Last Post: 13th December 2007, 21:52
  2. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 02:49
  3. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.