3

There is Following C-Function with void pointer as Argument:

int read(void *buf, size_t num); 

int => returns if the read operation was successfully or not.
void *buf => c void unsigned byte buffer pointer as function argument.
size_t num => size of the byte buffer which should be filled up.

Currently I have the following Ada Implementation:

with Ada.Text_IO; with System; with Interfaces.C; use Ada.Text_IO; use Interfaces.C; procedure Main is -- Imported C function function Read(Buf:System.Address; Num:int) return int; pragma Import(C, Read, "read"); -- Byte Array Type type Byte_Type is mod 2**8; for Byte_Type'Size use 8; type Byte_Array_Type is array(Positive range <>) of Byte_Type; -- Initialize Buffer_Check:int; Buffer_Size:Positive:=10; Buffer_Array:Byte_Array_Type(1 .. Buffer_Size):=(others => 0); --initialise array with zero begin Buffer_Check:=Read(Buffer_Array'Address, int(Buffer_Size)); if Buffer_Check /= 0 then Put_Line("Read success"); for K in Buffer_Array'First .. Buffer_Array'Last loop Put_Line(Integer'Image(Integer(Byte_Type(Buffer_Array(K))))); end loop; else Put_Line("Read Failed"); end if; end Main; 

The Buffer_Array doesn't get filled up as expected. It would be Great if some Ada Enthusiast have some hint or Idea.

3
  • "doesn’t get filled up as expected" - what actually happens? What OS are you using? Which library is read() imported from? Commented Jan 1, 2021 at 17:49
  • Output : Read success 0 0 0 0 0 0 0 0 0 0 OS:Slackware14.04 64 bit, read is an example, but i want to import SSL_read from openssl.org/docs/man1.1.1/man3/SSL_read.html Commented Jan 1, 2021 at 18:54
  • 2
    SSL_read Works now. Nothing was wrong with the Ada Code. It was an false c implementation of my own c example function. Thank you Simon to have a look at the code. Commented Jan 1, 2021 at 19:50

1 Answer 1

5

Assuming that an Ada private type System.Address and a C pointer are compatible is extremely non-portable and unnecessary. Declare your types with convention C and your out parameter as an out parameter:

pragma Convention (C, Byte_Type); pragma Convention (C, Byte_Array_Type); function Read (Buf : out Byte_Array_Type; Num : in int) return int; pragma Import (C, Read, "read"); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.