Solved

SetLength and pointers

Posted on 2003-10-30
4
357 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now