Solved

How to use arrays

Posted on 1998-07-26
12
343 Views
Last Modified: 2010-04-04
Proper usage of arrays:

Can someone confirm whether this is the proper way to use arrays in Delphi?

I am concerned about a few things:

1) Must I 'Free' my array type before the Application terminates?

2) When I '.Clear' my array (see below), what are the new values of the array?  In the case of a Boolean array, do all the items in the array turn to "False" or to "nil"?  If this was an Integer array, would would all the cleared values become?  Zero?  How about strings?

Are there any other things I need to be concerned about when using Arrays in this manner?

Thanks.


type
      TMarkedForDeletion = array[0..512] of Boolean;

var
      MessageMarkedForDeletion : TMarkedForDeletion;

implementation

{$R *.DFM}

procedure EraseAll;
begin
      MessageMarkedForDeletion.Clear;
end;

procedure MarkThisDelete(Record : Integer);
begin
      MessageMarkedForDeletion[Record] := True;
end;
0
Comment
Question by:Monroe406
  • 3
  • 2
  • 2
  • +5
12 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1358707
I'm not exactly sure what you are trying to get but here is something that might help you...
Actually you can not write
procedure EraseAll;
begin
   MessageMarkedForDeletion.Clear;
end;

What you can do is like this...
procedure EraseAll;
var RecordIndex: integer;
begin
  for RecrodIndex := 0 to 512 do
    MessageMarkedForDeletion[RecordIndex] := false;
end;

Viktor Ivanov
0
 

Author Comment

by:Monroe406
ID: 1358708
OK, you answered #2, I guess.  Evidently there's no way to reset the entire array back to nil or zero or whatever.  Correct?  Also, using...

for RecordIndex := 0 to High(MessageMarkedForDeletion) do...

.be a better way of handling this clearing function?

How about question #1?  Will the application free up the array upon termination of the app?

Thanks.
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1358709
By you declaring an array[0..n] of typeX, you are actually asking the computer to allocated n * sizeof(typeX) byte of memory space (statically) for the array.  The memory space will be released automatically when you exit the block in which the array is declared.

The initial value of an array is undefined (ie whatever data previously stored in that location). Therefore you need to initilalise the value of each element as per your comment above.

You do not need to clear the array. It is different from pointer, tlist or TstringList where you need to clear and freed the object.
 
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1358710
Correct, you DO NOT need to free the memmory of an array, thereby when making an array (allocating memmory), the data in this array will be random (memmory crap...)... see explenation kjteng and viktornet
Zif.
0
 
LVL 3

Expert Comment

by:vladika
ID: 1358711
> Evidently there's no way to reset the entire array back to nil or zero or  whatever.

Try it
FillChar(MessageMarkedForDeletion, SizeOf(MessageMarkedForDeletion), 0);

0
 
LVL 3

Expert Comment

by:vladika
ID: 1358712
Integer    Boolean
 =0        False
<>0        True

0
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.

 
LVL 6

Expert Comment

by:Holger101497
ID: 1358713
>Are there any other things I need to be concerned about when using Arrays in this manner?

Not really, except for the fact that you cannot use arrays in this manner *g* (see above)

I recommend taking a look at the Delphi/Pascal (not much has changed for arrays since the "good old Pascal days) documentation. I guess you've been programming Java before?

In Delphi/Pascal, arrays are NOT objects! They are simply a larger number of elements that can be addresses by using an index. Because the adresses of these objects are contiguous in memory, you can use "FillChar" to reset all of them to 0, but that won't work if you try to reset an array of integers to 10, for example. FillChar would enter a 10 into every byte of the integers which would result in some big number (*g* - don't have a calculator handy).

You can think of arrays as very large variables. As long as you don't use pointers to arrays and dynamically allocate memory, you don't need to free it either. An Array of integer works just like an integer...
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1358714
Since you use such a large array, my humble assumption is that you might have oversized it in order to be on the safe side. If you haven't, you can stop reading here, otherwise; to use an array dynamically, take the following steps:

1. Declare appropriate array types:
    type
      PMyBoolArray = ^TMyBoolArray; // yes, it is legal to put this first
      TMyBoolArray = array[ 0..0 ] of Boolean;
2. Declare an array variable:
    var
      prgbDeletion: PMyBoolArray;
3. Allocate the appropriate amount of memory:
    GetMem( prgbDeletion, cFiles * SizeOf( Boolean ) ); // bytes, not values!
    AllocMem( prgbDeletion, cFiles * SizeOf( Boolean ) ); // bytes, not values, initialised to 0(False), goto section 5
4. Initialise the array (only if allocated with GetMem):
    FillChar( prgbDeletion^, cFiles * SizeOf( Boolean ), 0 ); // don't forget "^"!
5. Use it:
    prgbDeletion^[ 5 ] := True;
    if ( prgbDeletion^[ 5 ] ) then ...

    Resizing:
    ReAllocMem( prgbDeletion, NewSize * SizeOf( Boolean ) );
    if ( NewSize > cFiles ) then FillChar( Pointer( Integer( prgbDeletion ) + cFiles * SizeOf( Boolean ) )^, ( NewSize - cFiles ) * SizeOf( Boolean ), 0 ); // don't forget to init new values!

6. Free memory:
    FreeMem( prgbDeletion );

Regards,
/// John
0
 

Author Comment

by:Monroe406
ID: 1358715
Reply to: kjteng

>>By you declaring an array[0..n] of typeX, you are actually asking the computer
>> to allocated n *  sizeof(typeX) byte of memory space (statically) for the array.
>>The memory space will be released automatically when you exit the block in
>> which the array is declared.

I think my problem is that I was treating this type of array as DYNAMIC, when in fact it is a static array.

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1358716
Well, something similar. If the array isn't dynamic created then you don't need to free it up, the same is in your case... C'Ya...

Regards,
Viktor Ivanov
0
 

Author Comment

by:Monroe406
ID: 1358717
Reply to: kjteng

>>By you declaring an array[0..n] of typeX, you are actually asking the computer
>> to allocated n *  sizeof(typeX) byte of memory space (statically) for the array.
>>The memory space will be released automatically when you exit the block in
>> which the array is declared.

I think my problem is that I was treating this type of array as DYNAMIC, when in fact it is a static array.

0
 

Accepted Solution

by:
donaldw earned 50 total points
ID: 1358718
Hello, Monroe,

Your proposed type declaration:

>>type
TMarkedForDeletion = array[0..512] of Boolean;

and your variable declaration

>>var
MessageMarkedForDeletion : TMarkedForDeletion;

.simply declare one instance of an  array variable of the specifications you give.  

- There are _no_ methods associated with the 'MessageMarkedForDeletion'  variable. No .Clear, no .Free. This will be true for any such array or record declaration in Delphi.

- MessageMarkedForDeletion will always contain 512 elements of boolean type. It is up to you to keep track of the number of elements actually in use in your app.

- A 'var' declaration of MessageMarkedForDeletion at the unit level will be cleared to binary 0 (false values) automatically by Delphi at program startup. This variable will disappear automatically when the program exits with no action required by you.

- A 'var' declaration of MessageMarkedForDeletion at the function level will contain indeterminate values upon function entry. This variable will disappear automatically when the function exits.

>> 1) Must I 'Free' my array type before the Application terminates?

No, see above.

>> 2) When I '.Clear' my array (see below), what are the new values of the array?  

There is no .Clear method. You must loop through the elements yourself.

>> Are there any other things I need to be concerned about when using Arrays in this manner?

'Virtually' none, except that you do use 512 bytes anywhere this type is used to declare a variable --- in a function as a local variable, will consume a finite resource called 'the stack'. Heavily nested function calls that declare such large variables may crash your program because the stack becomes exausted. Otherwise,  all such memory management is delegated to the mechanism of Pascal itself.

Don
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

Title # Comments Views Activity
Delphi OLE Error 8 95
Virtuailstring tree compare node issue 14 105
Downloading email attachments 2 67
How to renew a Delphi rad-studio licence? 5 49
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 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

23 Experts available now in Live!

Get 1:1 Help Now