Thread: Kosher or leaky?

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Kosher or leaky?

    Does this code leak memory, or is the allocated vector object cleaned up?
    Code:
    void func(vector <int> veccie)
    
    {
    
        if(veccie.empty()) cout << "Empty" << endl;
    
        else cout << "size: " << veccie.size() << "\t veccie[0]: " << veccie[0] << endl;
    
    }
    
    
    
    int main(int argc, char *argv[])
    
    { 
    
        func(vector <int> (4, 23));
    
        system("PAUSE");
    
        return 0;
    
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by CodeMonkey
    Does this code leak memory, or is the allocated vector object cleaned up?
    Code:
    void func(vector <int> veccie)
    
    {
    
        if(veccie.empty()) cout << "Empty" << endl;
    
        else cout << "size: " << veccie.size() << "\t veccie[0]: " << veccie[0] << endl;
    
    }
    
    
    
    int main(int argc, char *argv[])
    
    { 
    
        func(vector <int> (4, 23));
    
        system("PAUSE");
    
        return 0;
    
    }
    The vector you pass into the function initially is only used as a temporary to initialize the copy of the vector used in the function itself. The temporary should take care of its own destruction. In the func function itself, the copy will destruct itself one the function ends. So.. I believe everything is cleaned up.
    "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

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does this code leak memory
    No, you're good.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Kosher or Leaky?
    By CodeMonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2005, 08:26 PM