Naveen
Naveen

Reputation: 1523

Deleting array of strings

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

Answers (1)

Remy Lebeau
Remy Lebeau

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

Related Questions