Dealing with function returns of pointer to null terminated string

Dealing with function returns of pointer to null terminated string

What is the best method for properly representing the return value of a function that returns a pointer to a null terminated string (actually a null terminated block of null terminated strings), for example Win32 GetEnvironmentStrings, as a string variable in CVF?

8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Functions that return address pointers should have their return values declared as INTEGER(KIND=INT_PTR_KIND()) You could then use the Integer POINTER extension to say that this is the address of a CHARACTER variable. You would have to determine the length yourself by finding the NUL.

Steve

Retired 12/31/2016

So should the character variable be declared as CHARACTER(BIG_NUMBER) varname rather than some syntax indicating that the size is not known at compile time?

James

Sure, or try CHARACTER*(*) - that may work (haven't tried it.)

Steve

Retired 12/31/2016

That was the first thing I tried as it seems like the intuitive syntax to use in this instance, however an error results - "This passed length character name has been used in an invalid context." Too bad because I *like* that answer. :-)

James

...but you're just fine with CHARACTER(BIG_NUMBER). Note that no storage for pointee is allocated (except explicitly with malloc or GlobalAlloc) so there's no memory overhead. Just take care not to read/write beyound terminal char(0) (use INDEX(string, CHAR(0)) to find where it is)

Fortran compiler must know about string lengths all the time so CHARACTER(*) form is not possible (since length doesn't come from arg-list). In contrast, arrays (*) are just fine (that's probably what inclined Steve to post his previous answer). For example, this is also not allowed:

SUBROUTINE foo(String) 
!DEC$ATTRIBUTES REFERENCE:: String  !Don't expect length in arg-list 
CHARACTER(*):: String      !Don't define length here either 

Jugoslav

Jugoslav,

I understand about character(BIG_NUMBER), being sufficient given that BIG_NUMBER is big enough for the task at hand. For the Win32 I referenced originally, I'm not sure what that would be, so it would be set to probably a couple pages and left at that. However the character(*) syntax is more intuitive given that the variable is a pointee in the POINTER extension. The compiler could wait to flag it as a syntax error until that possiblity is checked. Just makes the code a little more readable, that's all.

James

Try this if you can accept operating on the string in a subroutine...

GetEnvironmentStrings(..) to get the pointer to the memory (address)

Your own method for determining the number of bytes (Nbytes)

Call SubA(Nbytes, Address)

subroutine SubA(N, address)
integer(4) N, address
character(len=N) String
POINTER (pString, String) ! Compaq pointer type
pString = address
..... operate on String......
end subroutine SubA

Leave a Comment

Please sign in to add a comment. Not a member? Join today