• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

SetLength and pointers

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
gwarguitar
Asked:
gwarguitar
  • 2
1 Solution
 
j42Commented:
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
 
DeerBearCommented:
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
 
gwarguitarAuthor Commented:
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
 
j42Commented:
You're welcome.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now