Yes, the code has a memory leak unless you delete the pointers. If the foo
class owns the pointers, it is its responsibility to delete them. You should do this before clearing the vector, otherwise you lose the handle to the memory you need to de-allocate.
for (auto p : v) { delete p; } v.clear();
You could avoid the memory management issue altogether by using a std::vector
of a suitable smart pointer.
Related Posts:
- C++ – No matching member function for call to ‘push_back’
- How to access the contents of a vector from a pointer to the vector in C++?
- How to initialize a vector of pointers
- How to implement 2D vector array?
- What are the differences between a pointer variable and a reference variable in C++?
- What is the easiest way to initialize a std::vector with hardcoded elements?
- Vector of Vectors to create matrix
- How to find out if an item is present in a std::vector?
- How do I print out the contents of a vector?
- Return array in a function
- What does “dereferencing” a pointer mean?
- Regular cast vs. static_cast vs. dynamic_cast
- What is a smart pointer and when should I use one?
- What does “dereferencing” a pointer mean?
- What is a char*?
- check if a std::vector contains a certain object?
- What exactly is nullptr?
- How to navigate through a vector using iterators? (C++)
- C++ for each, pulling from vector elements
- How do I erase an element from std::vector<> by index?
- How to fix a “invalid operands to binary expression” error?
- Is it more efficient to copy a vector by reserving and copying, or by creating and swapping? [duplicate]
- How do I reverse a C++ vector?
- What is a dangling pointer?
- Debug assertion failed. C++ vector subscript out of range
- What does `*&` in a function declaration mean?
- No matching member function for call to ‘push_back’ error
- (->) arrow operator and (.) dot operator , class pointer
- Pass a vector by reference C++
- What is uintptr_t data type
- What is the difference between const int*, const int * const, and int const *?
- Why there is no pop_front method in C++ std::vector?
- Initializing a two dimensional std::vector
- What is uintptr_t data type
- What does int & mean
- What is the difference between const int*, const int * const, and int const *?
- Debug assertion failed. C++ vector subscript out of range
- Vector is not a Template?
- Displaying a vector of strings in C++
- ‘vector’ in namespace ‘std’ does not have a template type
- How to find memory leak in a C++ code/project?
- Pointer to incomplete class type is not allowed
- Vector of structs initialization
- Passing a 2D array to a C++ function
- Converting a vector
to string - Can’t resolve Error: indirection requires pointer operand (‘int’ invalid)
- Why use a new call with a C++ ‘vector’?
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- Why doesn’t std::vector::push_front() exist?
- C++ delete vector, objects, free memory
- How to iterate over a vector?
- How to convert vector to array
- No operator << matches these operands
- Initialize empty vector in structure – c++
- What does int & mean
- C++: Expression must have a constant value when declaring array inside function
- splitting a string into an array in C++ without using vector
- Sorting Linked List C++ with pointers
- Is the sizeof(some pointer) always equal to four?
- What’s the most efficient way to erase duplicates and sort a vector?
- Best way to extract a subvector from a vector?
- Appending a vector to a vector
- C++ Expression must have pointer-to-object type
- How to run valgrind with basic c example?
- C++ strings and pointers
- initial value of reference to non-const must be an lvalue
- Typedef function pointer?
- C++ Erase vector element by value rather than by position?
- C++ – Assigning null to a std::string
- Correct way to work with vector of arrays
- C++ error: double free or corruption (fasttop)
- error C2106: ‘=’ : left operand must be l-value
- Using C-string gives Warning: “Address of stack memory associated with local variable returned”
- Reference to non-static member function must be called
- Why use pointers?
- I get this error: “glibc detected”
- What’s the difference between * and & in C?
- C++ pass an array by reference
- How to create a vector of class objects in C++?
- “vector” was not declared in this scope
- Function stoi not declared
- C++ Array of pointers: delete or delete []?
- Function stoi not declared
- Vector declaration “expected parameter declarator”
- Why is this vector iterator not incrementable?
- C++ correct way to return pointer to array from function
- Warning: comparison of distinct pointer types
- Initializing pointers in C++
- Why can’t I make a vector of references?
- Difference between function arguments declared with & and * in C++
- Is C++ Array passed by reference or by pointer?
- set head to NULL (‘NULL’ : undeclared identifier)
- Displaying contents of a vector container in C++
- What is the size of sizeof(vector)? C++
- What is a `char*`?
- c++ vector bubble sort
- C++ Swapping Pointers
- Array of Linked Lists C++
- “Cannot allocate an object of abstract type” error
- Differences between unique_ptr and shared_ptr