Variable sized array

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
red_2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

julio011597Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
julio011597Commented:
Sorry, as you can see, there's just _one_ example.
0
ronit051397Commented:
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
julio011597Commented:
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
JimBob091197Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.