Solved

SetLength and pointers

Posted on 2003-10-30
4
355 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You're welcome.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

10 Experts available now in Live!

Get 1:1 Help Now