Copy Constructor for Array

Nov 7, 2011 at 7:07am
Hi, this has been bugging me forever. I've tried searching but unable to get the right info for this.

So I'm trying to implement my copy constructor for an array class, and here it goes:
and buf, size, and capacity are the data members.

Array(const Array& source)
{
capacity = source.capacity;
buf = new string[capacity];
size = source.size;
for (unsigned int i = 0; i < size; i++)
{
buf[i] = source.buf[i];
}
}

What am I allocating wrong? I am getting a seg fault when I test this.
Any guidance would be helpful and appreciated, thanks.
Nov 7, 2011 at 8:26am
Can you post your class declaration, default constructor (if any) and where you calling copy constructor.

Also, makes sure 'size' doesn't exceed 'capacity'.
Last edited on Nov 7, 2011 at 8:27am
Nov 7, 2011 at 3:52pm
need more information on the class Array .
Nov 9, 2011 at 10:34pm
Sorry.

Default is
Array(size_t capacity)
{
capacity = CAPACITY;
//CAPACITY is a global and capacity is 45000 so size wont exceed capacity anytime soon

buf = new string[capacity];
size = 0;
}

im just calling the copy constructor like this:
Array array;
//then i pushed elements into the array, already confirmed this works properly
then i do
Array copy = array;
Nov 9, 2011 at 10:42pm
hi,
don't take me wrong but it would be better if you paste both, class declaration and definition in your post.
also use code tags so other people can read your code.
to do this use "code tags" button (small <> button on the right when you start posting)
Topic archived. No new replies allowed.