Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Variable sized array

Posted on 1998-01-13
Medium Priority
141 Views
I'm writing a screensaver that's showing some stars flying across the screen. I use an array to represent the stars, and the length of array is the number of stars on the screen.
Is there a way to use an integer value from the registry to set the size of the array, so that by manipulating the screensavers properties one would be able to choose the number of stars?
I've only gotten the message that the array limits must be constant expressions, and therefore included in the program itself.

Alternative solutions would be appreciated

Rune Berge
0
Question by:red_2
[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
• 3

LVL 5

Accepted Solution

julio011597 earned 400 total points
ID: 1356370
Dynamic allocation (i.e. runtime allocation) can be made in basically two ways:

1. Delphi specific (and easier): use a Variant Array;
2. use Pointers (if you are familiar with them).

Here are a couple of examples:

--//--
{ using a VarArray }

var
Size: Integer; { get it from the Registry or wherever you want }
MyArray: Variant; { here is the array to be created }

procedure InitArray;
var
i: Integer;
begin
MyArray := VarArrayCreate([0, Size-1], varInteger); { a zero-based array, but could be [1, Sized] or anything else }

{ an example of use }
for i:=0 to Size-1 do MyArray[i] := i;
end;
--//--

Using pointers is a bit more complicated; morover Delphi does support char pointer arithmetic only, so you need to set up your integer pointer arithmetic.
Anyway, if performance is very important for your application, i could give you some code.

Let me know, julio

0

LVL 5

Expert Comment

ID: 1356371
Sorry, as you can see, there's just _one_ example.
0

LVL 5

Expert Comment

ID: 1356372
Yes, agreed, you could do it with a TList.

IMO, the point, anyway, is that a TList not only still involves pointers, but, for this particular problem, the size of the array, after initialization, never changes; so, as far as pointer are a chance, dynamic allocation with GetMem() (or related stuff) is quite more efficient and quite less resource consuming.

Anyway, choosing among implementations, often, is a matter of style and a matter of how critical speed and resource consumption are for your particular application.
0

LVL 5

Expert Comment

ID: 1356373
Julio is right: "choosing among implementations, often, is a matter of style and a matter of how critical speed and resource consumption are for your particular application"

We could probably find another 10 methods to accomplish the task, but let me throw in one just for fun.  The method below allows the user to create an array at run-time (similar to julio's variant array, but MUCH faster for big arrays).

type
TStar = record
X, Y: Integer;
Color: TColor;
end;

TDynArr = array[0..0] of TStar;
PDynArr = ^TDynArr;

procedure TForm1.Button1Click(Sender: TObject);
var
Ix, NumStars: Integer;
hMem: THandle;
MyArray: PDynArr;
begin
// Get this from registry, or whereever...
NumStars := 5000;

// Allocate space for stars.
hMem := GlobalAlloc(GMEM_FIXED, NumStars * SizeOf(TStar));
MyArray := GlobalLock(hMem);

// Set data for element #123
Ix := 123;
MyArray^[Ix].X := 5;
MyArray^[Ix].Y := 10;
MyArray^[Ix].Color := clBlue;

// Do other stuff...

// Free memory.
GlobalUnlock(hMem);
GlobalFree(hMem);
end;

To be honest, Julio's original suggestion is the simplest, cleanest solution, and it makes sense if speed is not critical.  (It also depends on the version of Delphi.  I don't think Delphi 1 supports variants.)

Regards,
JB
0

LVL 5

Expert Comment

ID: 1356374
And, just for fun, i'll post an example of the kind of wrappers i usually use to make my life easier with dynamic arrays (under Delphi 2).

BTW, one would write a version of, say, the following "TIntMemH" class for each type of data to handle - e.g. a TFloatMemH, etc. -; sometimes i wonder why Delphi has not template classes as C++.

--//--
unit Support;

interface

uses SysUtils;

type
EMemHOutOfBounds = class(Exception)
public
constructor Create(const Idx, Low, High: Integer);
end;

TIntMemH = class
private
FSize: Integer;
FP: PChar;
function GetItem(const Idx: Integer): Integer;
procedure SetItem(const Idx, Value: Integer);
public
constructor Create(const Size: Integer);
destructor Destroy; override;
property Items[const Idx: Integer]: Integer read GetItem write SetItem; default;
end;

implementation

{ EMemHOutOfBounds }

constructor EMemHOutOfBounds.Create(const Idx, Low, High: Integer);
begin
inherited CreateFmt('MemH: acces out of bounds' + #13 + #10 + '%d is not in [%d..%d]', [Idx, Low, High]);
end;

{ TIntMemH }

const
SOI = SizeOf(Integer);

type
PInteger = ^Integer;

constructor TIntMemH.Create(const Size: Integer);
begin
FSize := Size;
GetMem(FP, Size * SOI);
end;

destructor TIntMemH.Destroy;
begin
FreeMem(FP);
inherited;
end;

function TIntMemH.GetItem(const Idx: Integer): Integer;
begin
if (Idx < 0) or (Idx >= FSize) then
raise EMemHOutOfBounds.Create(Idx, 0, FSize-1);
Result := PInteger(FP + Idx * SOI)^;
end;

procedure TIntMemH.SetItem(const Idx, Value: Integer);
begin
if (Idx < 0) or (Idx >= FSize) then
raise EMemHOutOfBounds.Create(Idx, 0, FSize-1);
PInteger(FP + Idx * SOI)^ := Value;
end;

end.
--//--

And, a sample of use (just to show it all):
--//--
uses Support;

var AIntArray: TIntMemH;

procedure ArrayUse(const Size: Integer);
var i: Integer;
begin
AIntArray := TIntMemH.Create(Size);
for i:=0 to Size-1 do AIntArray[i] := i;
AIntArray.Free;
end;
--//--

I guess this approach should be very efficient, both in terms of resources and speed; let me know if you see any drawbacks.

Cheers.
0

Featured Post

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…