i have a dynamic memory containing some characters...i need to push the characters into a list and i do it by
1 2
list <char*> lst;
lst.push_back(ptr); //where ptr in on the heap.
however this memory (ptr) is destroyed via a destructor....so now lst has some garbage value or nothing...how do i push a pointer and make sure i have no issues with retrieving even if the memory goes out of scope....
iterate through the lst and it has garbage value since ptr has gone out of scope and it has been deleted by the destructor. Now how do i make it such that the value remains even though the ptr goes out of scope but its contents is what i need...
Oh, and the list appears to contain garbage because you didn't put anything in your char arrays. So they contain random values. But they are still valid arrays that you can use from the pointers stored in your list.
yes but since the ptr is in a class the destructor is called and delete []ptr; now there is no memory. but i have pushed it earlier into the list and now the list shows its empty. But im trying to have something that will continue to exist in the list even after ptr is destroyed. So im sorry, its not out of scope but DESTROYED.
list <char*> lst1;
{
134 Tokens Obj;
135 pair<char*,char*> test = Obj.tokenize_func(data);/* in the function tokenize, the ptr is pushed onto the list but since the display() is outside the braces and the scope of the object is not there, the destructor is called and the ptr is destroyed*/
136
137
151
153 }
154 display(lst1);
156
157
165
166 ////////////////////////////////////////////////////////////
167 return 0;
168 }
169 ///////////////////////////////FUNCTIONS//////////////////////////
170 void display(list<char*> &lst)
171 {
172
173 list<char*>::iterator iter = lst.begin();//now there is nothing pointed to in the list
174 while(iter != lst.end())
175 {
176 cout<<"the values is: "<<*iter<<endl;
177 iter++;
178 }
179 cout<<endl<<endl;;
180 }
If this is the case, you need to create a new array and copy it before pushing into the list
eg:
1 2 3
char *nptr = newchar [ /*size of ptr array*/ ];
// copy everything from ptr to nptr
lst.push_back ( nptr ); // push a copy of the array, so it won't get deleted with the object
list <char*> lst1;
int main()
{
{
Tokens Obj;
pair<char*,char*> test = Obj.tokenize_func(data);/* in the function tokenize, the ptr is pushed onto the list but since the display() is outside the braces and the scope of the object is not there, the destructor is called and the ptr is destroyed*/
}
display(lst1);// now Obj is destroyed and hence is alpha_val.first
return 0;
}
169 ///////////////////////////////FUNCTIONS//////////////////////////
170 void display(list<char*> &lst)
171 {
172
173 list<char*>::iterator iter = lst.begin();//now there is nothing pointed to in the list
174 while(iter != lst.end())
175 {
176 cout<<"the values is: "<<*iter<<endl;
177 iter++;
178 }
179 cout<<endl<<endl;;
180 }
pair<char*,char*> Tokens::tokenize_func(constchar* ptr)
303 {
304 size_t len = strlen(ptr);
305
306 alpha_val.first = newchar[len+1];
307 mem = newchar[len+1];
308 if(mem == NULL || alpha_val.first == NULL)
309 {
310 cout<<"error allocating memory in tokenize"<<endl;
311 exit(-1);
312 }
313 int j=0,i=0;
314
316 //////////////////////////////IF IDENT//////////////////////////////////
317 if(charcmp(ptr,alpha))
318 {
319 mem[i] = *ptr;
320 i++;
321 while((charcmp((ptr+i),alpha)) || (charcmp((ptr+i),digit)))
322 {
323 mem[i] = *(ptr+i);
324 i++;
325 }
326 mem[i] = '\0';
327 memcpy(alpha_val.first, mem,strlen(mem)+1);
328
329
330 lst1.push_back(alpha_val.first);
)
This is part of the code.
But I have solved the puzzle though i think it is not the best solution perhaps. I created a list that takes a class parameter and pushes an entire class with a copy constructor. and this class copies the the value of alpha_val into its pointer.so each time the class is pushed in and the copy constructor would create a dynamic heap that will thus would not be destroyed till the very end of the program