I have a C function int func( int a, int* b) that I need to import and use in Ada 95. Where the C function would typically be called in C as c = func(a, &b);
I import C functions all the time into Ada, but have always avoided using functions with pass by reference arguments, but it’s finally time to learn.
I would like to know how to declare this function in Ada, and would also like a quick example of using it with declared variables shown (because I’m still a little fuzzy on the Access types).
Thanks all!
In C,
int* bcan mean a lot of things. Perhaps it’s really just a pointer to one variable, but it may also be an array for whichint ais the length. This code assumes thatint* bis actually just a value that’s passed by reference:You can use
'Accessonaliasedvariables. This is safe as long as you’re sure that the pointer won’t be stored on the C side and accessed after the end of life of variableB. (If the C declaration uses theconstkeyword, you can useaccess constanton the Ada side, but that’s Ada 2005 only.)You can also use a named type:
Now we need to use
'Unchecked_Accessbecause Ada normally does not allow a non-local access type (asInt_Access) to refer to a local variable. If you know what the C code will do with the pointer (like you should), you can use named types to specify that no references to local variables should be passed.Nota bene 1: If you have a procedure (in C: a function that returns
void), you can specify a variable to be passed by reference by usingin outin your Ada procedure declaration instead ofaccess. This way, you do not need to worry about access types at all. As before, you need to be sure that the pointer is not stored at the C side.Nota bene 2: Record types and arrays are passed by reference anyway – unless you specify
pragma Convention (C_Pass_By_Copy, Your_Type);. This is a common gotcha when wrapping C functions in Ada.