From: b_duke@my-dejanews.com
Subject: A Fortran/C code malloc problem
Date: 1999/03/24 [thread overview]
Message-ID: <7d9vrc$clg$1@nnrp1.dejanews.com> (raw)
I have been struggling for some weeks with the port of an established
program to Alpha linux. It is a fortran code that calls a small
amount of C which in turn calls malloc to allocate dynamic memory.
The offset of the memory so allocated from the beginning of an
array X(1) is then used to access various areas. I'm not interesting
in changing this. It is'nt my code and it works on almost every
other machine except alpha linux. I just want to get it to work.
I am aware that malloc returns a long int pointer and that long
int is 8 bytes on alpha linux, while it is 4 bytes on PC Linux.
The code below isolates the problem from the main program. It
seems to give exactly the results I get from the main program.
I have tried to force everything that should be IINTEGER*8 to be
so, even to making the functions external so I can declare them.
Program TEST
IMPLICIT NONE
DOUBLE PRECISION X
INTEGER*8 LOCMEM,LOCX,LOFFS1,LOFFS2,MEMGET,LADDRS
INTEGER*4 MEMLIM,LOFFS
COMMON /F/ X(1)
EXTERNAL MEMGET
EXTERNAL LADDRS
LOCX=LADDRS(X(1))
MEMLIM = 100000
LOCMEM = MEMGET(MEMLIM)
C Allocate MEMLIM words (8 bytes) of memory.
LOFFS1 = LOCMEM - LOCX
LOFFS2 = (LOFFS1+7)/8 + 1
C WE NEED LOFFS TO BE I*4
LOFFS = LOFFS2
WRITE(*,1) LOCMEM
WRITE(*,1) LOCX
WRITE(*,1) LOFFS1
WRITE(*,1) LOFFS2
WRITE(*,1) LOFFS
1 FORMAT(1x,I16)
STOP
END
with this C linked in:-
long memget_(nwords) long *nwords;
{ unsigned int nbytes;
long nn;
nbytes = (*nwords+2)*8;
nn = (long) malloc(nbytes);
printf("size of nn = %ld nn = %ld\n",sizeof(nn),nn);
return nn; }
/* Taking out the nn stuff and doing return malloc gives the
same result. */
long laddrs_(arg) long arg;
{ return(arg); }
laddrs is used in place of loc to give the address of X(1), so
I can see what is going on, but loc gives the same results.
Running this on the alpha gives:-
size of nn = 8 nn = 1466015604752
1466015604752
4832925752
1461182679000
182647834876
-2035758852
long is *8 and the number in memget is the same as the one coming out.
On a PC under linux, I get:-
size of nn = 4 nn = 1074581512
1074581512
134546552
940034960
117504371
117504371
after altering the INTEGER*8 to INTEGER*4 and this seems fine.
The problem is that the number returned by malloc is far too
large giving an offset from X(1) that is far too large. It is
182 Gb and can not be converted to an INTEGER*4 required for
the rest of the code to access the memory.
Can anyone explain what is going on here? Have I blundered
with the code? Can you reproduce this on other alpha linux
systems? I would welcome any assistence. Please e-mail your
replies and I will summarise to comp.os.linux.alpha.
Brian Salter-Duke.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
next reply other threads:[~1999-03-24 5:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-03-24 5:00 b_duke [this message]
1999-03-24 5:00 ` A Fortran/C code malloc problem Robert Harley
1999-03-25 5:00 ` b_duke
1999-03-25 5:00 ` Greg Lindahl
1999-03-25 5:00 ` Larry Zimmerman
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox