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
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No memory leak. The destructor for std::vector cleans up after itself.

    An example of the type of memory leak I think you were hinting at:
    Code:
    void foo(int* p) {
       std::cout << *p << std::endl;
       // delete p; is never called, and the only [see main] reference to this pointer is lost
       // when this function returns.
    }
    
    int main( ) {
       foo(new int);
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed