?
Solved

How to use arrays

Posted on 1998-07-26
12
Medium Priority
?
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

800 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