[Gambas-user] external functions and structs

gen braga genbraga1 at gmail.com
Thu Nov 29 16:36:11 CET 2018


Yes, i spent the last few hours debugging the lib's sources with gdb and
everything was ok inside it. I saw the problem you pointed only within
gambas debugger.

The point is, the array*  "char bits[SIXBIT_LEN]"* inside sixbit structure
is filled by a pointer with some characters, from the second string
parameter passed to the method called inside Gambas.

The array looks Ok from position 8 on. Position 0 to 7 receive garbage from
the argument's previous characters. As you said, misalignment issues...

I'm not very experienced in this matter but will try to tweak the lib's
sources. Do you have any suggestion?

Thanks in advance,

Em qui, 29 de nov de 2018 às 11:01, Benoît Minisini <g4mba5 at gmail.com>
escreveu:

> Le 28/11/2018 à 13:39, gen braga a écrit :
> > Hi there,
> >
> > I'm trying to use a method from an external library whose signature is
> > as follow
> >
> > int __stdcall assemble_vdm( ais_state *state, char *str )
> >
> > ais_state is:
> >
> > #define SIXBIT_LEN   255
> >
> > typedef struct {
> >      unsigned char msgid;               //!< Message ID 0-31
> >      unsigned int  sequence;            //!< VDM message sequence number
> >      unsigned int  total;               //!< Total # of parts for the
> > message
> >      unsigned int  num;                 //!< Number of the last part
> stored
> >      char          channel;             //!< AIS Channel character
> >      sixbit        six_state;           //!< sixbit parser state
> > } ais_state;
> >
> > six_state is:
> >
> > typedef struct {
> >      char bits[SIXBIT_LEN];          //!< raw 6-bit ASCII data string
> >      char *p;                        //!< pointer to current character
> > in bits
> >      unsigned char remainder;        //!< Remainder bits
> >      unsigned char remainder_bits;   //!< Number of remainder bits
> > } sixbit;
> >
>
> Your structure has holes and alignment problems.
>
> In that case, the compiler will reorder the fields in memory
> arbitrarily, and the Gambas interpreter does not know how to deal with
> that.
>
> If you made the library, try to reorder the structure fields so that
> there is no hole nor alignment problems anymore.
>
> In the future, I may be able to associate Gambas structures with libffi
> structures, so that passing Gambas structure to extern functions
> eventually work in all cases. Alas, nothing sure !
>
> Regards,
>
> --
> Benoît Minisini
>
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20181129/8fd8c8f7/attachment.html>


More information about the User mailing list