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

Error using malloc to allocate memory

kai_long_mi
Beginner
1,492 Views
I am using ifort to compile my Fortran source codes. There is a problem when running the one of the functions to allocate memories. The function 'rmalloc' returns an negative value, which is not correct, once the 'size' parameters is larger than 70000. There is no such error when the 'size' parameter less than 2260. Any suggestion is appreciated.


function rmalloc (size,at,total)
integer size,amount,idx,rmalloc,total
integer(C_INTPTR_T) ptr, block
dimension at(*)
pointer (ptr,a(1)),(block,b(1))
ptr = %loc(at)
amount = 4*size + 8
block = malloc(amount)
do i=1,size+1
b(i) = 0
enddo
rmalloc = (block - ptr)/4 + 1
total = total + amount/4
return
end


0 Kudos
9 Replies
Steven_L_Intel1
Employee
1,492 Views

Are you using a 64-bit compiler? If so, your use of "integer" for block, etc. is wrong. I suggest declaring address-sized integers as:

integer(C_INTPTR_T)

where C_INTPTR_T is defined in intrinsic module ISO_C_BINDING.
0 Kudos
kai_long_mi
Beginner
1,492 Views

Yes. I am compiling the codes on the 64-bit system.The segment error happens when the 'size' parameter equals 70000, there is no such error when the 'size' parameter less than 2260.When I modify the 'ptr' and 'block' to use 'integer(C_INTPTR_T) ptr, block'. There is a compiling error like 'error #6683: A kind type parameter must be a compile-time constant. [C_INTPTR_T]'. I simplified the function like below.

function rmalloc (size,at,total)
integer size,amount,idx,rmalloc,total
integer(C_INTPTR_T) ptr, block
dimension at(*)
pointer (ptr,a(1)),(block,b(1))
ptr = %loc(at)
amount = 4*size + 8
block = malloc(amount)
do i=1,size+1
b(i) = 0
enddo
rmalloc = (block - ptr)/4 + 1
total = total + amount/4
return
end

0 Kudos
Steven_L_Intel1
Employee
1,492 Views

Add:

use, intrinsic :: iso_c_binding

just after the "function" statement.

Did you edit the code in your initial post?
0 Kudos
kai_long_mi
Beginner
1,492 Views
Thank you, Steve. Yes, I simplified the intial post for easier to view. I tried adding intrinsic :: iso_c_binding rightbehind the 'function rmalloc (size,at,total)'. There is a compiling error says 'error #6407 This symbolic name is not an intrinsic function name or an intrinsic subroutine name [ISO_C_BINDING]'






Add:

use, intrinsic :: iso_c_binding

just after the "function" statement.

Did you edit the code in your initial post?

0 Kudos
Steven_L_Intel1
Employee
1,492 Views

You forgot the keyword "use".

use, intrinsic :: iso_c_binding
0 Kudos
kai_long_mi
Beginner
1,492 Views
Yes. You are right. I forgot the 'use'. Since this function is used by other main programs. Should I also define integer(C_INTPTR_T) for the variables which uses this function? Is this (C_INTPTR_T) same as I define integer *8 ptr, block for the 64-bit system? Thank you.

0 Kudos
Steven_L_Intel1
Employee
1,492 Views

You should use this to declare any address-sized integer variables. It will select the proper size for 32-bit or 64-bit compilations.

However, if you name the variable as the pointer in an "integer POINTER" statement, as is shown currently in your source, you can skip declaring it entirely and the compiler will declare it to the correct size.
0 Kudos
kai_long_mi
Beginner
1,492 Views
Thank you Steve. I tried this method. There are too many variables in the source codes. The major problem is that the source code is written in 32-bit, while our computing system is 64-bit now. Is there any method could run 32-bit code on 64-bit system without changing the source code.

You should use this to declare any address-sized integer variables. It will select the proper size for 32-bit or 64-bit compilations.

However, if you name the variable as the pointer in an "integer POINTER" statement, as is shown currently in your source, you can skip declaring it entirely and the compiler will declare it to the correct size.

0 Kudos
Steven_L_Intel1
Employee
1,492 Views

You would have to build with the 32-bit compiler, which is likely to require that you install additional 32-bit development components as specified in the Intel Fortran release notes.
0 Kudos
Reply