rutledgj
asked on
question about pointers
Suppose I have a record like such:
Type
pMsgRec = ^msgRec;
msgRec = record
msgID:array [0..23] of char;
msgType: String;
end;
Now suppose I have a TList that I want to add msgRec to.
Lets say in a program I assign some values to msg:
msg:pMsgRec;
messages : TStringList;
for i:= 0 to 4 do
begin
New(msg); //Do I need to do this?
strcopy(msg.msgID,'Some info');
msg.msgType := 'NEW';
messages.add(msg);
Dispose(msg); //Do I need this?
end;
Now lets say I want to get a message back out
for i:= 0 to messages.count-1 do
begin
msg := messages.item(i);
//do some work
msg := nil; // Do I need this?
end;
I'm confused on
1. after insertion into the tlist I have a list of pointers. Where does the actual record exist? (I assume it exists since I used New()).
2. If I dispose(msg) does the item placed in the list no longer point to a memory location? Or if I'm not suppose to dispose(msg), does the next iteration through the loop assign it to another record and the previous one is lost?
3. When reading it out do I need to set it to nil before getting the next record out of the list?
4. Why use pointers at all. Can't I just fill the TList with records?
Thanks,
Rut
Type
pMsgRec = ^msgRec;
msgRec = record
msgID:array [0..23] of char;
msgType: String;
end;
Now suppose I have a TList that I want to add msgRec to.
Lets say in a program I assign some values to msg:
msg:pMsgRec;
messages : TStringList;
for i:= 0 to 4 do
begin
New(msg); //Do I need to do this?
strcopy(msg.msgID,'Some info');
msg.msgType := 'NEW';
messages.add(msg);
Dispose(msg); //Do I need this?
end;
Now lets say I want to get a message back out
for i:= 0 to messages.count-1 do
begin
msg := messages.item(i);
//do some work
msg := nil; // Do I need this?
end;
I'm confused on
1. after insertion into the tlist I have a list of pointers. Where does the actual record exist? (I assume it exists since I used New()).
2. If I dispose(msg) does the item placed in the list no longer point to a memory location? Or if I'm not suppose to dispose(msg), does the next iteration through the loop assign it to another record and the previous one is lost?
3. When reading it out do I need to set it to nil before getting the next record out of the list?
4. Why use pointers at all. Can't I just fill the TList with records?
Thanks,
Rut
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.