From: glen herrmannsfeldt on
In comp.lang.fortran Eli Osherovich <eli.osherovich(a)gmail.com> wrote:
> On Mar 9, 3:18?pm, Malcolm McLean <malcolm.mcle...(a)btinternet.com>
> wrote:
>> Phred Phungus wrote:
>> > Eli Osherovich wrote:
>> > > What is the correct way to write a wrapper for C functions
>> > > that accept/return strings.

>> > > For example
>
>> > > void cfunc1(char* str)
>> > > char * cfunc2(void)

Do you mean a Fortran wrapper for the C routine, or a C wrapper,
with the above declaration, for a Fortran routine?

>> Acceting a string is easy. Just accept a char * or a
>> const char * to a> nul-terminated string.

>> Returning strings you have to make a judgement call. Sometimes it is
>> easier for caller to pass you a char * to a buffer which he controls,
>> sometimes it is easier to return a char * containing allocated data.
>> (Just rarely you want to retrn a char * to a static buffer). The fisrt
>> method is likely to be faster and doesn't depend on malloc(), the
>> second method is much less likely to allow a buffer overflow, and may
>> the the only practical method if the length of the return string can't
>> be predicted in advance.

The first and third are most commonly used by C library routines,
and by user written functions. C programmers hate to have to keep
track of memory malloc()ed by other routines.

The string tokenizer, strtok(), returns pointers into the string
supplied to it in the first call. ctime() returns a pointer to
a static buffer. strdup() returns a pointer from malloc().

> May be I do not understand you correctly... but my impression is you
> are talking about C not Fortran.

Well, you have to talk about C somewhere. As a C wrapper to a Fortran
function, you have to do what other C programs can use. As a wrapper
to a C funtion, you have to live with what the C function does.
The above three are pretty much the choices you have.

-- glen

From: Eli Osherovich on
On Mar 9, 7:58 pm, glen herrmannsfeldt <g...(a)ugcs.caltech.edu> wrote:
> In comp.lang.fortran Eli Osherovich <eli.osherov...(a)gmail.com> wrote:
>
> > On Mar 9, 3:18?pm, Malcolm McLean <malcolm.mcle...(a)btinternet.com>
> > wrote:
> >> Phred Phungus wrote:
> >> > Eli Osherovich wrote:
> >> > > What is the correct way to write a wrapper for C functions
> >> > > that accept/return strings.
> >> > > For example
>
> >> > > void cfunc1(char* str)
> >> > > char * cfunc2(void)
>
> Do you mean a Fortran wrapper for the C routine, or a C wrapper,
> with the above declaration, for a Fortran routine?
>
> >> Acceting a string is easy. Just accept a char * or a
> >> const char * to a> nul-terminated string.
> >> Returning strings you have to make a judgement call. Sometimes it is
> >> easier for caller to pass you a char * to a buffer which he controls,
> >> sometimes it is easier to return a char * containing allocated data.
> >> (Just rarely you want to retrn a char * to a static buffer). The fisrt
> >> method is likely to be faster and doesn't depend on malloc(), the
> >> second method is much less likely to allow a buffer overflow, and may
> >> the the only practical method if the length of the return string can't
> >> be predicted in advance.
>
> The first and third are most commonly used by C library routines,
> and by user written functions.  C programmers hate to have to keep
> track of memory malloc()ed by other routines.
>
> The string tokenizer, strtok(), returns pointers into the string
> supplied to it in the first call.  ctime() returns a pointer to
> a static buffer.  strdup() returns a pointer from malloc().
>
> > May be I do not understand you correctly... but my impression is you
> > are talking about C not Fortran.
>
> Well, you have to talk about C somewhere.  As a C wrapper to a Fortran
> function, you have to do what other C programs can use.  As a wrapper
> to a C funtion, you have to live with what the C function does.
> The above three are pretty much the choices you have.
>
> -- glen

I want a Fortran wrapper for C functions whose prototypes were given
in the first message.