Archived

This topic is now archived and is closed to further replies.

How would I go about....

This topic is 5868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

passing a multidimensional array to a function or procedure if I didn''t know the how the array was defined or declared (other than the base type)? In other words, to basically write a third party utility for programmers, that would say take any two dimensional array of integers and set the each element to zero. Keep in mind I have no type to associate my array with, nor do I know how the programmer implemented it. Is there a way to do this in Delphi, pointers maybe? I''ve been searching for a way to do this for a while now... and well I''m still searching.

Share this post


Link to post
Share on other sites
If it is for Delphi programmers only, then you can use the Low() and High() functions to give you the upper and lower bounds of the array.

I'm not sure if this will work (it's off the top of my head)...

procedure ZeroArray(Data: array of array of Integer);
var
k, j: Integer;
begin
for k := Low(Data) to High(Data) do
begin
for j := Low(Data[k]) to High(Data[k]) do
begin
Data[k, j] := 0;
end;
end;
end;

Hint: Never use i as an array index when providing sample code on this forum. The forum software thinks it means italics.

Steve 'Sly' Williams   Monkey Wrangler  Krome Studios
turbo game development with Borland compilers

Edited by - sly on November 18, 2001 5:57:09 PM

Share this post


Link to post
Share on other sites
See that''s just it, that doesn''t work. I''ve tried passing a two dimensional array parameter using array of array of but it won''t let you. It generates the following error:

Identifier expected but ''ARRAY'' found

Open array parameters seem to only work for single dimension arrays, multidimensional arrays always want a specific user defined type. Which is where I run into my problem. I mean if I just used array of rather than array of array of I get an error during any access or assignment within the procedure with the compiler telling me I need an array type? They just couldn''t make this easy could they...

AAAAAAAAAARRRRRGGGGGGGGGGGGHHHHHHHH!!!!!!!!!!!!!

Share this post


Link to post
Share on other sites
Hello there,

Just pass a pointer to it ie

procedure Process2DArray(p2DArray: Pointer);
var
aArray: array of array of integer;
begin
aArray := p2DArray;

//do array stuff here
end;

then to call just use

procedure TestArray;
var
aArray: array of array of integer;
begin
Process2DArray(aArray);
end;

Note that you dont pass the pointer to the array
as an array is already a pointer (dont use the @ symbol
before the array identifier eg Process2DArray(@aArray) as
this will not work ).

Hope it helps.

Mark

Share this post


Link to post
Share on other sites
Another solution:

type MyArray = Array of Array of Integer;

function MyFunction(Arry: MyArray);
begin
Arry[0, 0]:= -1;
end;


- Arcane Lifepower -

"Although the world would call me free
Each day the more her slave am I
For in her very way to be
There''s I don''''t know what, I don''t know why"

Share this post


Link to post
Share on other sites
MButchers: that only works when passing an array of array of integer to the procedure. Try it with array[1..10] of array[1..10] of integer , and you''ll get:

quote:
Origional quote by my compiler
Incompatable types: ''Array'' and ''Pointer''



The procedure that Xorcist wants needs to be able to work with ALL 2D arrays of integers, no matter how they were declaired.

~ There''s no substitute for failure ~

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Lifepower: we''ve ran into the same problem here. Now, any variables passed to the procedure MUST be array of array of integer , and nothing else:

quote:
Origional quote by my compiler
Incompatable types: ''Array'' and ''MyArray''



~ There''s no substitute for failure ~

Share this post


Link to post
Share on other sites
Would it be possible to use variants to solve this problem? I''ve never been big on using them, and thus don''t know a whole lot about how they work and what would be required, but it''s a thought.

P.S. Exactly how are multidimensional arrays structured in Delphi? Are they completely linear and contiguous in memory? I''m just wondering if I could just take in the initial address of the array as a pointer, the size of a single element, and the size of the array, and manually index myself through the memory... sure it would be more work for me, but at this point anything I can get to work I''ll take...

Share this post


Link to post
Share on other sites
But consider I''m not always going to be zero-ing out my entire array. I may be attempting to populate the entire array or even display it... what then? FillMemory, to my understanding, takes in a byte pattern which it uses to fill a block of memory. Based on that I would have to, taking an integer as an example, break a 32bit datatype into 4 bytes, and pass each of those bytes one at a time through FillMemory making sure I increment the pointer properly each time to fill a specific element of the array (which largely depends on how multidimensional arrays are structured in memory through Delphi, where exactly does [5,3] reside from the initial array pointer?). It''s a good starting place, and I''ll give it a try, but I''ve still got a ways to go.

Share this post


Link to post
Share on other sites
quote:
Original post by Sly
That''s an easier way to do it. Just set a memory block to zero using ZeroMemory().


Does that work? I tried it with an array of array of integer and it destroyed the lengths of each dimension, not actually changing it''s values to 0. Of course, if the size was recorded beforehand, it can then be re-set afterwards...can''t it?



~ There''s no substitute for failure ~

Share this post


Link to post
Share on other sites
There''s a good reason to use good old blocks of memory instead of dynamic arrays. You can do whatever you want to a block of memory and it won''t destroy itself.

I think trying to make it so generic is asking a bit too much. If they are dynamic arrays then you have the problem with the hidden data (length and reference count) being overwritten. Being a two-dimensional array, you need to know the size of each dimension. You have to know something about how it was declared.

Steve ''Sly'' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers

Share this post


Link to post
Share on other sites
See that''s the problem! Why is delphi set up to handle open array parameters if and only if they are of a single dimension! Hell this would be a no brainer if they implemented extended open array parameters that could be issued as array of array of up to the required dimension. Am I just looking at this problem wrong? I mean I could predefine a dynamic array type and require them to use it, when defining any array that will word with my utility functions. I''d have to write some extra utility functions to abstract the dynamic arrays from my users, so those who don''t fully understand them could still work with them. But I guess those are the wages of war. Well thanks for the time guys, if nothing else it has been a good "think tank" session.

Share this post


Link to post
Share on other sites