Link to home
Start Free TrialLog in
Avatar of bnz
bnz

asked on

Dynamic arrays from pointer

I have got a pointer to a shared memory area.
In this area there is stored lets say 10 succesitive integers.
Now I want to access each of these integers in an array like syntax.

something like this

if sharedmem[1] = 10 then ..

Is that possible, I don't know the numbers at designtime. So it should be dynamic.
ASKER CERTIFIED SOLUTION
Avatar of SchweizerD
SchweizerD

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Madshi
Madshi

Yep, David is right. In my type definition base unit I'm declaring these types (and some more) for my convenience:

type
  // TAxxx = array [0..maxPossible] of xxx
  TAByte         = array [0..maxInt      -1] of byte;
  TAShortInt     = array [0..maxInt      -1] of shortInt;
  TAChar         = array [0..maxInt      -1] of char;
  TABoolean      = array [0..maxInt      -1] of boolean;
  TAExtBool      = array [0..maxInt      -1] of TExtBool;
  TAWord         = array [0..maxInt shr 1-1] of word;
  TASmallInt     = array [0..maxInt shr 1-1] of smallInt;
  TACardinal     = array [0..maxInt shr 2-1] of cardinal;
  TAInteger      = array [0..maxInt shr 2-1] of integer;
  TAPointer      = array [0..maxInt shr 2-1] of pointer;
  TAString       = array [0..maxInt shr 2-1] of string;
  TAIUnknown     = array [0..maxInt shr 2-1] of IUnknown;
  TAInt64        = array [0..maxInt shr 3-1] of int64;

  // TPAxxx = ^(array [0..maxPossible] of xxx)
  TPAByte        = ^TAByte;
  TPAShortInt    = ^TAShortInt;
  TPAChar        = ^TAChar;
  TPABoolean     = ^TABoolean;
  TPAExtBool     = ^TAExtBool;
  TPAWord        = ^TAWord;
  TPASmallInt    = ^TASmallInt;
  TPACardinal    = ^TACardinal;
  TPAInteger     = ^TAInteger;
  TPAPointer     = ^TAPointer;
  TPAString      = ^TAString;
  TPAIUnknown    = ^TAIUnknown;
  TPAInt64       = ^TAInt64;

Now I can always use something like the following without ever getting an out-of-bounds exception:

  if TPAInteger(sharedMemoryPointer)^[index] = blabla then ...

Regards, Madshi.
Avatar of bnz

ASKER

Ahh clever :)