When trying to delete/free character ptr without being processed completely by strtok_r
, its giving me stack trace error.
I know that one cannot free/delete a strtok_r
char ptr in a regular way, without completing the whole strings separation process by strtok_r
func.
Can anyone tell me how to free a char ptr, when its under process by strtok_r
?
char *data = new char[temp->size()+1];//temp is of type string
copy(temp->begin(),temp->end(),data);
data[temp->size()]='\0';
count = 0;
while(pointData != NULL)
{
if(count == 0)
pointData = strtok_r(data,":",&data);
else
pointData = strtok_r(NULL,":",&data);
if(count == 5)//some condition to free data
delete[] data;// this produces stack trace error
cout<<pointdata<<endl;
count++;
}
Because strtok_r is advancing "data" as it goes, which means that it is no longer pointing at the address of the allocation; you need to keep a "freedata" pointer or the like around:
char *data, *freedata;
freedata = data = new char[temp->size()+1];
// do yer stuffz here
delete[] freedata;