<?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 Re: Fortran and C alignment in Operating System - OpenVMS</title>
    <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479257#M42771</link>
    <description>Two obvious paths here include altering the C code use C pointers to reference the data structures (an approach which has other benefits going forward), or an upgrade of the OpenVMS I64 boxes to use consistent versions of the operating system and tools.&lt;BR /&gt;</description>
    <pubDate>Thu, 20 Aug 2009 22:11:57 GMT</pubDate>
    <dc:creator>Hoff</dc:creator>
    <dc:date>2009-08-20T22:11:57Z</dc:date>
    <item>
      <title>Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479236#M42750</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I have searched the KB, but did not find what is needed.&lt;BR /&gt;&lt;BR /&gt;We have a legacy application written in Fortran, that we are converting to C.&lt;BR /&gt;For unit testing purposes (also divide-and-conquer), we typically convert a process to C, and leave the rest in Fortran. This will require our converted C process to call certain modules that are in Fortran, that is, calling Fortran from C (sharing programs in mixed languages). Our problem now lies in the alignment especially for COMMON blocks (shared memory).&lt;BR /&gt;&lt;BR /&gt;Our legacy uses many COMMON, and we have "successfuly" map the global section in C to the COMMON in Fortran. However, we found out (through compile maps) that the global section size in C does not match the one in Fortran, and this will cause issues at run-time. This may be caused by alignment.&lt;BR /&gt;&lt;BR /&gt;Using MMS, we built the FOrtran using:&lt;BR /&gt;FORTRAN/noopt/float=ieee_float/align=(commons=(natural,multilanguage),records=natural)&lt;BR /&gt;&lt;BR /&gt;For C:&lt;BR /&gt;cc/noopt/standard=C99/member_alignment/extern_mode=common_block/float=ieee_float&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;We think the build commands are correct, as previously, for Fortran, the records=packed is used.&lt;BR /&gt;&lt;BR /&gt;However, because of the way our legacy was coded, the build commands may not cure the problem.  In a typical declaration, the different data types are interspersed, as opposed to clean ordering. For eg,&lt;BR /&gt;&lt;BR /&gt;INTEGER*2 c1w&lt;BR /&gt;INTEGER*4 c2l&lt;BR /&gt;INTEGER*2 c3w&lt;BR /&gt;REAL*8    costr&lt;BR /&gt;INTEGER*2 c4w&lt;BR /&gt;&lt;BR /&gt;COMMON /xyz/ c1w,c2l,c3w,costr,c4w&lt;BR /&gt;&lt;BR /&gt;Is there a way to resolve this alignment issue without having to change the legacy code (there are countless of such COMMON declaration)?&lt;BR /&gt;&lt;BR /&gt;Thank you very much.</description>
      <pubDate>Thu, 13 Aug 2009 15:52:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479236#M42750</guid>
      <dc:creator>Jenwae</dc:creator>
      <dc:date>2009-08-13T15:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479237#M42751</link>
      <description>If I understand the question and in the C code, you can control alignment at the structure level with the following:&lt;BR /&gt;&lt;BR /&gt;#pragma member_alignment save&lt;BR /&gt;#pragma [no]member_alignment&lt;BR /&gt;whatever data you are looking to control&lt;BR /&gt;#pragma member_alignment restore&lt;BR /&gt;&lt;BR /&gt;The pragmas can also be used to control ref-def and related external addressing, as well.  When working with a common, these are key.  See #pragma extern_model et al.&lt;BR /&gt;&lt;BR /&gt;Personally, I'd look to ditch the Fortran common stuff entirely going forward.  Global sections have various advantages, and the installed commons have various disadvantages.&lt;BR /&gt;&lt;BR /&gt;Though you're probably past this stage, here's some stuff I wrote up on working with Fortran commons in C code, if that's of interest:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://h71000.www7.hp.com/wizard/wiz_2486.html" target="_blank"&gt;http://h71000.www7.hp.com/wizard/wiz_2486.html&lt;/A&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 13 Aug 2009 16:24:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479237#M42751</guid>
      <dc:creator>Hoff</dc:creator>
      <dc:date>2009-08-13T16:24:08Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479238#M42752</link>
      <description>&lt;!--!*#--&gt;Or manually pad the C structures so that the&lt;BR /&gt;(real) C data align with the Fortran data.&lt;BR /&gt;&lt;BR /&gt;Someone had to create the C structures for&lt;BR /&gt;the new C code, right?  How was that done?&lt;BR /&gt;(I see your Fortran example code.  I don't&lt;BR /&gt;see the corresponding C code.)</description>
      <pubDate>Thu, 13 Aug 2009 17:21:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479238#M42752</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2009-08-13T17:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479239#M42753</link>
      <description>Assuming the order of variables in the Fortran common and the C structures are the same, then the length of the Psects is determined by padding.&lt;BR /&gt;Specify  /PSECT_MODEL=MULTILANGUAG in CC, corresponding to  /align=commons=(natural,multilanguage) in Fortran, to get the same Psect size.&lt;BR /&gt;</description>
      <pubDate>Fri, 14 Aug 2009 06:02:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479239#M42753</guid>
      <dc:creator>Joseph Huber_1</dc:creator>
      <dc:date>2009-08-14T06:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479240#M42754</link>
      <description>BTW,&lt;BR /&gt;if compiled with NOMULTILANGUAGE, DECC still padds the Psect to a multiple of 8 bytes, while  Fortran does no padding at all in this case.&lt;BR /&gt;&lt;BR /&gt;So to mix languages always align natural AND multilanguage.&lt;BR /&gt;</description>
      <pubDate>Fri, 14 Aug 2009 08:13:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479240#M42754</guid>
      <dc:creator>Joseph Huber_1</dc:creator>
      <dc:date>2009-08-14T08:13:11Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479241#M42755</link>
      <description>Jenwae,&lt;BR /&gt;&lt;BR /&gt;Needless to say, using the compiler listings to VERIFY that the variable alignments actually are correct is an excellent idea.&lt;BR /&gt;&lt;BR /&gt;Both compilers have various listing options. A careful review of actual printed variable assignments (offsets into structures) would be sound procedure.&lt;BR /&gt;&lt;BR /&gt;- Bob Gezelter, &lt;A href="http://www.rlgsc.com" target="_blank"&gt;http://www.rlgsc.com&lt;/A&gt;</description>
      <pubDate>Fri, 14 Aug 2009 12:33:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479241#M42755</guid>
      <dc:creator>Robert Gezelter</dc:creator>
      <dc:date>2009-08-14T12:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479242#M42756</link>
      <description>Hi All,&lt;BR /&gt;&lt;BR /&gt;Thank you so much for the replies.&lt;BR /&gt;&lt;BR /&gt;Hoff, I almost did that but the application is huge, and to insert that pragma section for each module is not too practical. Also, trying the pragma directives on one individual module reveals that it does not work (seemingly).&lt;BR /&gt;&lt;BR /&gt;Steve:&lt;BR /&gt;The real issue is that the section for Fortran is SMALLER than the C. I want to avoid changing anything on the legacy Fortran.&lt;BR /&gt;Also, when I used the option COMMONS=(NATURAL, RECORDS=NATURAL), instead of the original RECORD=PACKED, the FOrtran section is still SMALLER than C, which I don't understand why.&lt;BR /&gt;&lt;BR /&gt;In the example below:&lt;BR /&gt;&lt;BR /&gt;INTEGER*2 c1w &lt;BR /&gt;INTEGER*4 c2l &lt;BR /&gt;INTEGER*2 c3w &lt;BR /&gt;REAL*8 costr &lt;BR /&gt;INTEGER*2 c4w &lt;BR /&gt;&lt;BR /&gt;the C equivalent (converted automatically) by a translator) is:&lt;BR /&gt;&lt;BR /&gt;short int cw1&lt;BR /&gt;long int c21&lt;BR /&gt;short int c3w&lt;BR /&gt;double costr&lt;BR /&gt;short int c4w&lt;BR /&gt;&lt;BR /&gt;For every short int, the C compiler will pad an additional 2 bytes (to make it natural-aligned). However for Fortran, it still maintain a Integer*2 (without padding) even after using RECORDS=NATURAL&lt;BR /&gt;&lt;BR /&gt;Joseph:&lt;BR /&gt;I tried both multi and nomulti, and all opther option combination...to no avail.&lt;BR /&gt;&lt;BR /&gt;Robert:&lt;BR /&gt;I linked with the option /map. From the map, I could see the section for C is always bigger than that of Fortran for that particular global sections.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 14 Aug 2009 19:59:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479242#M42756</guid>
      <dc:creator>Jenwae</dc:creator>
      <dc:date>2009-08-14T19:59:54Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479243#M42757</link>
      <description>&lt;!--!*#--&gt;&amp;gt; #pragma member_alignment save&lt;BR /&gt;&amp;gt; #pragma [no]member_alignment&lt;BR /&gt;&amp;gt; whatever data you are looking to control&lt;BR /&gt;&amp;gt; #pragma member_alignment restore&lt;BR /&gt;&lt;BR /&gt;This seems to work for me.  Did you try it?&lt;BR /&gt;&lt;BR /&gt;alp $ type cs.c&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;&lt;BR /&gt;main()&lt;BR /&gt;{&lt;BR /&gt;    struct&lt;BR /&gt;    {&lt;BR /&gt;         short int cw1;&lt;BR /&gt;         long int c21;&lt;BR /&gt;         short int c3w;&lt;BR /&gt;         double costr;&lt;BR /&gt;         short int c4w;&lt;BR /&gt;    } cs;&lt;BR /&gt;&lt;BR /&gt;    printf( " cw1:   %2lld.\n", (long long) &amp;amp;cs.cw1- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " c21:   %2lld.\n", (long long) &amp;amp;cs.c21- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " c3w:   %2lld.\n", (long long) &amp;amp;cs.c3w- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " costr: %2lld.\n", (long long) &amp;amp;cs.costr- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " c4w:   %2lld.\n", (long long) &amp;amp;cs.c4w- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " size:  %2d.\n", sizeof cs);&lt;BR /&gt;}&lt;BR /&gt;alp $ cc cs&lt;BR /&gt;alp $ link cs&lt;BR /&gt;alp $ run cs&lt;BR /&gt; cw1:    0.&lt;BR /&gt; c21:    4.&lt;BR /&gt; c3w:    8.&lt;BR /&gt; costr: 16.&lt;BR /&gt; c4w:   24.&lt;BR /&gt; size:  32.&lt;BR /&gt;&lt;BR /&gt;So, the default is 4-byte alignment, but:&lt;BR /&gt;&lt;BR /&gt;alp $ type cs2.c&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;&lt;BR /&gt;main()&lt;BR /&gt;{&lt;BR /&gt;#pragma member_alignment save&lt;BR /&gt;#pragma nomember_alignment&lt;BR /&gt;    struct&lt;BR /&gt;    {&lt;BR /&gt;         short int cw1;&lt;BR /&gt;         long int c21;&lt;BR /&gt;         short int c3w;&lt;BR /&gt;         double costr;&lt;BR /&gt;         short int c4w;&lt;BR /&gt;    } cs;&lt;BR /&gt;#pragma member_alignment restore&lt;BR /&gt;&lt;BR /&gt;    printf( " cw1:   %2lld.\n", (long long) &amp;amp;cs.cw1- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " c21:   %2lld.\n", (long long) &amp;amp;cs.c21- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " c3w:   %2lld.\n", (long long) &amp;amp;cs.c3w- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " costr: %2lld.\n", (long long) &amp;amp;cs.costr- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " c4w:   %2lld.\n", (long long) &amp;amp;cs.c4w- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " size:  %2d.\n", sizeof cs);&lt;BR /&gt;}&lt;BR /&gt;alp $ cc cs2&lt;BR /&gt;alp $ link cs2&lt;BR /&gt;alp $ run cs2&lt;BR /&gt; cw1:    0.&lt;BR /&gt; c21:    2.&lt;BR /&gt; c3w:    6.&lt;BR /&gt; costr:  8.&lt;BR /&gt; c4w:   16.&lt;BR /&gt; size:  18.&lt;BR /&gt;&lt;BR /&gt;If that's not what you want, then what _do_&lt;BR /&gt;you want?  One of us seems to be missing&lt;BR /&gt;something.  You or I?&lt;/STDIO.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Fri, 14 Aug 2009 20:39:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479243#M42757</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2009-08-14T20:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479244#M42758</link>
      <description>I've used the exact technique and the exact construction with the compiler pragmas.  It works in all the cases I've tried.  The pragma-based approach also works nicely in larger applications, when module-wide settings for non-native alignments can be detrimental to application performance.&lt;BR /&gt;&lt;BR /&gt;If you're repeating structures (eg: a C array) within the common, then you can get padding at the end of the structure.&lt;BR /&gt;&lt;BR /&gt;Use the debugger.  Examine memory.  Figure out what is going on.&lt;BR /&gt;&lt;BR /&gt;And chuck the common-based design out the airlock at your first opportunity; position-independent designs are much more flexible.&lt;BR /&gt;</description>
      <pubDate>Fri, 14 Aug 2009 21:02:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479244#M42758</guid>
      <dc:creator>Hoff</dc:creator>
      <dc:date>2009-08-14T21:02:14Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479245#M42759</link>
      <description>&amp;gt;&amp;gt; &lt;BR /&gt;Joseph:&lt;BR /&gt;I tried both multi and nomulti, and all opther option combination...to no avail.&lt;BR /&gt;&lt;BR /&gt;What compilers/system versions do You have ?&lt;BR /&gt;&lt;BR /&gt;Mine, starting with DECC 6.5, Fortran 7.2, VMS 7.3-1 and upwards (Alpha and Itanium) both Fortran and C commons are exactly the same length (32 bytes), and the individual fields as Steven showed, if compiled with natural alignement and multilanguage padding.</description>
      <pubDate>Sat, 15 Aug 2009 08:11:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479245#M42759</guid>
      <dc:creator>Joseph Huber_1</dc:creator>
      <dc:date>2009-08-15T08:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479246#M42760</link>
      <description>&lt;!--!*#--&gt;&amp;gt; What compilers/system versions do You&lt;BR /&gt;&amp;gt; have ? &lt;BR /&gt;&lt;BR /&gt;Why stop there?  Where's the real code?&lt;BR /&gt;Where's the real build procedure?  Where're&lt;BR /&gt;the real results?  What's the real problem?&lt;BR /&gt;&lt;BR /&gt;All we have so far is a lot of hand-waving&lt;BR /&gt;and claims, but no actual failing test case&lt;BR /&gt;with which to work.  _My_ test cases seem to&lt;BR /&gt;work as expected (following advice already&lt;BR /&gt;given here), so I'm happy.</description>
      <pubDate>Sat, 15 Aug 2009 11:32:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479246#M42760</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2009-08-15T11:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479247#M42761</link>
      <description>Jenwae,&lt;BR /&gt;&lt;BR /&gt;Non-natural aligned data is a long standing practice. It is admittedly difficult to imagine in these days of gigabyte hand-held devices, but not that long ago every byte, not to say bit, was precious, and data structures were packed very tightly. Remember, the original VAX-11/780 was sold in configurations of 128Kbytes [VAX-11/780 Hardware Handbook, pp 11, 1978).&lt;BR /&gt;&lt;BR /&gt;The data structures used for interfacing to X-Windows were packed very tightly, and there is associated documentation. The DECnet connection block is another example of a record format that is packed non-naturally aligned. There are many others.&lt;BR /&gt;&lt;BR /&gt;The pragma referred to by Hoff is placed in the external include file for the record definitions, not in "every module". If the exiting code base has COMMON block definitions in every module, that is unfortunate. The convention in C/C++ is to use the #include compiler directive to incorporate an external file (.h) containing record definitions. Typically, the #pragma directives are included in the .h file.&lt;BR /&gt;&lt;BR /&gt;This does work. I would suggest that it would be appropriate to produce a small FORTRAN/C file which demonstrates the problem so that the problem can be seen precisely.&lt;BR /&gt;&lt;BR /&gt;- Bob Gezelter, &lt;A href="http://www.rlgsc.com" target="_blank"&gt;http://www.rlgsc.com&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 15 Aug 2009 13:12:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479247#M42761</guid>
      <dc:creator>Robert Gezelter</dc:creator>
      <dc:date>2009-08-15T13:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479248#M42762</link>
      <description>Jenwae,&lt;BR /&gt;&lt;BR /&gt;One of the followup posts contained:&lt;BR /&gt;&lt;BR /&gt;"Robert:&lt;BR /&gt;I linked with the option /map. From the map, I could see the section for C is always bigger than that of Fortran for that particular global sections."&lt;BR /&gt;&lt;BR /&gt;Note the "linked with the option /map". This will not be particularly useful, while it will highlight the differences in the overall computed lengths.&lt;BR /&gt;&lt;BR /&gt;One wants the COMPILER layouts of structures. This will be in the COMPILER listing. By the time the LINKER is involved, that information is all irrelevant.&lt;BR /&gt;&lt;BR /&gt;- Bob Gezelter, &lt;A href="http://www.rlgsc.com" target="_blank"&gt;http://www.rlgsc.com&lt;/A&gt;</description>
      <pubDate>Sat, 15 Aug 2009 15:10:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479248#M42762</guid>
      <dc:creator>Robert Gezelter</dc:creator>
      <dc:date>2009-08-15T15:10:52Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479249#M42763</link>
      <description>&amp;gt;For every short int, the C compiler will &lt;BR /&gt;&amp;gt;pad an additional 2 bytes (to make it &lt;BR /&gt;&amp;gt;natural-aligned). However for Fortran, &lt;BR /&gt;&amp;gt;it still maintain a Integer*2 (without &lt;BR /&gt;&amp;gt;padding) even after using RECORDS=NATURAL &lt;BR /&gt;&lt;BR /&gt;  I think C is getting it wrong. The natural alignment for a WORD (short int) is WORD alignment (ie: even byte boundaries), so you'd expect either no padding, or a single byte to move the field to an even byte boundary. If C is adding 2 bytes to longword align the field, that's a different type of alignment from the Fortran definition of "natural".&lt;BR /&gt;&lt;BR /&gt;  You may need to manually align your records to ensure they match across languages. Since you say you don't want to change the Fortran, use a compiler listing to determine the exact placement of fields, then use NOMEMBER_ALIGNMENT pragma in C and define the record, if necessary with your own padding to place fields where they need to be.</description>
      <pubDate>Mon, 17 Aug 2009 23:26:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479249#M42763</guid>
      <dc:creator>John Gillings</dc:creator>
      <dc:date>2009-08-17T23:26:01Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479250#M42764</link>
      <description>&lt;!--!*#--&gt;&amp;gt; I think C is getting it wrong. [...]&lt;BR /&gt;&lt;BR /&gt;We still don't know what happens in the&lt;BR /&gt;secret code cited in the original complaint,&lt;BR /&gt;but in the example code which I created from&lt;BR /&gt;the original vague description, the C&lt;BR /&gt;compiler  seems to do fine.  The last two&lt;BR /&gt;"short" guys _are_ 4-byte aligned, but they&lt;BR /&gt;follow properly aligned 4- or 8-byte members&lt;BR /&gt;("long int", "double"), so they get&lt;BR /&gt;better-than-needed alignment as a free bonus.&lt;BR /&gt;&lt;BR /&gt;&amp;gt; &amp;gt;For every short int, the C compiler will&lt;BR /&gt;&amp;gt; &amp;gt;pad an additional 2 bytes (to make it&lt;BR /&gt;&amp;gt; &amp;gt;natural-aligned).[ ...]&lt;BR /&gt;&lt;BR /&gt;Remember, we have only this assertion, with&lt;BR /&gt;no actual evidence for it.  What's true in my&lt;BR /&gt;actual example code is that padding is added&lt;BR /&gt;_after_ a "short int", so that the thing&lt;BR /&gt;_after_ that "short int" is getting naturally&lt;BR /&gt;aligned.  No one is doing any padding to&lt;BR /&gt;super-align the "short int" members.  The&lt;BR /&gt;only padding done is to align naturally the&lt;BR /&gt;guys which follow them.  Which is what I'd&lt;BR /&gt;expect.  Isn't it?  I think that that's what&lt;BR /&gt;I expect.</description>
      <pubDate>Tue, 18 Aug 2009 02:50:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479250#M42764</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2009-08-18T02:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479251#M42765</link>
      <description>&amp;gt;&amp;gt; &lt;BR /&gt;I think C is getting it wrong. The natural alignment for a WORD (short int) is WORD alignment (ie: even byte boundaries), &lt;BR /&gt;&lt;BR /&gt;No, C is doing it exactly right: just arrange the shiort words in sequence, and they are aligned realluy naturally, i.e. on consecutive word addresses as one would assume. There is no 4 byte default or padding.&lt;BR /&gt;Only at the end of a structure C padds to the next full 8 byte boundary, so that the total length is always a multiple of 8.&lt;BR /&gt;Fortran does the same natural alignment, it just does not pad at the end of a common unless "multilanguage" is specified, then both compiklers padd to the next 16-byte multiple.&lt;BR /&gt;&lt;BR /&gt;Just see Stevens example with rearranged variables below, with 2 consecutive shorts, and the single byte at the end forcing size=40.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;    struct&lt;BR /&gt;    {&lt;BR /&gt;         short int cw1;&lt;BR /&gt;         long int c21;&lt;BR /&gt;         short int c3w;&lt;BR /&gt;         short int c4w;&lt;BR /&gt;         char b1[3];&lt;BR /&gt;         short int cw5;&lt;BR /&gt;         char b2[3];         &lt;BR /&gt;         double costr;&lt;BR /&gt;         char b3;&lt;BR /&gt;    } cs;&lt;BR /&gt;&lt;BR /&gt;    printf( " cw1:   %2lld.\n", (long long) &amp;amp;cs.cw1- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " c21:   %2lld.\n", (long long) &amp;amp;cs.c21- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " c3w:   %2lld.\n", (long long) &amp;amp;cs.c3w- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " c4w:   %2lld.\n", (long long) &amp;amp;cs.c4w- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( "  b1:   %2lld.\n", (long long) &amp;amp;cs.b1- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " cw5:   %2lld.\n", (long long) &amp;amp;cs.cw5- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( "  b2:   %2lld.\n", (long long) &amp;amp;cs.b2- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " costr: %2lld.\n", (long long) &amp;amp;cs.costr- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( "  b3: %2lld.\n", (long long) &amp;amp;cs.b3- (long long) &amp;amp;cs);&lt;BR /&gt;    printf( " size:  %2d.\n", sizeof cs);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;_HUB&amp;gt;cc commonc2.c&lt;BR /&gt;_HUB&amp;gt;link commonc2&lt;BR /&gt;_HUB&amp;gt;run commonc2&lt;BR /&gt; cw1:    0.&lt;BR /&gt; c21:    4.&lt;BR /&gt; c3w:    8.&lt;BR /&gt; c4w:   10.&lt;BR /&gt;  b1:   12.&lt;BR /&gt; cw5:   16.&lt;BR /&gt;  b2:   18.&lt;BR /&gt; costr: 24.&lt;BR /&gt;  b3: 32.&lt;BR /&gt; size:  40.&lt;BR /&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Tue, 18 Aug 2009 06:37:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479251#M42765</guid>
      <dc:creator>Joseph Huber_1</dc:creator>
      <dc:date>2009-08-18T06:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479252#M42766</link>
      <description>Hello to All of You,&lt;BR /&gt;&lt;BR /&gt;Sorry for my late reply. I've been trying out a few things as per your suggestion.&lt;BR /&gt;&lt;BR /&gt;OK, some of the findings to your suggestions:&lt;BR /&gt;&lt;BR /&gt;Using the #pragma directives (noalignment) for this BFCCOM struct, it works brilliantly as Hoff, Steve and Robert suggested. This approach works after I build the Fortran without alignment too, using /align=(commons=packed,record=packed).&lt;BR /&gt;&lt;BR /&gt;Joseph suggested using the /psect_mode=multilanguage. It works brilliantly, after I adjust the Fortran option /align=(commons=natural,record=natural).&lt;BR /&gt;&lt;BR /&gt;So the clue is to use the right combination. Thank you so much to all of you. I really appreciate that. I will use Joseph's solution so that I can avoid inserting the directives.&lt;BR /&gt;&lt;BR /&gt;There's still some peculiarity as to how the alignment actually works. The compiler does not behave consistently, or I could have understood the wrong way. Anyway, the content below is just for curiosity as to how the sizes can differ.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;To make it more understandable,  I'm using the similar code definition.&lt;BR /&gt;&lt;BR /&gt;The Fortran header file BFCCOM.INC looks like this:&lt;BR /&gt;&lt;BR /&gt;        INTEGER*2       DATEW(400) &lt;BR /&gt;        INTEGER*4       FILEL(400) &lt;BR /&gt;        INTEGER*4       SIZEL(400) &lt;BR /&gt;        INTEGER*2       CNDXW&lt;BR /&gt;        INTEGER*2       CTOPW        &lt;BR /&gt; LOGICAL*1       LIVT(400)  &lt;BR /&gt;        BYTE            CURB&lt;BR /&gt;        INTEGER*4       BITL(768)&lt;BR /&gt;        BYTE     CEND&lt;BR /&gt;&lt;BR /&gt;        COMMON /BFCCOM/ DATEW, FILEL, SIZEL,&lt;BR /&gt;               CNDXW, CTOPW, LIVT, CURB,&lt;BR /&gt;               BITL, CEND&lt;BR /&gt;&lt;BR /&gt;The converted C equivalent is:&lt;BR /&gt;&lt;BR /&gt;typedef struct bfccom {&lt;BR /&gt;        short int  datew[400]; &lt;BR /&gt;        long int  filel[400]; &lt;BR /&gt;        long int  sizel[400]; &lt;BR /&gt;        short int  cndxw; &lt;BR /&gt;        short int  ctopw; &lt;BR /&gt;        BYTE   livt[400];&lt;BR /&gt;        BYTE   curb;    &lt;BR /&gt;        long int  bitl[768]; &lt;BR /&gt;        BYTE   cend;&lt;BR /&gt;} BFCCOM;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;extern BFCCOM bfccom;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;In our legacy system, we have a abc.FOR program calling a def.FOR subroutine, and both have the BFCCOM common as include, so that they can share data.&lt;BR /&gt;&lt;BR /&gt;In our "new" system, we are converting code to C, but at this point of time, we need to inter-mix C and Fortran.&lt;BR /&gt;So we changed to abd.C calling the same def.FOR (C calling Fortran).&lt;BR /&gt;Obvously, we need to map the BFCCOM in C to match the one included in def.FOR so that they can share data, as before.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Hence, I compile the C using the option cc/extern_mode=common_block/member_alignment/standard=C99/float=ieee_float&lt;BR /&gt;&lt;BR /&gt;I compile the Fortran using important option such as /align=(commons=natural,records=natural)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;IN the abc.C routine, I did a series of printf statements for both the sizes and the starting addresses of the members. I get (size, then start address)&lt;BR /&gt;&lt;BR /&gt;datew = 800  -&amp;gt; 0&lt;BR /&gt;filel = 1600  -&amp;gt; 800&lt;BR /&gt;sizel = 1600 -&amp;gt; 2400&lt;BR /&gt;cndxw = 2 -&amp;gt; 4000&lt;BR /&gt;ctopw = 2 -&amp;gt; 4002&lt;BR /&gt;clivt = 400 -&amp;gt; 4004&lt;BR /&gt;curb = 1 -&amp;gt; 4404&lt;BR /&gt;bitl = 3072 -&amp;gt; 4408&lt;BR /&gt;cend = 1 -&amp;gt; 7480&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;In the abc.FOR routine, I did the same prints, and got the same result. (the map in def.FOR is same as abc.FOR, so I use abc.FOR for convenience).&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;However, when I look at the map produced by each C and Fortran build, the length is different! &lt;BR /&gt;&lt;BR /&gt;In the C build, I see:&lt;BR /&gt;&lt;BR /&gt;Psect Name  Module/Image  Length&lt;BR /&gt;BFCCOM&lt;BR /&gt;              ABC          (       7488.)&lt;BR /&gt;              DEF          (       7481.)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;IN the Fortran build, I see:&lt;BR /&gt;&lt;BR /&gt;Psect Name  Module/Image  Length&lt;BR /&gt;BFCCOM&lt;BR /&gt;              ABC          (       7481.)&lt;BR /&gt;              DEF          (       7481.)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Somehow, after the build, the BFCCOM is extended by another 7 bytes in the C program! Because of this, ABC cannot successfuly share data with DEF.&lt;BR /&gt;&lt;BR /&gt;I'm not sure where the padding happens, but the sizes of the following might help:&lt;BR /&gt;1) size of bfccom object/instance = 7484&lt;BR /&gt;2) sum of struct's (bfccom) member sizes = 7478&lt;BR /&gt;3) size of the BFCCOM in the map = 7488&lt;BR /&gt;&lt;BR /&gt;I suppose C compiler pads the "bytes" CURB and CEND to natural-align to long (4 bytes), and this additional 6 bytes increase size from (2) to (1). Maybe someone can explain if you have the time.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks!</description>
      <pubDate>Tue, 18 Aug 2009 23:04:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479252#M42766</guid>
      <dc:creator>Jenwae</dc:creator>
      <dc:date>2009-08-18T23:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479253#M42767</link>
      <description>I'd probably use C pointers and the Structure Definition Language (SDL) here, and create and assign data structures.   SDL lets you declare the data structures, and then load the definitions into every module using includes.  And you can use these same definitions across a mixture of programming languages; C or Fortran or whatever.  (This is how OpenVMS itself is built; using SDL.)&lt;BR /&gt;&lt;BR /&gt;I'd not look to use strict COMMON mapping, and would not look to try to force-fit a Fortran design into some new C code.&lt;BR /&gt;&lt;BR /&gt;You're writing new C code, after all.  Not old Fortran code.&lt;BR /&gt;&lt;BR /&gt;If you really want to do this without SDL, I'd use the following:&lt;BR /&gt;&lt;BR /&gt;typedef struct bfccom { &lt;BR /&gt;short int *datew; &lt;BR /&gt;long int *filel; &lt;BR /&gt;long int *sizel; &lt;BR /&gt;short int *cndxw; &lt;BR /&gt;short int *ctopw; &lt;BR /&gt;BYTE *livt; &lt;BR /&gt;BYTE *curb; &lt;BR /&gt;long int *bitl; &lt;BR /&gt;BYTE *cend; } DFCCOM; &lt;BR /&gt;&lt;BR /&gt;And set the pointers to the COMMON at run-time.&lt;BR /&gt;&lt;BR /&gt;In particular, please stop replicating the existing Fortran limits and the existing designs.  For instance, there are fixed-size arrays in the Fortran code.  You don't need to do that when you're not using a COMMON, so (here) set up to use the COMMON, and when you excise the last of the Fortran code from your environment you can then use C and RTL and OpenVMS system service calls to adjust and tune and even increase the size of this stuff on the fly.&lt;BR /&gt;&lt;BR /&gt;If you replicate the exact design, you can end up replicating the same old limits.&lt;BR /&gt;</description>
      <pubDate>Wed, 19 Aug 2009 00:18:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479253#M42767</guid>
      <dc:creator>Hoff</dc:creator>
      <dc:date>2009-08-19T00:18:41Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479254#M42768</link>
      <description>&amp;gt;&amp;gt;&lt;BR /&gt;&lt;BR /&gt;I suppose C compiler pads the "bytes" CURB and CEND to natural-align to long (4 bytes), and this additional 6 bytes increase size from (2) to (1). Maybe someone can explain if you have the time.&lt;BR /&gt;&amp;gt;&amp;gt;&lt;BR /&gt;&lt;BR /&gt;Don't suppose: the C padding happens at the end of the struct up to the next 8-byte border, not for individual members, as I have shown in a previous reply.&lt;BR /&gt;You (purpeously ?) omitted the MULTILANGUAGE option in the Fortran align, and the /PSECT_MODE=MULTILANGUAGE option in CC:&lt;BR /&gt;this forces both compilers to pad up to the next 16-byte border. The member alignment stays as is with natual, but the total length of the psect/common will be the same for both languages.&lt;BR /&gt;</description>
      <pubDate>Wed, 19 Aug 2009 04:23:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479254#M42768</guid>
      <dc:creator>Joseph Huber_1</dc:creator>
      <dc:date>2009-08-19T04:23:26Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran and C alignment</title>
      <link>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479255#M42769</link>
      <description>Try the following and I think it should work&lt;BR /&gt;$ ccxd == "CC/DecC/noopt/noWarn/Float=D_Float/SHARE_GLOBALS/extend/" + -&lt;BR /&gt;         "EXTERN_MODEL=COMMON_BLOCK/deb/lis"</description>
      <pubDate>Wed, 19 Aug 2009 11:46:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-openvms/fortran-and-c-alignment/m-p/4479255#M42769</guid>
      <dc:creator>circepb</dc:creator>
      <dc:date>2009-08-19T11:46:04Z</dc:date>
    </item>
  </channel>
</rss>

