How to use arrays

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;
Monroe406Asked:
Who is Participating?
 
donaldwConnect With a Mentor Commented:
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
 
viktornetCommented:
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
 
Monroe406Author Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
kjtengCommented:
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
 
ZifNabCommented:
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
 
vladikaCommented:
> 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
 
vladikaCommented:
Integer    Boolean
 =0        False
<>0        True

0
 
Holger101497Commented:
>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
 
erajojCommented:
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
 
Monroe406Author Commented:
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
 
viktornetCommented:
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
 
Monroe406Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.