this is my first time posting here, so apologies if this is improperly formatted. Anyway, this block of code has been causing me nothing but problems for the last few hours. From what I can tell it ends up with unspecified behavior, because when run through an IDE it works, but when run on command line it causes a munmap_chunk(): invalid pointer error.
I've looked into said error, and the consensus was that it was caused by freeing memory twice. But i removed all destructors and delete statements from my code in an effort to reduce my mistakes and nothing helped.
So my next idea is that allocating memory with this friend function is the problem, and that temp is created, has memory allocated in the function, and when the function is finished, the memory is deallocated, and then temp has its destructor called, which causes yet another deallocation, which is essentially the error.
And thus, my idea is that I have to find a way to allocate memory to this array, yet I don't have a clue how to do so. Could someone save me? The worst part is that its due by tonight.
friend Chain operator+(const Chain &c1, const Chain &c2) {
Chain<Object> temp;
temp.size_= c1.size_ + c2.size_;
temp.array_ = new Object[temp.size_];
copy(c1.array_, c1.array_ + c1.size_, temp.array_);
copy(c2.array_, c2.array_ + c2.size_, temp.array_ + c1.size_);
return temp;
}