I have tried to implement solution to dynamical expansion of array as user is entering new data in main as shown:
ComplexNumber** nizKompl = new ComplexNumber*[1];
bool unos = true;
while(unos){
cout << "Unesite novi kompleksni broj(realni imaginarni): ";
if(cin >> re >> im){
nizKompl[brojUnesenih] = new ComplexNumber(re,im);
++brojUnesenih;
ComplexNumber** nizTemp = new ComplexNumber*[brojUnesenih+1];
copy(nizKompl, nizKompl+brojUnesenih, nizTemp);
for(int i=0; i<brojUnesenih; ++i){
delete nizKompl[i];
}
delete [] nizKompl;
nizKompl = nizTemp;
} else {
cout << endl << endl;
unos = false;
}
}
ComplexNumber is custom type. ComplexNumber works fine but I am having problem with double free or corruption error as I am entering new data and creating new ComplexNumbers in program.
Is there some better way to achieve this? I need to use dynamical array because It is for educational purpose.
As I understand double free error occurs when you try to free memory which is already free'd. But It seems that I just can't resolve the issue, as I can see no double free should happen.
Is something happening in memory of which I have no knowledge? Is it problem with std::copy as I am copying array of pointers of pointers?
I would really appreciate any help or suggestion. Thank you!
Aucun commentaire:
Enregistrer un commentaire