[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Passing parameters of type 'ARRAY[0..MAXINDEX] OF ElementType' to C
From: |
Gaius Mulley |
Subject: |
Re: Passing parameters of type 'ARRAY[0..MAXINDEX] OF ElementType' to C |
Date: |
Mon, 29 Jan 2024 08:47:25 +0000 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) |
Rudolf Schubert <rudolf@muc.de> writes:
> 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
Hi Rudolf and Benjamin,
Benjamin Kowarsch <trijezdci@gmail.com> write:
> but I would like to recommend a different approach which is to use
> open array parameters.
indeed I agree - it makes for easier, cleaner and safer code (and it
also allows "hello world" to be passed by address without requiring a
temporary variable). I guess sometimes this is not an option though.
Thanks for the code Rudolf - all very useful - I've added:
unix4: an example of unbounded array.
unix5: pass by value (for string256).
unix6: pass by address (for string256). Interesting to note that
"Hello world" cannot be
passed in this example.
to the original tarball for reference,
hope this helps? Probably this should be written up in the
documentation
regards,
Gaius
wrk_gm2_16-u456.tgz
Description: wrk16 with unix456 examples