How to use 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?

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.

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.

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

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

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:

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

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

.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

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

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…