Basically I am trying to implement my own data type similar to vector. I'm doing this just for fun. Thing is that I constantly get Segmentation fault when trying to put a new element of type string (tried to create a struct named person and it had the same problem). Piece of code which is I think most important in this case (table.h):
template<typename type>
class table
{
private:
type* elements = new type[0];
unsigned int length_ = 0;
void resize(int change)
{
length_ = length_ + change;
type* elements_ = new type[length_];
std::memcpy(elements_, elements, length_ * sizeof(type));
delete[] elements;
elements = elements_;
}
public:
/* ... */
void put(type&& element)
{
resize(1);
elements[length_ - 1] = element;
}
}
Then (main.cpp):
/* ... */
int main()
{
table<string> t;
t.append("Hello World"); // segfault
for(string s : t) {cout << s << endl;}
/* But this works:
table<char*> t;
t.append((char*)"Hello World");
for(string s : t) {cout << s << endl;} */
}