gm2
[Top][All Lists]
Advanced

[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: Michael Riedl
Subject: Re: Passing parameters of type 'ARRAY[0..MAXINDEX] OF ElementType' to C
Date: Mon, 29 Jan 2024 13:39:06 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:102.0) Gecko/20100101 Thunderbird/102.11.0

Hallo all,

attached my "ugly but running" solution. Workes with XDS M2 and GM2 on my machine - it is similar to unix4 solution proposed,

which I saw after I made my own one ...

Only some safeguards against buffer overflow (which might need further work) are added and I do not path the high value within a record ...

as long as the char array passed does not exceed the limit (see unix8.c" it may work :-)

Gruß

Michael

Am 29.01.24 um 09:47 schrieb Gaius Mulley:
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

Attachment: paramtest.tgz
Description: application/compressed-tar


reply via email to

[Prev in Thread] Current Thread [Next in Thread]