* Getting length of array from C @ 2005-02-01 4:16 Chuck 2005-02-01 5:26 ` Simon Wright 2005-02-01 7:25 ` Martin Krischik 0 siblings, 2 replies; 9+ messages in thread From: Chuck @ 2005-02-01 4:16 UTC (permalink / raw) Is there some way of getting an ada array length from C interface code? I have to try and accomplish this while minimizing the number of changes to the original Ada code. Any ideas? Thanks, Chuck ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Getting length of array from C 2005-02-01 4:16 Getting length of array from C Chuck @ 2005-02-01 5:26 ` Simon Wright 2005-02-01 7:25 ` Martin Krischik 1 sibling, 0 replies; 9+ messages in thread From: Simon Wright @ 2005-02-01 5:26 UTC (permalink / raw) "Chuck" <cwinters15@comcast.net> writes: > Is there some way of getting an ada array length from C interface > code? I have to try and accomplish this while minimizing the number > of changes to the original Ada code. Any ideas? Afraid I can't tell which way round you want this to work! Do you have an Ada array that you need C code to be able to get at, or a C array that you need Ada code to be able to get at? What's the concept of operation? Typically C code needs/can provide the address of the first element of the array and a count of the number of elements. Depending on your compiler, you may find something like procedure C_Proc (A : My_Array; Count : Integer); pragma Import (C, C_Proc, "c_proc"); helpful. You might need to declare a derived array type to get the representation to be compatible: type My_C_Array is new My_Array; pragma Convention (C, My_C_Array); -- Simon Wright 100% Ada, no bugs. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Getting length of array from C 2005-02-01 4:16 Getting length of array from C Chuck 2005-02-01 5:26 ` Simon Wright @ 2005-02-01 7:25 ` Martin Krischik 2005-02-01 13:17 ` Chuck 1 sibling, 1 reply; 9+ messages in thread From: Martin Krischik @ 2005-02-01 7:25 UTC (permalink / raw) Chuck wrote: > Is there some way of getting an ada array length from C interface code? > I have to try and accomplish this while minimizing the number of > changes to the original Ada code. Any ideas? I did a lot of C interfacing and there are quite a few option open. Usualy you need two parameters one for the array one for the size. A very interesting hint - if you declare the following type inside a procedure: type C_Array is array 0 .. Number - 1 of Interfaces.C.int; then Number does not need to a constant. And of course the other way around as well: type Ada_Array is array 1 .. Number of Integer; Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Getting length of array from C 2005-02-01 7:25 ` Martin Krischik @ 2005-02-01 13:17 ` Chuck 2005-02-01 14:17 ` Martin Krischik ` (3 more replies) 0 siblings, 4 replies; 9+ messages in thread From: Chuck @ 2005-02-01 13:17 UTC (permalink / raw) Here's the deal. I currently have some Ada code. Quite a bit of it. The originators of the code had done some C interfacing with the Ada code. So, I have some procedures like the following: procedure Write_Array( my_array : in My_Array_Type ); pragma Import( C, Write_Array, "c_write_array" ); The C function prototype from the header file I was given is: void c_write_array( void *array ); In the C code is there anyway I can get the length of the incoming array? So, is Ada actually passing in a structure or an array? Hope this clears things up. Chuck ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Getting length of array from C 2005-02-01 13:17 ` Chuck @ 2005-02-01 14:17 ` Martin Krischik 2005-02-01 20:07 ` Ludovic Brenta ` (2 subsequent siblings) 3 siblings, 0 replies; 9+ messages in thread From: Martin Krischik @ 2005-02-01 14:17 UTC (permalink / raw) Chuck wrote: > Here's the deal. I currently have some Ada code. Quite a bit of it. > The originators of the code had done some C interfacing with the Ada > code. So, I have some procedures like the following: > > procedure Write_Array( my_array : in My_Array_Type ); > pragma Import( C, Write_Array, "c_write_array" ); Well, at least GNAT is very upset if My_Array_Type is not a fixed array type. You are allowed to do it - but there is warning about the use of the dope vector. http://en.wikipedia.org/wiki/Dope_vector > The C function prototype from the header file I was given is: > void c_write_array( void *array ); That does not look right. If My_Array_Type is fixed then there should be typedef documenting that. If My_Array_Type in not fixed then indeed a length parameter is missing. > In the C code is there anyway I can get the length of the incoming > array? So, is Ada actually passing in a structure or an array? > Hope this clears things up. If My_Array_Type is fixed the the size is fixed. If My_Array_Type is not fixed then the code is broken because someone forgot to pass on the dope vector. Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Getting length of array from C 2005-02-01 13:17 ` Chuck 2005-02-01 14:17 ` Martin Krischik @ 2005-02-01 20:07 ` Ludovic Brenta 2005-02-07 6:55 ` Dave Thompson 2005-02-02 0:15 ` Jeffrey Carter 2005-02-02 2:22 ` Steve 3 siblings, 1 reply; 9+ messages in thread From: Ludovic Brenta @ 2005-02-01 20:07 UTC (permalink / raw) "Chuck" writes: > Here's the deal. I currently have some Ada code. Quite a bit of it. > The originators of the code had done some C interfacing with the Ada > code. So, I have some procedures like the following: > > procedure Write_Array( my_array : in My_Array_Type ); > pragma Import( C, Write_Array, "c_write_array" ); > > The C function prototype from the header file I was given is: > void c_write_array( void *array ); > > In the C code is there anyway I can get the length of the incoming > array? So, is Ada actually passing in a structure or an array? > Hope this clears things up. > > Chuck No length information is passed to, or expected by the C function. The "designers" (if they call themselves that) of this old code don't want you to be bothered with array lengths :) Because of the pragma Import, there is no dope vector either; the C function just gets a void* pointing at the first element in the array. This reminds me of the C library's null-terminated strings. Of course, all the functions in the C library that fail to use a length parameter are deprecated (e.g. strdup is deprecated in favour of strndup). You should look for a design document that explains where the array ends; there may be a terminating element at the end of the array, or perhaps the array has a "well-known size" somewhere. If My_Array_Type is constrained, you're in luck because you'll know the size at compile time. -- Ludovic Brenta. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Getting length of array from C 2005-02-01 20:07 ` Ludovic Brenta @ 2005-02-07 6:55 ` Dave Thompson 0 siblings, 0 replies; 9+ messages in thread From: Dave Thompson @ 2005-02-07 6:55 UTC (permalink / raw) On Tue, 01 Feb 2005 21:07:08 +0100, Ludovic Brenta <ludovic.brenta@insalien.org> wrote: <snip> > This reminds me of the C library's null-terminated strings. Of > course, all the functions in the C library that fail to use a length > parameter are deprecated (e.g. strdup is deprecated in favour of > strndup). > Presumably you mean strncpy vs strcpy and strncat vs strcat, and the wide-string wcs* versions similarly. These latter are not deprecated by any C standard (yet), and the former are NOT just exact replacements-plus-size for the latter -- this is a fairly frequent source of problems posted to comp.lang.c. Neither is sprintf deprecated in favor of snprintf, which is only standard as of C99. (If you want the gritty, *ncpy pads if short but doesn't terminate if long; *ncat always terminates but requires the _available/remaining_ size minus 1 not the total size. Oh, and snprintf was fairly widely available pre-C99 but often with a different return value for the overflow case. Oh joy.) There is work in WG14 (and J11) currently on a proposed "TR" (effectively a standardized option) for "secure" versions of library routines that are exact replacements. If this is widely adopted it might become required and the older forms deprecated around '09 or so. Berkeley strlcpy and strlcat already are exact replacements, which some people promote as preferable, but they are not standard, not even POSIX/SUS. To be clear, even in the 'n' 'l' and 's' versions only the size aka maximum length (or a variant of it) is an added/explicit parameter; the _current_ length is still done by NUL-termination. If you want explicitly counted strings you have to build them yourself with mem*, although of course in C++ you can package the result as classes with methods and if you want operators -- or just use std::string which is already pointer+counts like Ada Unbounded_String. strdup is not in any C standard, although it is POSIX/SUS; strndup doesn't exist at all and probably doesn't need to since strdup's target is newly allocated large enough space not an existing buffer. > You should look for a design document that explains where the array > ends; there may be a terminating element at the end of the array, or > perhaps the array has a "well-known size" somewhere. If My_Array_Type > is constrained, you're in luck because you'll know the size at compile > time. - David.Thompson1 at worldnet.att.net ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Getting length of array from C 2005-02-01 13:17 ` Chuck 2005-02-01 14:17 ` Martin Krischik 2005-02-01 20:07 ` Ludovic Brenta @ 2005-02-02 0:15 ` Jeffrey Carter 2005-02-02 2:22 ` Steve 3 siblings, 0 replies; 9+ messages in thread From: Jeffrey Carter @ 2005-02-02 0:15 UTC (permalink / raw) Chuck wrote: > procedure Write_Array( my_array : in My_Array_Type ); > pragma Import( C, Write_Array, "c_write_array" ); > > The C function prototype from the header file I was given is: > void c_write_array( void *array ); There isn't enough information here to know what c_write_array is doing with the array. You may have to look at the body to know how to use it. > In the C code is there anyway I can get the length of the incoming > array? So, is Ada actually passing in a structure or an array? > Hope this clears things up. The only way for C to know the length is to pass it as a 2nd parameter. Ada will pass a convention-C pointer to the array's 1st element. -- Jeff Carter "You cheesy lot of second-hand electric donkey-bottom biters." Monty Python & the Holy Grail 14 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Getting length of array from C 2005-02-01 13:17 ` Chuck ` (2 preceding siblings ...) 2005-02-02 0:15 ` Jeffrey Carter @ 2005-02-02 2:22 ` Steve 3 siblings, 0 replies; 9+ messages in thread From: Steve @ 2005-02-02 2:22 UTC (permalink / raw) How is "My_Array_Type" defined? Steve (The Duck) "Chuck" <cwinters15@comcast.net> wrote in message news:1107263868.447580.257310@z14g2000cwz.googlegroups.com... > Here's the deal. I currently have some Ada code. Quite a bit of it. > The originators of the code had done some C interfacing with the Ada > code. So, I have some procedures like the following: > > procedure Write_Array( my_array : in My_Array_Type ); > pragma Import( C, Write_Array, "c_write_array" ); > > The C function prototype from the header file I was given is: > void c_write_array( void *array ); > > In the C code is there anyway I can get the length of the incoming > array? So, is Ada actually passing in a structure or an array? > Hope this clears things up. > > Chuck > ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2005-02-07 6:55 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2005-02-01 4:16 Getting length of array from C Chuck 2005-02-01 5:26 ` Simon Wright 2005-02-01 7:25 ` Martin Krischik 2005-02-01 13:17 ` Chuck 2005-02-01 14:17 ` Martin Krischik 2005-02-01 20:07 ` Ludovic Brenta 2005-02-07 6:55 ` Dave Thompson 2005-02-02 0:15 ` Jeffrey Carter 2005-02-02 2:22 ` Steve
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox