|
From: | Benjamin Kowarsch |
Subject: | Re: Passing parameters of type 'ARRAY[0..MAXINDEX] OF ElementType' to C |
Date: | Tue, 30 Jan 2024 19:12:03 +0900 |
Benjamin,
can you have a look on the solution I provided in my last mail - I think the "high" parameter is comming after the actual value. Maybe I am wrong,
anyhow, worth checking.
Gruß
Michael
Am 28.01.24 um 18:04 schrieb Benjamin Kowarsch:
Gaius will probably answer this with the specific parameter passing and foreign function interfacing conventions used by GM2, but I would like to recommend a different approach which is to use open array parameters.The actual number of array components of an open array parameter is passed as a hidden parameter before the open array parameter.
PROCEDURE P ( str : ARRAY OF CHAR );
is actually
PROCEDURE P ( count : CARDINAL; str : ARRAY OF CHAR );
and in C
void p ( unsigned argc, char str[] );
This convention is pretty universal for most if not all Modula-2 compilers and thus its use is portable.
When using specific array types, varying parameter passing conventions will apply depending on the compiler and even use cases, for example, a compiler or its optimising back-end may decide to pass fixed arrays exceeding a certain size by reference even if they aren't VAR parameters. The code will then become more difficult to maintain and non-portable.
regardsbenjamin
On Sun, 28 Jan 2024 at 22:03, Rudolf Schubert <rudolf@muc.de> wrote:
High Gaius,
I'm facing a problem when trying to pass a parameter of typ String256
to C, which I've defined like this:
CONST
len256= 256;
TYPE
String256= ARRAY[0..len256-1] OF CHAR;
In the appended example I've set up three DEFINITION MODULEs
(unix1.def, unix2.def, unix3.def) each with a corresponding
C file (unix1.c, unix2.c, unix3.c). There is only one simple
function in these C files which should print out the length
of the passed string and the string proper.
unix1.def does NOT have 'FOR "C"' whereas unix2.def and unix3.def
do have a 'FOR "C"' in the MODULE definition. In unix1.def and unix2.def
the PROCEDURE ParamTest has got (my_str: String256) but in unix3.def
I'm using 'varargs' (...).
I'm seeing that only unix3 does give the expected results. unix1 and
unix2 do either crash the programm or return wrong results.
I do observe similar behaviour not only for
type 'ARRAY[0..MAXINDEX] OF CHAR'
but for instance also for
type 'ARRAY[0..MAXINDEX] OF CARDINAL'
Now my question is: how would I pass parameters of this type to
C functions? I would suspect that this should not only work for
'varargs' but also in the other cases? When using open ARRAYs in
the PROCEDURE things are also working fine. It's just when using
'non open' ARRAYs that the problem occurs.
BR
Rudolf
--
Rudolf Schubert \
Kirchstr. 18a \ mailto:rudolf@muc.de
82054 Sauerlach > http://www.dose.muc.de
Deutschland / (alpine archkiste)
Tel. 08104/908311 /
[Prev in Thread] | Current Thread | [Next in Thread] |