Mailinglist Archive: opensuse-programming (60 mails)

< Previous Next >
Re: [suse-programming-e] C-Fortran interface
  • From: Jerry Feldman <gaf@xxxxxxx>
  • Date: Tue, 6 Jun 2006 17:43:26 -0400
  • Message-id: <200606061743.26218.gaf@xxxxxxx>
On Tuesday 06 June 2006 5:06 pm, Maura Edeweiss Monville wrote:
> Thak you. Worked examples are always welcome to understand the rules.
> It's not clear if the C external struct must be embedded in that "pack"
> declaration. In some other similar websites it is stated that a
> particular include file must be included in the C module ... This is
> confusing ...
A C struct can cause many problems:
Unpacked in a 64-bit Linux/Unix system:
struct {
char a; /* starts on a 64-bit boundary */
long b; /* Also starts on a 64-bit boundary */
} s;
The size of s is 16 bytes with a 7 byte filler between a and b.
On a 32-bit system,
a will start on a 32-bit boundary, and b will start on a 32-bit boundary
with a 3 byte filler leaving the size of s as 8 bytes.
In a packed struct, a will start on a 64-byte boundary assuming it is not
included inside a packed structure. But, b will start immediately after a
with no filler so the 64-bit version of s is 9 bytes.

Remember that floats (32-bits) and doubles(64-bits) are defined by the IEEE
754 standard (on a Linux and Unix platform). Integers in FORTRAN and C will
be the same on the same platform as long as you understand the difference
in sizes in C and FORTRAN. Structures are another matter as they are
defined by the C standards and the alignments are "implementation defined".
I don't recall if the C standards require variables to be naturally aligned
or not. It does require that a structure start on a natural boundary. And,
as I mentioned, C arrays are row-major and FORTRAN arrays are column major.
Strings in FORTRAN as I remember have a leading length attribute and no
trailing nul.

(I'm going home :-).


--
Jerry Feldman <gaf@xxxxxxx>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9

< Previous Next >
Follow Ups