Solved

Variable sized array

Posted on 1998-01-13
5
140 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
[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
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses
Course of the Month6 days, 23 hours left to enroll

622 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