Thread: dynamic memory

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    dynamic memory

    Just been reading about the new and delete keywords and i made the following simple code

    Code:
    // dynamic memory allocation
    
    #include<iostream>
    using namespace std;
    
    int main(void)
    {
        int* pAge=new int;
        if(pAge == 0)
        {
            cout << "Sorry no memory available!" << endl;
            return(0);
        }
        
        *pAge=27;
    
        cout << "Address=" << pAge << endl
             << "Contents=" << *pAge << endl;
    
        return(0);
    }
    Notice i havent used a delete here to free the memory.

    How come this doesnt prompt an error or at least a warning on my compiler ?(MSVC++ 6.0)

    I would have thought whereever there is a new there must be a delete ?

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    You have just created a memory leak. It is up to the programmer to "clean" the memory up after you have used it.

    Yes, every time you use the keyword new there should be a corresponding delete.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Yeah i appreciate that but i thought a compiler would at least be able to count the number of news in a program and the number of deletes and if the numbers dont match issue a warning.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Or you could use an auto_ptr which would not need a delete at the end of the code:

    Code:
    #include <iostream>
    #include <memory>
    using namespace std;
    
    int main(void)
    {
        auto_ptr<int> pAge( new int );
        if( pAge.get() == NULL )
        {
            cout << "Sorry no memory available!" << endl;
            return(0);
        }
        
        *pAge=27;
    
        cout << "Address=" << pAge.get() << endl
             << "Contents=" << *pAge << endl;
    
        return(0);
    }
    If the program is successfull in allocating space for the integer, then it will automatically be freed without the need for a delete to be called prior to the return statement. You do need to remember to do things a bit differently however as noted in blue above. The get member gets the address of the pointer.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Even better. check out the smart pointer classes available here.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM