Segmentation Fault (Core Dumped) C++ - Pointers -
i'm pretty new pointers , i'm assigned array doubling task whenever run code segmentation fault(core dumped) error. can @ code , tell me error coming , can fix it?
i'm stuck , appreciated.
code:
int size = length; string *new_array = null; string *tmp_array = new string[50]; for(int k=0; k<50; k++)//initial array copying. tmp_array[k] = wordarray[k]; for(int i=0; i<=1; i++)//array range 0 n-1 { new_array = new string[size*2]; for(int j=0; j<size; j++)//array range 0 n-1 { new_array[j]=tmp_array[j]; new_array[j+size]="empty"; } delete[] tmp_array; //deleting old array size=size*2; } delete[] tmp_array;// free memory cout<<new_array[x]<<endl;
there error somewhere in there. there no other pointer reference in of code.
thanks
the problem trying access tmp_array
after it's been delete
d.
for(int i=0; i<=1; i++)//array range 0 n-1 { new_array = new string[size*2]; for(int j=0; j<size; j++)//array range 0 n-1 { new_array[j]=tmp_array[j]; new_array[j+size]="empty"; } // deleted tmp_array used in next iteration of loop, delete[] tmp_array; //deleting old array //=============================== // add fix problem //=============================== tmp_array = new_array; size=size*2; }
Comments
Post a Comment