Solved

Variable sized array

Posted on 1998-01-13
5
134 Views
Last Modified: 2010-04-04
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
Comment
Question by:red_2
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 100 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

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

Expert Comment

by:ronit051397
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

by:julio011597
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

by:JimBob091197
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2010 Export to pdf 2 285
FMX enumerated colours 2 84
Can Viruses spread while transferring files via FTP protocol to unshared NAS drives ? 6 83
find a node in VST 2 63
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

22 Experts available now in Live!

Get 1:1 Help Now