Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29393 Discussions

Memory allocation using Cray pointer and malloc (of C)

psr1978
Beginner
1,905 Views
Halo All,

I am FORTRAN user, and encountered forllowing problem.

I am alocating memory for the fortran arrays using cray type pointer and malloc function of C. Here is simple program which describes my usage:

Program MemTest
implicit none

integer*4, parameter :: Len=4000,Size=8

integer*4 :: Array1(Size,Len)

integer*4 :: ii, memalloc

pointer (Array1Ptr,Array1)

Array1Ptr = memalloc(Size*Len*4)

do ii = 1, Len

Array1(1,ii) = ii*2
Array1(2,ii) = ii*3
Array1(3,ii) = ii*4

end do

stop

end

#include
#include
#include

int *memalloc_(int *length)

{
int *alocptr;

alocptr = (int *) malloc((unsigned) *length);

if (alocptr == 0) {
printf("Unable to allocate --> %d bytes)\n", *length);

}
return(alocptr);
}


Compiling with ifort -- Intel 64, Version 11.1 Build 20090827 Package ID: l_cprof_p_11.1.056.
It works well below and eqal LEN=4000, but does not work further. Any help.. Thank you in advance..

Regards,

PS

0 Kudos
7 Replies
Steven_L_Intel1
Employee
1,905 Views
What doesn't work? Were you aware that Intel Fortran supports a "malloc" intrinsic (and "free")? Have you considered using ALLOCATABLE arrays instead?
0 Kudos
psr1978
Beginner
1,905 Views
What doesn't work? Were you aware that Intel Fortran supports a "malloc" intrinsic (and "free")? Have you considered using ALLOCATABLE arrays instead?

First, i am sorry for late repsonce and thank you for your responce.

As i mentioned earlier, if LEN > 4000, the following error message will be delivered.

forrtl: severe (174): SIGSEGV, segmentation fault occurred

If LEN < or = 4000, every thing is fine.

To other HINTS,
My actual code (this is a TOY which i have made to show here) should also be compiled with other compilers, so i am not using malloc of intel FORTRAN.
The code was developed for F77. So i had old syntax.

Anyhow, thank you for your responce and HINTs.


Thank you.

PS

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,905 Views

What is the sizeof(int) on the C++ side?
If 8 then the *length may be returning junk in the high 4 bytes.

Jim Dempsey

0 Kudos
psr1978
Beginner
1,905 Views

What is the sizeof(int) on the C++ side?
If 8 then the *length may be returning junk in the high 4 bytes.

Jim Dempsey

Thank you Jim,

I have checked and the following shows the sizeof(int) is OK.
Size of int types is 4 bytes
Signed int min: -2147483648 max: 2147483647
Unsigned int min: 0 max: 4294967295

Thank you.

PS

0 Kudos
IanH
Honored Contributor III
1,905 Views
Quoting - psr1978
ifort -- Intel 64, Version 11.1 Build 20090827 Package ID: l_cprof_p_11.1.056
...
Size of int types is 4 bytes

A guess as I'm not familiar with the combination of intel fortran/unspecified C compiler/Intel 64/linux:

Your C function doesn't return an int, it returns an int*, which is probably 8 bytes on a 64 bit system. You are then squeezing that into an INTEGER(4), which is probably still only four bytes in that environment. You then pretend it's still a valid pointer. With it's head (or bottom?) chopped off that cray pointer develops some significant personality disorders - some of the time it works and some of the time it doesn't.

If this guess is off, then some other calling convention related thingy would be what I'd look at next.

As others have indicated, if you are only doing this to overcome the "no dynamic memory allocation" limitations of F77, then I'd be ditching the whole C malloc/cray pointer side of things and using ALLOCATABLE variables. If you must still "interoperate" with C for other reasons, then I'd be using the F2003 standard Fortran-C interoperability feature. There are other fortran/C compiler pairings on linux and windows that support this. Otherwise, I'd regard the ability for the source to be "compiled with other compilers", mentioned in a previous post, as more of an illusion rather than a reality.


0 Kudos
TimP
Honored Contributor III
1,905 Views
There are c_ptr data types both in iso_c_binding and in the older ifort macro list. No need to guess whether it will be compiled for 32 or 64-bit mode.
0 Kudos
John4
Valued Contributor I
1,905 Views
Quoting - psr1978

My actual code (this is a TOY which i have made to show here) should also be compiled with other compilers, so i am not using malloc of intel FORTRAN.
The code was developed for F77. So i had old syntax.

Having an old syntax (i.e., fixed source form), doesn't mean you cannot use any of the F90+ features. As others already suggested, unless some of the "other compilers" you mentioned lack support for the Fortran 90 standard, ALLOCATABLE arrays seem to be your best option in this case.

0 Kudos
Reply