- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Good morning all,
I have the feeling I may have a problem hiding away regarding interoperability of my Fortran code with C. I'm using C to allocate some page-aligned memory, and on the C side I'm using size_t as the data type of the "bytes to allocate" argument. I initially set up my Fortran side to pass a variable declared as INTEGER (KIND=C_SIZE_T), however I'm realizing now that Fortran doesn't have a concept of unsigned data types (and the SYSV definition of size_t is "long unsigned int"). So presumably, on 32-bit architectures, Fortran will only be able to request at most 2GB of memory. I would prefer to keep my programs portable (since the C routines can be called by other languages too), but is just doing a blanket "unsigned long long" & "INTEGER*8" for these data types my only solution?
Thanks
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
So you want to pass a value larger than 2**31 from Fortran to C? Do you really think you'll be able to do an allocation of that size on a 32-bit platform? I'm skeptical.
Interoperability pretty much glosses over the notion of unsigned ints - INTEGER(C_SIZE_T) is a variable that is the same number of bits as a C size_t variable, so that's really what counts.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Not on a 32-bit platform, no. However, if the math that computes how much memory we'll need (which is done before the call to the C routine) ends up overflowing the Fortran storage variable, C won't be able to detect this (or Fortran, for that matter) and the allocate call will likley succeed, but allocate the wrong amount of memory. So in the end you'll get a program that won't crash until probably well after the actual failure point, and will probably fail with a hard SIGSEGV error rather than the more appropriate (and soft) ENOMEM error at the point of failure (posix_memalign(), in my case). This problem can be compounded even further on Linux, where overcommit can allow you to allocate obnoxiously large portions of memory, beyond what you may have actually available on your system.
And I understand the goal of the interop subsystem is to provide compatible data type sizes, however the gotcha is that while the variable sizes are equivalent for passing them from C to Fortran and then back again to C for additional use, reading or writing these variables in Fortran is dangerous due to the different signedness of the data type.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You could detect it before the call by checking to see if the size is negative. You could also do the computation in an INTEGER(C_INT64_T) variable and then test it to see if it is "too big".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ereisch,
You can use unsigned (8-bit, 16-bit, 32-bit, and 64-bit) integers in Fortran provided that you supply the operators. Awkward using user defined types and generic functions.
As for computing unsigned numbers up to 2**32-1. use 64-bit numbers for the calculation then extract the low 32-bits. Be careful in using the result in any calculation or comparison on the Fortran side.
Jim Dempsey

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page