I want to dynamically allocate an array of std::string. There is a function to allocate. I can call the function as many number of times as I want through out the program. If the pointer to the array is already allocated, I want to release the memory first then allocated the new one.
Here is what I tried:
std::string *names;
bool already_allocated = false;
void allocate( int n)
{
if( already_allocated)
{
delete names;
}
names = new std::string[n];
already_allocated = true;
}
int main()
{
allocate(5);
allocate(6);
return 0;
}
But it is giving runtime error in the 2nd allocate() call for the line delete names
Am I misunderstanding something?
std::vector<std::string>andresize?