@Peter87
I tried again to use the second version and it worked, turns out it was a issue with my display function, so in the end both versions work.
I am curious however: Is there any performance cost between the two?
@gunnerfunner
Thank you this is a very useful hint you pointed out.
@Thomas1965
It works now thank you,there was a problem with another function that displayed the output wrong.
I use the GNU GCC compiler default of code::blocks.
Is there any performance cost between the two?
While we are on the topic, I have another question:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
void Friend_list::add_friend()
{
system("CLS");
Friend *f_new=new Friend[++size];
if(size>=1)
{
for(int i=0;i<size-1;++i)
{
f_new[i]=f[i];
}
}
f=f_new;
delete f_new; // if I use delete[] here;crash
f_new=NULL;
cout<<"\n\n \"Add friend\"\n\n";
cout<<" Please input name: ";
cin.ignore();
getline(cin,f[size-1].name,'\n');
cout<<" Please input no. of days: ";
cin>>f[size-1].days;
while(f[size-1].days<0)
{
cout<<" Please input no. of days: ";
cin>>f[size-1].days;
}
}
|
I have read about dynamic allocation on my struct as you can see my object is dynamically allocated as an array and I should free it to prevent memory leak,but most people state that a
<type> *ptr = new <type>[] should be followed with a
delete[] ptr statement not a simple
delete ptr.
However if I try doing so to avoid undefined behavior my program crashes, any idea why?