How to effectively reallocate/copy std::string array?

Hello,
I have a C++ homework assignment, which forbids me to use list or vector and I have to store some data in arrays of std::string. I have to allocate it for 1000 records and when it comes near to this value, I should reallocate the array (for twice the previous size...).

What I do is I create new arrays using "new", but I don't know how copy the data from the previous array to the new one. I tried to use memmove, which gives me segfaults, probably because I state that I want to copy "Size_of_array * sizeof(string)", which I don't think work with std::strings.

How to easily and correctly do this? I hope I am understood :) And sorry for my English too.

EDIT: To be more specific about my code, I have:

1
2
3
if (this->size == (this->max - 1)){
		    Realloc();
		}


...

1
2
3
4
5
6
7
8
9
10
11
void CPhoneBook::Realloc(){
    string * newName = new string [2 * this->max];

    memmove (newName, this->name, this->size * sizeof(string));

    delete [] this->name;
	
    this->name = newName;

    this->max *=2;
}


Thanks very much for your advice!
Last edited on
#include <algorithm>

move(name, name+size, newName); // modern C++
// copy(name, name+size, newName); // old C++
string * newName = new string [2 * this->max];
Is this legal C++? Doesnt the array size have to be a constant?
Is this legal C++? Doesnt the array size have to be a constant?


No, the whole point of new is give you a way to have dynamically sized arrays.
Topic archived. No new replies allowed.