Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

String question

ahasan
Beginner
1,564 Views

Bothuses of the string szFileName(iPos1+1:iPos2-1)//' - Graph'C below compile OK, but not sure if the first construct is really legal. I believe that
loc(szFileName(iPos1+1:iPos2-1) is OK, but not sure aboutconcatenating the ' - Graph'C as part of the arg for loc(). If the first construct is legal,how does IVF handle the memory for the contatenated string? Is a new memory location created? Thanks for comments.

di%lpszDocName = loc(szFileName(iPos1+1:iPos2-1)//' - Graph'C)

or,

character(Max_Path) szName
szName = szFileName(iPos1+1:iPos2-1)//' - Graph'C)
di%lpszDocName = loc(szName)

Message Edited by halcyong@fcc.net on 02-28-2006 04:47 AM

0 Kudos
5 Replies
Jugoslav_Dujic
Valued Contributor II
1,564 Views
Yes, the argument to LOC() can be pretty much anything. If it's an expression, the compiler will appoint a location on the stack for the result and LOC() will return that address.

However, the documentation does not discuss the lifetime of such stack variable (i.e. when the stack is erased). It's almost certainly OK to supply it as an argument to a function:
call SomeAPI(LOC(string1//string2))

I'm not so sure about the "intermediate" approach as you described it (assign it to a variable and pass it to an API shortly afterwards). Steve should be able to tell you better the important issue is whether the stack is cleaned up on the next line or when the containing routine ends my guess is the latter (in which case it's OK to reuse the returned address subsequently in the same routine), but it's better to be on the safe side in the meanwhile.
0 Kudos
Steven_L_Intel1
Employee
1,564 Views
Jugoslav's suspicions are correct - loc(expression) is valid only until the end of the statement. After that, the contents of the expression are undefined, so it is incorrect to save the address of an expression across statements.
0 Kudos
ahasan
Beginner
1,564 Views

Just to make sure I understand correctly:

di%lpszDocName = loc(str1//str2) is OK,

but incorrect to use di%lpszDocName in a subsequent statement because the addressmayhave been overwritten on the stack.

Thanks for the replies.

0 Kudos
Steven_L_Intel1
Employee
1,564 Views
Right. The statement will compile and execute fine, but the value stored in the field won't necessarily point to anything useful in subsequent statements.
0 Kudos
ahasan
Beginner
1,564 Views
Thank you.
0 Kudos
Reply