Solved

Variable sized array

Posted on 1998-01-13
5
133 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
Comment Utility
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
Comment Utility
Sorry, as you can see, there's just _one_ example.
0
 
LVL 5

Expert Comment

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

14 Experts available now in Live!

Get 1:1 Help Now