Reputation: 1523
Suppose I have an array of strings:
string* item
And this item
array is dynamically constructed using new
operator. How do I free up this dynamically allocated memory if the number of entries in the array is numItems
?
Upvotes: 0
Views: 87
Reputation: 596352
Use new[]
to allocate the array, and delete[]
to free it:
#include <string>
std::string* item = new std::string[numItems];
...
delete[] item;
A better option is to use std::vector
and let it handle the memory for you:
#include <string>
#include <vector>
std::vector<std::string> item(numItems);
Upvotes: 2