Solved

SetLength and pointers

Posted on 2003-10-30
4
360 Views
Last Modified: 2012-05-04
I'll try and confuse everyone as little as possible here.

I have a record. It is called email_info. This record is basically an e-mail account.
I have email_accounts which is an array of email_info.

I add new accounts to a list (similar to a treeview i guess.) when the user mouses over, they see a tooltip (hint) containting data from that account. i.e. new messages, etc..

Problem. When I add a new record (account) I call SetLength(email_accounts, Length(email_accounts) +1)

this moves my array to a different location in memory. so when i add a new account, the accounts above it don't know where to grab their data. so i usually get some funky stuff in there.

what would solve this? a pointer to the pointer that's already in the tooltip?

this is probably VERY confusing, sorry :)
0
Comment
Question by:gwarguitar
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
j42 earned 75 total points
ID: 9656238
I can see two ways:

1. Don't store the address of your record but the index in the array instead
2. Don't use records at all. Define a class to store your email info and
   use a TObjectList as container.
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9656314
Hi,

I basically second j42 here, but I would add some insight on the matter.

First of all, we've no clue of the record structure( yes, it matters ) and I
think you're using LongStrings in it, right? <g>

If I was you, in order to have a safe move, I'd do this:

procedure ExpandEmailArray( SrcArr : array of TEmailInfo;Delta : Integer;var DstArr : array of TEmailInfo );

var SrcLen : Integer;
      Elem_Idx : Integer;
begin
   SrcLen := Length( SrcArr );
   SetLengthDstArr( DstArr,Delta );
   for Elem_Idx := 0 to Pred( SrcLen ) do
   begin
      DstArr[ Elem_Idx ] := SrcArr[ Elem_Idx ];
   end;
end;

Anyway, an OO approach would definitely better.

HTH,

Andrew
0
 

Author Comment

by:gwarguitar
ID: 9679659
1. Don't store the address of your record but the index in the array instead

that put me on the right track, thanks, guys.
0
 
LVL 2

Expert Comment

by:j42
ID: 9679759
You're welcome.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question