stl list

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....
Don't destroy anything you put in the list. Don't put anything in the list that you didn't create using new.
I think u didnt get it. Say:

1
2
3
4
5
6
{
char *ptr = new char[10];
...
lst.push_back(ptr);

}

after:

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...
Last edited on
The pointer went out of scope but not what it was pointing to since it was allocated with new
Even though ptr went put of scope, when you pushed it onto the list a copy was made. The list contains the copy.
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.

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
27
28
29
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 I understand correctly , you have something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Tokens
{
    char *ptr;

    void somewhere()
    {
         ptr = new char[10];
         // fill ptr with something useful
    }

    void tokenize_func ()
    {
        lst.push_back(ptr);
    }

    ~Tokens()
    {
        delete [] ptr;
    }
};

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 = new char [ /*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 
Can you post more of your code. Its a bit like trying to solve a jig-saw puzzle with only 3 pieces. ;o)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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(const char* ptr)
303 {
304         size_t len = strlen(ptr);
305        
306         alpha_val.first = new char[len+1];
307         mem = new char[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
Topic archived. No new replies allowed.