- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This might really be a questioon for a C forum, but maybe someone working on ISO_C_BINDING has already considered this.
Fortran cannot easily call functions with variable argument lists, but I am wondering if it works in practice to call such functions with the variable argument part excluded. Is calling a vararg function with no extra arguments the same as calling an function with the same prototype without the final ellipsis, as long as it also uses the standard __cdecl calling convention? ISO_C_BINDING specifically excludes vararg functions, but maybe that is only because some systems use a non-standard calling convention, like MS-Windows STDCALL, or because the don't want to support the var-arg part?
If so, maybe it is reliable to prototype a C vararg function as an equivalent non-vararg function using ISO_C_BINDING, as long as there is some way to ensure that it uses the __cdecl convention, such as !DEC$ attribute extensions on Windows. It is simple enough to write a C wrapper, but it would be nice to call directly and avoid the C code, if possible.
Fortran cannot easily call functions with variable argument lists, but I am wondering if it works in practice to call such functions with the variable argument part excluded. Is calling a vararg function with no extra arguments the same as calling an function with the same prototype without the final ellipsis, as long as it also uses the standard __cdecl calling convention? ISO_C_BINDING specifically excludes vararg functions, but maybe that is only because some systems use a non-standard calling convention, like MS-Windows STDCALL, or because the don't want to support the var-arg part?
If so, maybe it is reliable to prototype a C vararg function as an equivalent non-vararg function using ISO_C_BINDING, as long as there is some way to ensure that it uses the __cdecl convention, such as !DEC$ attribute extensions on Windows. It is simple enough to write a C wrapper, but it would be nice to call directly and avoid the C code, if possible.
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It is my understanding that a vararg call actually passes an array with a count that has the ... arguments. There is no direct support for this from Fortran, but you may be able to hack something. It was not a goal of Fortran's C interoperability feature to support all possible C interfaces.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My goal ios to call varargs without the extra arguments. In most cases, varargs is a string formatting convenience, and a Fortran caller can just do an internal write, then pass a pre-formatted string, as long as it has no printf syntax.
For stack-based compilers, var-arg is just a standard call, except that the caller optionally pushes extra arguments onto the stack. The inital set of arguments is the same as a regular C call. So, if no extra arguments are added, it works just like a C prototype without the "...". What I don't know is whether that is required for the CDECL calling convention, or if this just works for typical stack-based hardware.
I'll post this question to the C forum.
For stack-based compilers, var-arg is just a standard call, except that the caller optionally pushes extra arguments onto the stack. The inital set of arguments is the same as a regular C call. So, if no extra arguments are added, it works just like a C prototype without the "...". What I don't know is whether that is required for the CDECL calling convention, or if this just works for typical stack-based hardware.
I'll post this question to the C forum.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I think you will find, when you post on the C forum, that your understanding of vararg is incorrect.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
MADsblionel:
I think you will find, when you post on the C forum, that your understanding of vararg is incorrect.
I think that passing an argument array, as you mentioned, is what happens when passing a "va_list" argument, which is different than "...".
Also, I forgot that "vararg" is the old interface, and "stdarg" is the new interface to variable argument lists. Maybe vararg does pass arguments the same as va_list?
Just to verify that stdarg does push extra arguments onto the stack, I compiled the following C test code, which is compiled in two parts via an #ifdef, to avoid a conflicting type error:
#include
#include
#ifdef MAIN
void test(int a,...);
int main(int argc, char *argv[]) {
test(1,2,3,4);
return 0;
}
#else
void test(int a, int b, int c, int d) {
printf("%d,%d,%d,%d ",a,b,c,d);
}
#endif
When run, I get:
1,2,3,4
Even though it happens to work on Linux, that doesn't mean that CDECL is required to work that way.
I suppose that even if CDECL does ensure that this works, it is still a hack, and it is really better just to have a C wrapper function.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page