<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Best/Correct  way to Dereferencing C-pointers in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789911#M32053</link>
    <description>Dear all,&lt;BR /&gt;&lt;BR /&gt;I am a little confused on some of this thread. Steve gave as an example the command:&lt;BR /&gt;&lt;BR /&gt;CALL_C_F_POINTER(TRANSFER(lParam,C_NULL_PTR), dis)&lt;BR /&gt;&lt;BR /&gt;where I should use&lt;BR /&gt;&lt;BR /&gt;CALL C_F_POINTER(C_LOC(lParam), dis)&lt;BR /&gt;&lt;BR /&gt;I have tested both of these two versions and both seems to work. Is there any preference for one of them? Did I missed some important point? (Note: I am neither a C-expert nor a Fortran-pointer expert.)&lt;BR /&gt;&lt;BR /&gt;Robert van Anerongen</description>
    <pubDate>Thu, 14 Jul 2011 15:47:24 GMT</pubDate>
    <dc:creator>Robert_van_Amerongen</dc:creator>
    <dc:date>2011-07-14T15:47:24Z</dc:date>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789903#M32045</link>
      <description>Frequently in windows programming you have a procedure which supplies a pointer to windows structure. e.g. &lt;BR /&gt;&lt;BR /&gt;function MyDlgProc(hDlg, message, wParam, lParam)&lt;BR /&gt;&lt;BR /&gt;where lParam may be a INTEGER(HANDLE) pointer.&lt;BR /&gt;&lt;BR /&gt;In order to access the structure to which it is an address, what I find works at present is to do the following:&lt;BR /&gt;&lt;BR /&gt;type (T_DRAWITEMSTRUCT) DIS ! Window DrawItem Structure...&lt;BR /&gt;POINTER(pDIS,DIS)&lt;BR /&gt;TYPE (T_RECT) RC ! Window rectangle structure...&lt;BR /&gt;...&lt;BR /&gt;pDIS=lparam ! Dereference the pointer&lt;BR /&gt;RC=DIS%rcItem&lt;BR /&gt;&lt;BR /&gt;What is the correct, portable way to do this?&lt;BR /&gt;&lt;BR /&gt;TIA &lt;BR /&gt;&lt;BR /&gt;P.S.&lt;BR /&gt;&lt;BR /&gt;I have seen TRANSFER and C_F_POINTER used, but am at a loss to know exactly what they do that is different.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 13 Jul 2011 10:55:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789903#M32045</guid>
      <dc:creator>anthonyrichards</dc:creator>
      <dc:date>2011-07-13T10:55:11Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789904#M32046</link>
      <description>You ought to be able to &lt;BR /&gt;USE iso_c_binding&lt;BR /&gt;&lt;BR /&gt;and make a Fortran standard pointer from the C pointer by c_f_pointer. &lt;BR /&gt;I don't expect a consensus on the answer to your question; most of the important compilers support Cray pointer well enough to consider your method portable, at least among the widespread 64-bit operating systems.</description>
      <pubDate>Wed, 13 Jul 2011 12:20:14 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789904#M32046</guid>
      <dc:creator>TimP</dc:creator>
      <dc:date>2011-07-13T12:20:14Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789905#M32047</link>
      <description>The way you're doing it is ok - that code is inherently non-portable. But if you wanted to avoid use of language extensions, you could do something like this:&lt;BR /&gt;&lt;BR /&gt;use, intrinsic :: iso_c_binding&lt;BR /&gt;use ifwin&lt;BR /&gt;type(T_DRAWITEMSTRUCT), POINTER :: DIS&lt;BR /&gt;...&lt;BR /&gt;call C_F_POINTER(TRANSFER(lParam, C_NULL_PTR), DIS)&lt;BR /&gt;RC=DIS%rcItem&lt;BR /&gt;&lt;BR /&gt;The TRANSFER converts the address-sized lParam to an entity of type C_PTR (C_NULL_PTR is a convenient predeclared item of that type) and then C_F_POINTER makes DIS a pointer to the target.</description>
      <pubDate>Wed, 13 Jul 2011 15:27:05 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789905#M32047</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2011-07-13T15:27:05Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789906#M32048</link>
      <description>Thanks, Steve. &lt;BR /&gt;
&lt;BR /&gt;
I must admit to not really understanding pointers and how they are transformed to 'normal' declared objects.&lt;BR /&gt;

Take for example my code:&lt;BR /&gt;
&lt;BR /&gt;
type (T_DRAWITEMSTRUCT) DIS ! Window DrawItem Structure...&lt;BR /&gt;
POINTER(pDIS,DIS)&lt;BR /&gt;
&lt;BR /&gt;
Now, normally when I declare a variable (in this case DIS), I expect the
 compiler to alot a place in memory for it and whenever 'DIS' appears in
 subsequent lines of code to use that address (inserted into a 
placeholder when linking) when its value (or address + an offset when 
the value of one of its components) is referenced.&lt;BR /&gt;
&lt;BR /&gt;
So DIS must have an address in memory and space alotted to it. What I 
believe the POINTER(pDIS,DIS) does is to tell the compiler &lt;BR /&gt;
&lt;UL&gt;&lt;LI&gt;EITHER that the initial address alotted to DIS in memory is actually going to be changed to the value subsequently given to pDIS&lt;/LI&gt;&lt;LI&gt;OR not to alot a place (address) in memory for DIS, but to 
substitute the value that subsequently appears in the memory address 
pointed to by pDIS (pDIS must be alotted a place in memory by the 
compiler, no?).&lt;/LI&gt;&lt;/UL&gt;
In the first case, there remains a memory area set aside for DIS which 
is subsequently not used (because subsequent references to DIS point to a
 new location) whereas in the second case, no initial memory area is 
alotted but the memory area used is that subsequently pointed to by pDIS
 (and alotted by code elsewhere).&lt;BR /&gt;
&lt;BR /&gt;
Which is the case? Ot is neither the complete story?&lt;BR /&gt;
&lt;BR /&gt;
Then we come to the declaration&lt;BR /&gt;&lt;BR /&gt;type(T_DRAWITEMSTRUCT), POINTER :: DIS&lt;BR /&gt;&lt;BR /&gt;which leaves me wondering 'what space is initially alotted to DIS?'. Is it just a 4-byte area to store an integer address? In which case, how does the compiler take care of the subsequent SIZE required for the structure pointed to? Does it dynamically allocate it after a value is supplied for its address and how is this address supplied in place of the value stored in the address initially pointed to by 'DIS'?&lt;BR /&gt;&lt;BR /&gt;TIA.</description>
      <pubDate>Thu, 14 Jul 2011 07:49:14 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789906#M32048</guid>
      <dc:creator>anthonyrichards</dc:creator>
      <dc:date>2011-07-14T07:49:14Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789907#M32049</link>
      <description>When you use the "Integer POINTER extension" (commonly referred to as 
"Cray pointers", though Cray itself doesn't like the term), the "OR" 
case you list is what happens. The variable is not allocated any 
memory. When you access the variable, the code goes through the pointer 
variable, whatever its contents, to get to the target.&lt;BR /&gt;&lt;BR /&gt;When using
 the Fortran 90 style POINTER, it's much the same. No space is 
allocated to DIS, but you can use ALLOCATE to do so. Alternatively, as I
 suggested here, you can use C_F_POINTER to "fill in" the pointer with 
the address from the C pointer. Note that such a pointer can also be an
 assumed-shape array, in which case you need to supply the array bounds 
in the call to C_F_POINTER.</description>
      <pubDate>Thu, 14 Jul 2011 13:41:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789907#M32049</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2011-07-14T13:41:00Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789908#M32050</link>
      <description>Tony,&lt;BR /&gt;&lt;BR /&gt;Here is a point-of-view regarding Fortran-90 pointers that I found helpful during my own struggles with the concept, given a mind already corrupted by the C-language notion of pointer. The facts of the point-of-view are debatable, and &lt;SPAN style="text-decoration: underline;"&gt;it will be useless and unnecessary if you do not know C pointers&lt;/SPAN&gt;.&lt;BR /&gt;&lt;BR /&gt;In C, pointers are never confused with targets. The operator/prefixes '*' and '&amp;amp;' are used to dereference pointers and to create pointers to existing objects. At least in my C programs, the frequency of occurrence of '*' is much more than that of '&amp;amp;'. The creators of Fortran 90 must have felt that '*' clutters up source code, so they decided to leave it out. Thus, &lt;SPAN style="text-decoration: underline;"&gt;dereferencing is always implied&lt;/SPAN&gt; when a pointer type variable occurs in an expression. If, in C, you say&lt;BR /&gt;&lt;BR /&gt; var = *ptr; OR *ptr = var;&lt;BR /&gt;&lt;BR /&gt;in Fortran you simply write, respectively,&lt;BR /&gt;&lt;BR /&gt; var = ptr OR ptr = var&lt;BR /&gt;&lt;BR /&gt;For the few cases where, in C, you would say&lt;BR /&gt;&lt;BR /&gt; ptr = &amp;amp;var;&lt;BR /&gt;&lt;BR /&gt;in Fortran you say&lt;BR /&gt;&lt;BR /&gt; ptr =&amp;gt; var&lt;BR /&gt;&lt;BR /&gt;I think that providing the TARGET attribute was made all but necessary by this choice in Fortran, given the emphasis on code speed in Fortran.&lt;BR /&gt;</description>
      <pubDate>Thu, 14 Jul 2011 14:08:29 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789908#M32050</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2011-07-14T14:08:29Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789909#M32051</link>
      <description>&lt;P&gt;The term pointer is a bit unfortunate -"reference" would have been a better one, IMHO.&lt;BR /&gt;And in my equally humble opinion, C (and especially C++ with its references) makes it&lt;BR /&gt;unnecessarily complicated. You have * to dereference a pointer and -&amp;gt; to dereference&lt;BR /&gt;a component of a pointer to a structure. The compiler complains if you get it wrong -&lt;BR /&gt;so it actually knows what it shouldbe!Such a distinction is not needed in Fortran :).&lt;BR /&gt;(And there are other places where the Fortran syntax is simpler too - function pointers&lt;BR /&gt;for instance).&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Arjen&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2011 14:57:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789909#M32051</guid>
      <dc:creator>Arjen_Markus</dc:creator>
      <dc:date>2011-07-14T14:57:00Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789910#M32052</link>
      <description>&lt;I&gt;&amp;gt; The term pointer is a bit unfortunate&lt;BR /&gt;&lt;BR /&gt;&lt;/I&gt;I agree. The argument reminds me of being startled when I first came to the US and heard someone say, "you know, in the UK they drive on the wrong side of the road".</description>
      <pubDate>Thu, 14 Jul 2011 15:19:06 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789910#M32052</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2011-07-14T15:19:06Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789911#M32053</link>
      <description>Dear all,&lt;BR /&gt;&lt;BR /&gt;I am a little confused on some of this thread. Steve gave as an example the command:&lt;BR /&gt;&lt;BR /&gt;CALL_C_F_POINTER(TRANSFER(lParam,C_NULL_PTR), dis)&lt;BR /&gt;&lt;BR /&gt;where I should use&lt;BR /&gt;&lt;BR /&gt;CALL C_F_POINTER(C_LOC(lParam), dis)&lt;BR /&gt;&lt;BR /&gt;I have tested both of these two versions and both seems to work. Is there any preference for one of them? Did I missed some important point? (Note: I am neither a C-expert nor a Fortran-pointer expert.)&lt;BR /&gt;&lt;BR /&gt;Robert van Anerongen</description>
      <pubDate>Thu, 14 Jul 2011 15:47:24 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789911#M32053</guid>
      <dc:creator>Robert_van_Amerongen</dc:creator>
      <dc:date>2011-07-14T15:47:24Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789912#M32054</link>
      <description>The latter, with C_LOC, would only work if lParam was passed in by value, which it is in this specific case. The first argument must be of type C_PTR and be a value containing the address.</description>
      <pubDate>Thu, 14 Jul 2011 16:42:49 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789912#M32054</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2011-07-14T16:42:49Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789913#M32055</link>
      <description>Steve,&lt;BR /&gt;&lt;BR /&gt;when using window procecures I always have the dummy arguments declared with the VALUE attribute. So this answers my question. Thanks for that.&lt;BR /&gt;&lt;BR /&gt;Best regards,&lt;BR /&gt;Robert van Amerongen</description>
      <pubDate>Fri, 15 Jul 2011 12:50:02 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789913#M32055</guid>
      <dc:creator>Robert_van_Amerongen</dc:creator>
      <dc:date>2011-07-15T12:50:02Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789914#M32056</link>
      <description>Thanks for the guidance.&lt;BR /&gt;I have implemented Steve's suggestions and examined the sizes and values of the types produced and am now clearer about what happens. This is how I see it:&lt;BR /&gt;&lt;BR /&gt;The ISO_C_BINDING module contains the type C_PTR which has a single private component C_PTR%PTR of size 4 bytes.&lt;BR /&gt;(The C_NULL_PTR is a C_PTR which has C_PTR%PTR=0).&lt;BR /&gt;&lt;BR /&gt;Interoperable pointers must always be cast as this type for safety.&lt;BR /&gt;&lt;BR /&gt;Since its component is private, the component(s) of C_PTR types can only be manipulated by public procedures such as C_F_POINTER.&lt;BR /&gt;If FORTRAN variable LPARAM contains an address (pointer), it is not interoperable and so must be cast to a C_PTR using the TRANSFER intrinsic. &lt;BR /&gt;Basically, all this does is put the value stored in LPARAM into C_PTR%PTR.&lt;BR /&gt;&lt;BR /&gt;C_LOC(LPARAM) produces a C_PTR with C_PTR%PTR=LOC(LPARAM). This only produces what I want if LPARAM contains the actual address of the structure I want.&lt;BR /&gt;&lt;BR /&gt;What if LPARAM contains a pointer to the address of the structure (i.e. it is supplied by reference)?&lt;BR /&gt;&lt;BR /&gt;P.S. I note that the module is named as ISO_C_BINDING.MODINTR. Why the MODINTR and not MOD? How does that work?&lt;BR /&gt;</description>
      <pubDate>Mon, 18 Jul 2011 09:47:44 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789914#M32056</guid>
      <dc:creator>anthonyrichards</dc:creator>
      <dc:date>2011-07-18T09:47:44Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789915#M32057</link>
      <description>The .MODINTR is how we distinguish intrinsic modules from user modules. If you say:&lt;BR /&gt;&lt;BR /&gt;USE, INTRINSIC :: ISO_C_BINDING&lt;BR /&gt;&lt;BR /&gt;then we only look at the .MODINTR even if there is a "user" ISO_C_BINDING.MOD in the include path. If you write:&lt;BR /&gt;&lt;BR /&gt;USE ISO_C_BINDING&lt;BR /&gt;&lt;BR /&gt;then if ISO_C_BINDING.MOD exists it will be used. This satisfies the language rules so that introduction of intrinsic modules doesn't break existing applications.&lt;BR /&gt;&lt;BR /&gt;C_LOC returns a C_PTR containing the address of the argument's value. If the argument is a normal Fortran variable, then it would be a pointer to that value. In the case of a variable LPARAM, the value itself is an address so you'd need an additional level of indirection, and that's what TRANSFER would do for you. The same would be needed if LPARAM was passed by reference (the address of the address).&lt;BR /&gt;</description>
      <pubDate>Mon, 18 Jul 2011 14:51:33 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789915#M32057</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2011-07-18T14:51:33Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789916#M32058</link>
      <description>Do you mean to say that you would then have to do two TRANSFERs to get to the C_PTR from LPARAM in that case?</description>
      <pubDate>Mon, 18 Jul 2011 15:35:18 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789916#M32058</guid>
      <dc:creator>anthonyrichards</dc:creator>
      <dc:date>2011-07-18T15:35:18Z</dc:date>
    </item>
    <item>
      <title>Best/Correct  way to Dereferencing C-pointers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789917#M32059</link>
      <description>No, just one. You're not using C_LOC then.</description>
      <pubDate>Mon, 18 Jul 2011 15:48:46 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Best-Correct-way-to-Dereferencing-C-pointers/m-p/789917#M32059</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2011-07-18T15:48:46Z</dc:date>
    </item>
  </channel>
</rss>

