Solved

How to use arrays

Posted on 1998-07-26
12
339 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

13 Experts available now in Live!

Get 1:1 Help Now