From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111c93,11734b1d8c3fcb1c,start X-Google-Attributes: gid111c93,public From: b_duke@my-dejanews.com Subject: A Fortran/C code malloc problem Date: 1999/03/24 Message-ID: <7d9vrc$clg$1@nnrp1.dejanews.com>#1/1 X-Deja-AN: 458264143 X-Http-Proxy: 1.0 x1.dejanews.com:80 (Squid/1.1.22) for client 138.80.63.85 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Wed Mar 24 06:14:09 1999 GMT Newsgroups: comp.os.linux.alpha X-Http-User-Agent: Mozilla/3.01 (Win95; I) 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