Kosher or leaky?

This is a discussion on Kosher or leaky? within the C++ Programming forums, part of the General Programming Boards category; Does this code leak memory, or is the allocated vector object cleaned up? Code: void func(vector <int> veccie) { if(veccie.empty()) ...

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

    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,806
    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,796
    >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, 09:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21