Hello,

I'm using a CList in order to hold structure into a list. When I need to
read the very next node (next structure within the list) I see pointer value
changes but l I still see same information in previous node or stucture.
Any suggestions?

Thanks,
Smith

here is sample:


typedef struct pMyStruct{
LONG field0;
CString field1;
CString field2;
CString field3;
CString field4;

} pMyStruct;



POSITION pos = MyList.GetHeadPosition();

while(pos)
{

MyList.GetNext(pos);
AfxMessageBox(pMyStruct->field2);
}

Re: problem with link list by Igor

Igor
Wed Sep 29 14:09:25 CDT 2004

"JSmith" <jsmithmitra@yahoo.com> wrote in message
news:%23iY6bUlpEHA.3856@TK2MSFTNGP10.phx.gbl
> I'm using a CList in order to hold structure into a list. When I need
> to read the very next node (next structure within the list) I see
> pointer value changes but l I still see same information in previous
> node or stucture. Any suggestions?
>
> here is sample:
>
>
> typedef struct pMyStruct{
> LONG field0;
> CString field1;
> CString field2;
> CString field3;
> CString field4;
>
> } pMyStruct;
>
>
>
> POSITION pos = MyList.GetHeadPosition();
>
> while(pos)
> {
>
> MyList.GetNext(pos);
> AfxMessageBox(pMyStruct->field2);
> }

This code should not compile. pMyStruct is a type, not a variable.
pMyStruct->field2 should produce a syntax error.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage



Re: problem with link list by JSmith

JSmith
Wed Sep 29 14:41:33 CDT 2004

Hi Igor,

Ok, this is the code I have, the previous one had some mistakes while I was
typig it sorry. Please let me know why the last phase which reads back the
data from link list only shows the last inserted node?

typedef struct {
LONG field0;
CString field1;
CString field2;
CString field3;
CString field4;
}MyStruct;

MyStruct* p = NULL;
CList <MyStruct, MyStruct> mylist;

POSITION pos = MyList.GetHeadPosition();
while(condition for reading a table)
{
p = new MyStruct;

p->field0 = data1; //placing data into struct fields
p->field1 = data2;

mylist.AddHead(p); //add the struct into link list
}


//Read back them from link list to make sure what the link list is holding


POSITION pos = mylist.GetHeadPosition();



while(pos)
{

mylist.GetNext(pos);
AfxMessageBox(p->field0);
}






"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:eDSgTflpEHA.3324@TK2MSFTNGP15.phx.gbl...
> "JSmith" <jsmithmitra@yahoo.com> wrote in message
> news:%23iY6bUlpEHA.3856@TK2MSFTNGP10.phx.gbl
> > I'm using a CList in order to hold structure into a list. When I need
> > to read the very next node (next structure within the list) I see
> > pointer value changes but l I still see same information in previous
> > node or stucture. Any suggestions?
> >
> > here is sample:
> >
> >
> > typedef struct pMyStruct{
> > LONG field0;
> > CString field1;
> > CString field2;
> > CString field3;
> > CString field4;
> >
> > } pMyStruct;
> >
> >
> >
> > POSITION pos = MyList.GetHeadPosition();
> >
> > while(pos)
> > {
> >
> > MyList.GetNext(pos);
> > AfxMessageBox(pMyStruct->field2);
> > }
>
> This code should not compile. pMyStruct is a type, not a variable.
> pMyStruct->field2 should produce a syntax error.
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>



Re: problem with link list by Igor

Igor
Wed Sep 29 14:48:23 CDT 2004

"JSmith" <jsmithmitra@yahoo.com> wrote in message
news:%23W7AfxlpEHA.1272@TK2MSFTNGP09.phx.gbl
> Hi Igor,
>
> Ok, this is the code I have, the previous one had some mistakes while
> I was typig it sorry. Please let me know why the last phase which
> reads back the data from link list only shows the last inserted node?
>
> typedef struct {
> LONG field0;
> CString field1;
> CString field2;
> CString field3;
> CString field4;
> }MyStruct;
>
> MyStruct* p = NULL;
> CList <MyStruct, MyStruct> mylist;
>
> POSITION pos = MyList.GetHeadPosition();
> while(condition for reading a table)
> {
> p = new MyStruct;
>
> p->field0 = data1; //placing data into struct fields
> p->field1 = data2;
>
> mylist.AddHead(p); //add the struct into link list

This code should not compile. CList <MyStruct, MyStruct>::AddHead takes
MyStruct as a paramter, not MyStruct*.

> POSITION pos = mylist.GetHeadPosition();
> while(pos)
> {
>
> mylist.GetNext(pos);

This line affects on mylist and pos

> AfxMessageBox(p->field0);

This line uses p, which is not in any way related to mylist or pos. Why
do you expect the first line to affect the second line?
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage



Re: problem with link list by JSmith

JSmith
Wed Sep 29 15:07:33 CDT 2004

Correct me if I'm wrong. I was assuming that way I would access to a feild
of the structure.
as it is placed by below code so it should be accessbile thru the "p" again.

> > while(condition for reading a table)
> > {
> > p = new MyStruct;
> >
> > p->field0 = data1; //placing data into struct fields
> > p->field1 = data2;
> >
> > mylist.AddHead(p); //add the struct into link list
>

Any comment on this?

thanks,
JSmith


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:eYYLF1lpEHA.592@TK2MSFTNGP11.phx.gbl...
> "JSmith" <jsmithmitra@yahoo.com> wrote in message
> news:%23W7AfxlpEHA.1272@TK2MSFTNGP09.phx.gbl
> > Hi Igor,
> >
> > Ok, this is the code I have, the previous one had some mistakes while
> > I was typig it sorry. Please let me know why the last phase which
> > reads back the data from link list only shows the last inserted node?
> >
> > typedef struct {
> > LONG field0;
> > CString field1;
> > CString field2;
> > CString field3;
> > CString field4;
> > }MyStruct;
> >
> > MyStruct* p = NULL;
> > CList <MyStruct, MyStruct> mylist;
> >
> > POSITION pos = MyList.GetHeadPosition();
> > while(condition for reading a table)
> > {
> > p = new MyStruct;
> >
> > p->field0 = data1; //placing data into struct fields
> > p->field1 = data2;
> >
> > mylist.AddHead(p); //add the struct into link list
>
> This code should not compile. CList <MyStruct, MyStruct>::AddHead takes
> MyStruct as a paramter, not MyStruct*.
>
> > POSITION pos = mylist.GetHeadPosition();
> > while(pos)
> > {
> >
> > mylist.GetNext(pos);
>
> This line affects on mylist and pos
>
> > AfxMessageBox(p->field0);
>
> This line uses p, which is not in any way related to mylist or pos. Why
> do you expect the first line to affect the second line?
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>



Re: problem with link list by Igor

Igor
Wed Sep 29 15:26:09 CDT 2004

"JSmith" <jsmithmitra@yahoo.com> wrote in message
news:uteRBAmpEHA.1712@tk2msftngp13.phx.gbl
> Correct me if I'm wrong. I was assuming that way I would access to a
> feild of the structure.

You are wrong. You access the elements like this:

const MyStruct& s = mylist.GetNext(pos);
// or
MyStruct s = mylist.GetNext(pos);

AfxMessageBox(s.field0);

--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage



Re: problem with link list by JSmith

JSmith
Wed Sep 29 15:54:37 CDT 2004

Hi Igor,

Right! I have another question on this. At end of the function, I have to
assign NULL to all those nodes thru a loop and delete the pointer finally.
Is that enough or there is some other steps I have to go ?

Once again thanks for your help.

Regards,
Smith


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:eeMhLKmpEHA.3520@TK2MSFTNGP11.phx.gbl...
> "JSmith" <jsmithmitra@yahoo.com> wrote in message
> news:uteRBAmpEHA.1712@tk2msftngp13.phx.gbl
> > Correct me if I'm wrong. I was assuming that way I would access to a
> > feild of the structure.
>
> You are wrong. You access the elements like this:
>
> const MyStruct& s = mylist.GetNext(pos);
> // or
> MyStruct s = mylist.GetNext(pos);
>
> AfxMessageBox(s.field0);
>
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>



Re: problem with link list by Igor

Igor
Wed Sep 29 16:10:11 CDT 2004

"JSmith" <jsmithmitra@yahoo.com> wrote in message
news:%23XYbUampEHA.3728@TK2MSFTNGP09.phx.gbl
> Right! I have another question on this. At end of the function, I
> have to assign NULL to all those nodes thru a loop and delete the
> pointer finally. Is that enough or there is some other steps I have
> to go ?

You have not stored these pointers anywhere, so now you have no way to
delete them. You have already produced a memory leak.

Realize that CList <MyStruct, MyStruct> does not store _pointers_ to
MyStruct - it stores _copies_ of MyStruct. When the list is destroyed,
it automatically destroys the copies that it stored, but it does not
know nor care about those instances you allocated on the heap, then
promptly forgot about. There is no reason to make any heap allocations
in the first place.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage



Re: problem with link list by JSmith

JSmith
Wed Sep 29 16:19:37 CDT 2004

Ok, how can I store pointers only in to link lists instead of holding a copy
of structure?
any link or if you have time can you explain it please? I need to realize
the whole procedure.


Thanks,

"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:udYRyimpEHA.1960@TK2MSFTNGP10.phx.gbl...
> "JSmith" <jsmithmitra@yahoo.com> wrote in message
> news:%23XYbUampEHA.3728@TK2MSFTNGP09.phx.gbl
> > Right! I have another question on this. At end of the function, I
> > have to assign NULL to all those nodes thru a loop and delete the
> > pointer finally. Is that enough or there is some other steps I have
> > to go ?
>
> You have not stored these pointers anywhere, so now you have no way to
> delete them. You have already produced a memory leak.
>
> Realize that CList <MyStruct, MyStruct> does not store _pointers_ to
> MyStruct - it stores _copies_ of MyStruct. When the list is destroyed,
> it automatically destroys the copies that it stored, but it does not
> know nor care about those instances you allocated on the heap, then
> promptly forgot about. There is no reason to make any heap allocations
> in the first place.
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>



Re: problem with link list by Igor

Igor
Wed Sep 29 16:27:45 CDT 2004

"JSmith" <jsmithmitra@yahoo.com> wrote in message
news:unUPSompEHA.536@TK2MSFTNGP09.phx.gbl
> Ok, how can I store pointers only in to link lists instead of holding
> a copy of structure?

That would be CList <MyStruct*, MyStruct*> of course. Then you are
indeed responsible for manually deleting every node, because the list
will only maintain pointers for you. Why do you feel the need to do
that?
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage



Re: problem with link list by JSmith

JSmith
Wed Sep 29 16:38:36 CDT 2004

Igor, Thanks for your quick response
You mean there is no difference between copying the structures and placing
pointers into the link list ?
Won't be that (placing pointer into list) an optimized code ?

Thanks,


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:etXbmsmpEHA.3712@TK2MSFTNGP15.phx.gbl...
> "JSmith" <jsmithmitra@yahoo.com> wrote in message
> news:unUPSompEHA.536@TK2MSFTNGP09.phx.gbl
> > Ok, how can I store pointers only in to link lists instead of holding
> > a copy of structure?
>
> That would be CList <MyStruct*, MyStruct*> of course. Then you are
> indeed responsible for manually deleting every node, because the list
> will only maintain pointers for you. Why do you feel the need to do
> that?
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>



Re: problem with link list by Igor

Igor
Wed Sep 29 16:48:23 CDT 2004

"JSmith" <jsmithmitra@yahoo.com> wrote in message
news:%23d874ympEHA.1668@TK2MSFTNGP14.phx.gbl
> Igor, Thanks for your quick response
> You mean there is no difference between copying the structures and
> placing pointers into the link list ?
> Won't be that (placing pointer into list) an optimized code ?

What makes you think so? Have you coded it both ways, and then run a
profiler to prove that one is faster than another? Is this code on the
critical path in your program, so that it becomes a bottleneck? Have you
taken into account your own time that you have wasted struggling with
the problem, that you could instead use to do something productive?
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage



Re: problem with link list by JSmith

JSmith
Wed Sep 29 17:04:18 CDT 2004

Anyhow thanks for your help.

regards,

"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:uweZI4mpEHA.3712@TK2MSFTNGP15.phx.gbl...
> "JSmith" <jsmithmitra@yahoo.com> wrote in message
> news:%23d874ympEHA.1668@TK2MSFTNGP14.phx.gbl
> > Igor, Thanks for your quick response
> > You mean there is no difference between copying the structures and
> > placing pointers into the link list ?
> > Won't be that (placing pointer into list) an optimized code ?
>
> What makes you think so? Have you coded it both ways, and then run a
> profiler to prove that one is faster than another? Is this code on the
> critical path in your program, so that it becomes a bottleneck? Have you
> taken into account your own time that you have wasted struggling with
> the problem, that you could instead use to do something productive?
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>



Re: problem with link list by JSmith

JSmith
Wed Sep 29 17:14:37 CDT 2004

I was reviewing your responses and you had pointed to copying a structure
method a "memory leaking". That's why I wan't sure about it.

Thanks,


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:uweZI4mpEHA.3712@TK2MSFTNGP15.phx.gbl...
> "JSmith" <jsmithmitra@yahoo.com> wrote in message
> news:%23d874ympEHA.1668@TK2MSFTNGP14.phx.gbl
> > Igor, Thanks for your quick response
> > You mean there is no difference between copying the structures and
> > placing pointers into the link list ?
> > Won't be that (placing pointer into list) an optimized code ?
>
> What makes you think so? Have you coded it both ways, and then run a
> profiler to prove that one is faster than another? Is this code on the
> critical path in your program, so that it becomes a bottleneck? Have you
> taken into account your own time that you have wasted struggling with
> the problem, that you could instead use to do something productive?
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>



Re: problem with link list by Lord2702

Lord2702
Thu Sep 30 00:54:21 CDT 2004

Igor, It looks you are taking a classroom training ahh.

"JSmith" <jsmithmitra@yahoo.com> wrote in message
news:%23d874ympEHA.1668@TK2MSFTNGP14.phx.gbl...
> Igor, Thanks for your quick response
> You mean there is no difference between copying the structures and placing
> pointers into the link list ?
> Won't be that (placing pointer into list) an optimized code ?
>
> Thanks,
>
>
> "Igor Tandetnik" <itandetnik@mvps.org> wrote in message
> news:etXbmsmpEHA.3712@TK2MSFTNGP15.phx.gbl...
> > "JSmith" <jsmithmitra@yahoo.com> wrote in message
> > news:unUPSompEHA.536@TK2MSFTNGP09.phx.gbl
> > > Ok, how can I store pointers only in to link lists instead of holding
> > > a copy of structure?
> >
> > That would be CList <MyStruct*, MyStruct*> of course. Then you are
> > indeed responsible for manually deleting every node, because the list
> > will only maintain pointers for you. Why do you feel the need to do
> > that?
> > --
> > With best wishes,
> > Igor Tandetnik
> >
> > "On two occasions, I have been asked [by members of Parliament], 'Pray,
> > Mr. Babbage, if you put into the machine wrong figures, will the right
> > answers come out?' I am not able to rightly apprehend the kind of
> > confusion of ideas that could provoke such a question." -- Charles
> > Babbage
> >
> >
>
>



Re: problem with link list by Lord2702

Lord2702
Thu Sep 30 00:53:40 CDT 2004

Is this a class room, I am sorry newsgroup training ?

"JSmith" <jsmithmitra@yahoo.com> wrote in message
news:OwvIBHnpEHA.3464@tk2msftngp13.phx.gbl...
> I was reviewing your responses and you had pointed to copying a structure
> method a "memory leaking". That's why I wan't sure about it.
>
> Thanks,
>
>
> "Igor Tandetnik" <itandetnik@mvps.org> wrote in message
> news:uweZI4mpEHA.3712@TK2MSFTNGP15.phx.gbl...
> > "JSmith" <jsmithmitra@yahoo.com> wrote in message
> > news:%23d874ympEHA.1668@TK2MSFTNGP14.phx.gbl
> > > Igor, Thanks for your quick response
> > > You mean there is no difference between copying the structures and
> > > placing pointers into the link list ?
> > > Won't be that (placing pointer into list) an optimized code ?
> >
> > What makes you think so? Have you coded it both ways, and then run a
> > profiler to prove that one is faster than another? Is this code on the
> > critical path in your program, so that it becomes a bottleneck? Have you
> > taken into account your own time that you have wasted struggling with
> > the problem, that you could instead use to do something productive?
> > --
> > With best wishes,
> > Igor Tandetnik
> >
> > "On two occasions, I have been asked [by members of Parliament], 'Pray,
> > Mr. Babbage, if you put into the machine wrong figures, will the right
> > answers come out?' I am not able to rightly apprehend the kind of
> > confusion of ideas that could provoke such a question." -- Charles
> > Babbage
> >
> >
>
>



Re: problem with link list by Igor

Igor
Thu Sep 30 08:32:19 CDT 2004

"JSmith" <jsmithmitra@yahoo.com> wrote in message
news:OwvIBHnpEHA.3464@tk2msftngp13.phx.gbl
> I was reviewing your responses and you had pointed to copying a
> structure method a "memory leaking". That's why I wan't sure about it.

The copy per se is not a problem. The problem was when you allocated a
structure on the heap, had the list make a copy of it, then promptly
forgot about the original. The list properly manages its copy - you leak
_yours_.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage



Re: problem with link list by JSmith

JSmith
Thu Sep 30 12:07:41 CDT 2004

Sorry, it is bothering you. I'll stop it to make you happy.


"Lord2702" <Lord2702@MSN.com> wrote in message
news:OtjLYHrpEHA.3988@tk2msftngp13.phx.gbl...
> Is this a class room, I am sorry newsgroup training ?
>
> "JSmith" <jsmithmitra@yahoo.com> wrote in message
> news:OwvIBHnpEHA.3464@tk2msftngp13.phx.gbl...
> > I was reviewing your responses and you had pointed to copying a
structure
> > method a "memory leaking". That's why I wan't sure about it.
> >
> > Thanks,
> >
> >
> > "Igor Tandetnik" <itandetnik@mvps.org> wrote in message
> > news:uweZI4mpEHA.3712@TK2MSFTNGP15.phx.gbl...
> > > "JSmith" <jsmithmitra@yahoo.com> wrote in message
> > > news:%23d874ympEHA.1668@TK2MSFTNGP14.phx.gbl
> > > > Igor, Thanks for your quick response
> > > > You mean there is no difference between copying the structures and
> > > > placing pointers into the link list ?
> > > > Won't be that (placing pointer into list) an optimized code ?
> > >
> > > What makes you think so? Have you coded it both ways, and then run a
> > > profiler to prove that one is faster than another? Is this code on the
> > > critical path in your program, so that it becomes a bottleneck? Have
you
> > > taken into account your own time that you have wasted struggling with
> > > the problem, that you could instead use to do something productive?
> > > --
> > > With best wishes,
> > > Igor Tandetnik
> > >
> > > "On two occasions, I have been asked [by members of Parliament],
'Pray,
> > > Mr. Babbage, if you put into the machine wrong figures, will the right
> > > answers come out?' I am not able to rightly apprehend the kind of
> > > confusion of ideas that could provoke such a question." -- Charles
> > > Babbage
> > >
> > >
> >
> >
>
>