HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

Alignement of Structure in Fortran and C

 
sandip bhoir
Occasional Visitor

Alignement of Structure in Fortran and C

I have a Follwing Fortran Structures as below:

1) Fortran Structure:
structure /Point/
union
map
REAL x,y,z
end map
map
REAL coor(3)
endmap
end union
end structure

2) Fortran Structure containing another Fortran Structure
struct COMP
{
Point pt;
INTEGER ext;
}

3)Fortran Structure
struct NESTED
{
COMP cp(10)
INTEGER tcp;
}

Can anybody help in writing properly aligned C Structure for the above fortran structures.
8 REPLIES
Ian Miller.
Honored Contributor

Re: Alignement of Structure in Fortran and C

I will guess at

typedef union
{
float x,y,z;
float coor[3];
} Point;

typedef struct
{
Point pt;
int ext;
} COMP;

typedef struct
{
COMP cp[10];
int tcp;
} NESTED;
____________________
Purely Personal Opinion
sandip bhoir
Occasional Visitor

Re: Alignement of Structure in Fortran and C


First of all, i want to correct my question.
structure for COMP and nested are as follow:
struct COMP
{
record /Point/ pt;
INTEGER ext;
}

struct NESTED
{
record /COMP/ cp(10)
INTEGER tcp;
}

I tried with the same C Strucutres as you suggested. But it result in mis alignment hence incorrect data is shared across fortran and C.

I think its something to do with
Hein van den Heuvel
Honored Contributor

Re: Alignement of Structure in Fortran and C

>> I tried with the same C Strucutres as you suggested. But it result in mis alignment hence incorrect data is shared across fortran and C.

Have you tried to identify the difference, for example by printing out address (offsets) for component fields?

What compiler switches are used?
What platform? VAX floats or IEEE?
Check out the HELP text for HELP CC /FLOAT

Now compile with /LIST and /SHOW=MAP (C: /SHOW=SYMBOLS and looks for thing like the FLOAT option(s) used.
The Fortran compiler listign might show some things like:

/DOUBLE_SIZE=64
/FLOAT=G_FLOAT
/GRANULARITY=QUADWORD
/IEEE_MODE=FAST
/INTEGER_SIZE=32
/REAL_SIZE=32

Now let that sink in, compare to C, and what you see in the programs.
Use Debugger for final confirmation

In the debugger I like EVAL/ADD after SET MODE HEX for TEST programs. You see addresses like 10000 or 30000 as start of those test structure and offsets for the fields are very obvious

Cheers,
Hein



Graham Burley
Frequent Advisor

Re: Alignement of Structure in Fortran and C

If alignment seems to be the issue you might try the following pragmas around your C structures:

#pragma member_alignment save
#pragma nomember_alignment
struct ...
}
#pragma member_alignment restore
Mike Kier
Valued Contributor

Re: Alignement of Structure in Fortran and C

On the Fortran side, any time I have a case where the physical layout of a structure must be maintained, I explicitly define the length of the component variables (I*8, I*4, I*2, R*4, etc.) and I put a CDEC$ OPTIONS /ALIGN=...CDEC$ END OPTIONS directive around the structure and the record definitions (OK, I'm paranoid) and put the STRUCTURE (or TYPE) definition into an INCLUDEd file (or MODULE) so every routine uses the same definition.

If using the F95 TYPE definition instead of the DEC Fortran STRUCTURE/RECORD extension, include the STORAGE attribute.

This way you are as insulated as possible from external factors such as the presence or omission of compiler switches that affed default Integer/Real sizes and alignment.
Practice Random Acts of VMS Marketing
sandip bhoir
Occasional Visitor

Re: Alignement of Structure in Fortran and C


Misalignment problem occurs only because i am using record /Point/ and
record /COMP/ cp(10)

However i have previously worked on simple structure containing INTEGER,REAL,LOGICAL.... and alignments work properly with my compiler settings.
But in this case structure (NESTED) contains Userdefined structure (Point and COMP)
Hoff
Honored Contributor

Re: Alignement of Structure in Fortran and C

Sharing data structures as has been requested here is a comparatively old software design technique; it works, but it does tend to be error-prone, and it tends to create maintenance headaches.

It can be preferable to use SDL for these definitions and for related definitions, as that usage reduces the likelihood of errors, and as it eases maintenance, and as it allows the structures to be documented via SDML (if the DEC Document package is available), and as it forces some discipline around the structures.

This SDL usage does presuppose control over the Fortran and the C source code involved.

Regardless of whether you might choose to use SDL here, embedding and implementing and checking a version number within the structures can be an invaluable technique for avoiding skews; this if you're working with a common or other such technique, and don't rebuild all code referencing the shared data as a unit. This means you can detect and flag cases where the (shared) data structure has changed.

SDL-related discussions:

http://labs.hoffmanlabs.com/node/588
http://labs.hoffmanlabs.com/node/595

John Gillings
Honored Contributor

Re: Alignement of Structure in Fortran and C

Sandip,

Rather than start with your existing Fortran structure and try to force another language to match it, look at the structure itself and put it in a form which naturally fits the data alignment models of all languages you want to define it in.

The first thing I'd suggest is to move your INTEGER fields to the BEGINNING of the nested structures and then pad to quadword align the embedded structure. I'd also pad out the "Point" structure to an even number of longwords. The simplest way to keep everything aligned is to make them all naturally aligned sizes.

Thus:

structure /Point/
union
map
INTEGER*4 padding(4) ! force size
end map
map
REAL x,y,z
end map
map
REAL coor(3)
endmap
end union
end structure

struct COMP
{
int ext;
int pad1;
Point pt;
}

struct NESTED
{
int tcp;
int pad2;
COMP cp(10)
}

You can do the same with appropriate alignment qualifiers, pragmas or compiler directives. Make sure you generate full compiler listings and check the offsets of all fields to ensure they line up.

Even better is to do what Hoff suggests and use a utility like SDL whos whole reason for existence is to solve the problem you're asking about.
A crucible of informative mistakes