Solved

Dynamic arrays from pointer

Posted on 2002-03-07
3
190 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:bnz
3 Comments
 
LVL 1

Accepted Solution

by:
SchweizerD earned 100 total points
Comment Utility
I am assuming you can't use open arrays here. In that case you're restricted to typecasting:

type
   TIntArray = array[1..1000] of integer;
   PIntArray = ^TIntArray;
var
    SharedMem: PIntArray;
begin
   SharedMem := PIntArray(...)  ;
// substitute your pointer, assuming you have it, for the three dots

   // now:
   SharedMem^[1] := 12;
   if SharedMem^[1] = 10 then
// etc.

HTH
David
   
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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.
0
 

Author Comment

by:bnz
Comment Utility
Ahh clever :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

6 Experts available now in Live!

Get 1:1 Help Now