Solved

variable size arrays

Posted on 1998-06-03
6
276 Views
Last Modified: 2010-04-06
In my program I am using an array[0..9,0..9] of Extended. But I would like to have the user to determine the size of the array, instead of pre-defining it.
It doesn't have to be an array. It can be a pointer or something that can be accessed as a bi-dimensional array, but with the size determined at run-time.
0
Comment
Question by:acerola
6 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1350132
0
 
LVL 1

Author Comment

by:acerola
ID: 1350133
I tried that solution. It locked my machine. Than I took that idea, but used GetMem instead of that weird memory allocation procedure. It worked, but the program itself didn't. When I used an array larger than 4x4 the calculation result was always zero. Maybe it's because my program calls a function recursively.

Does anyone have any other suggestion?

Raised the points to 50.
0
 

Expert Comment

by:michael2010898
ID: 1350134
use variant type
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:acerola
ID: 1350135
This answer is way too generic. Be more specific (e.g. give me an source code). Just changing the variable type from extended to variant will not solve the problem.
0
 

Accepted Solution

by:
descikartus earned 50 total points
ID: 1350136
Try the following code:

program Project1;

uses
  SysUtils, Dialogs;

{$R *.RES}

type
  PDouble = ^Double;

// This will get a value from the array.
// All you have to do is pass the array, the width of
// the array, and the location within it that you want.
// This function assumes a two dimentional array, modify
// at will.
function GetDouble(const AD : array of Double; W, X, Y : Integer) : Double;
begin
  Result := AD[Y * W + X];
end;

// This works similar to the function above, except
// you are also passing it a value.
procedure SetDouble(PD : PDouble; W, X, Y : Integer; Value : Double);
begin
  PDouble(
      Integer(PD) +
      (Y * W * SizeOf(Double) + X * SizeOf(Double))
    )^ := Value;
end;

// Just some variable to show you how it works
var
  PADouble : PDouble;
  W, H : Integer;
  DoubleVar : Double;

begin

  // Set up our dynamic array
  W := 9;
  H := 9;
  GetMem(PADouble, H * W * SizeOf(Double));

  // Assign a value within it
  SetDouble(PADouble, W, 5, 5, 3.14159265);

  // Get the value back
  DoubleVar := GetDouble(PADouble^, W, 5, 5);

  // Display our value
  ShowMessage(FloatToStr(DoubleVar));

end.

Hopefully this helps.

0
 
LVL 1

Author Comment

by:acerola
ID: 1350137
Works just perfectly.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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