From: Jere <jhb.chat@gmail.com>
Subject: Re: differences between Ada and C in gnat forcing me to use C instead of Ada
Date: Sun, 24 Mar 2019 22:58:15 -0700 (PDT)
Date: 2019-03-24T22:58:15-07:00 [thread overview]
Message-ID: <53581698-0b8e-48d7-90e9-fe336ec19482@googlegroups.com> (raw)
In-Reply-To: <c1aea932-9ae1-4915-a260-b6bc0b24c12c@googlegroups.com>
On Monday, March 25, 2019 at 1:46:07 AM UTC-4, matthewbr...@gmail.com wrote:
> Hi all:
>
> I really want to use Ada. But gnat is forcing me to use C. Basically, I wanted to interface with my scanner using SANE (Scanner Access Now Easy). Using my "real" Debian machine since gnat doesn't work under Windows 10 (Debian subsystem for Linux), I created the ada specs from the C headers and the sizes in C and Ada are different. For example:
>
> In C, this code:
> printf("sizeof(char *)=%d\n", sizeof(char *));
>
> prints "sizeof(char *)=8"
>
>
> and this code:
> printf("sizeof(SANE_Device)=%d\n", sizeof(SANE_Device));
>
> prints "sizeof(SANE_Device)=32". In C, the header defines it this way:
>
> typedef char SANE_Char;
> typedef const SANE_Char *SANE_String_Const;
>
> typedef struct
> {
> SANE_String_Const name; /* unique device name */
> SANE_String_Const vendor; /* device vendor string */
> SANE_String_Const model; /* device model name */
> SANE_String_Const type; /* device type (e.g., "flatbed scanner") */
> }
> SANE_Device;
>
> which makes sense to me since 8*4 = 32 (sizeof(char *) * 4 elements in the record. But on the Ada side, I get the following code:
>
> Text_Io.Put_Line ("sane_sane_h.SANE_Device'Size=>" &
> Integer'Image (sane_sane_h.SANE_Device'Size));
>
> to print "sane_sane_h.SANE_Device'Size=> 256"
>
> And sure enough "Integer'Image (InterfaceS.C.Strings.chars_ptr'Size))" prints out 64. Now, how can we interface to a C library passing around the 'Address of an array of pointers to records if the elements in the record are different sizes. No wonder, I get back garbage when calling sane_get_devices. How is it that "sizeof(char *) is 8 in C and InterfaceS.C.Strings.chars_ptr'Size is 64 in Ada. When I try to rep spec it, I get
So when you do sizeof() in C it returns the number of bytes. When
you do 'Size in Ada, it gives the number of bits. 8bits * 8bytes
is 64 bits, so everything looks correct. They are returning the same
effective size in both C and Ada.
next prev parent reply other threads:[~2019-03-25 5:58 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-25 5:46 differences between Ada and C in gnat forcing me to use C instead of Ada matthewbrentmccarty
2019-03-25 5:58 ` Jere [this message]
2019-03-25 8:25 ` Dmitry A. Kazakov
2019-03-25 14:06 ` Florian Weimer
2019-03-25 10:56 ` Philip Munts
2019-03-25 11:54 ` Lucretia
2019-03-25 14:09 ` matthewbrentmccarty
2019-03-25 14:20 ` Dmitry A. Kazakov
2019-03-25 16:46 ` Jeffrey R. Carter
2019-03-25 18:01 ` Dmitry A. Kazakov
2019-03-25 17:38 ` Niklas Holsti
2019-03-25 16:42 ` matthewbrentmccarty
2019-03-25 18:18 ` Dmitry A. Kazakov
2019-04-04 0:51 ` matthewbrentmccarty
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox