The program runs perfectly on 32 bit Linux. It compiles on 64 bit but gives the following errors.
I have four files. One of these (named STUFF) is included into the Fortran file named A.f. A.f calls routines in file B.c.
The third file (named B.c) is a C file that has three functions. If I don’t comment out most of the first two functions in this file and where I just leave the first “then clause” in each of the if-then-else statements I get a segmentation fault when I run the program.
If I only do this to the step2_ function where I only keep the “then clause” I get a segmentation fault in the step3_ function.
How can I keep the entire if-then-else clause of these two functions (step2_ and step3_)?
The first segmentation fault occurs on the fourth call to STEP2. The second segmentation fault occurs on the second call to STEP3.
I ask this because if I just keep the “then clause” body (throw away the if condition) and delete the “else clause” in each of these functions I get the same segmentation fault at step 5 which is called from the fourth file called C.f.
In the C code for step2_, the integer_adress varial print out as nil on the first three calls but some how is not nil on the fourth call.
I do not know if this code will compile. This pseudo code is to demonstrate the problem.
I tried changing floats to doubles in the C code but the segmentation fault persisted.
If I remove the first three function calls to STEP2 in file A.f the first segmentation fault does not occur. Nothing else in the code appears to be directly causing these faults. I.E.: Nothing else is changing these variables.
If I print out errno after calling malloc it prints out as 0.
The C file is used by the Fortran code via an archive file. The command outputs to create that archive file are:
cc -g -Wall -Werror -fmax-errors=1 -c -o B.o B.c
r - B.o
ar rvu ../lib.a
ranlib ../lib.a
The Fortran files are compiled with the following options:
-g -fno-second-underscore -Wall -Werror -fmax-errors=1 -fcheck=all
----- file A.f ----
SUBROUTINE STEP1
IMPLICIT NONE
INCLUDE 'STUFF'
INTEGER STEP2, STEP3
C = 0
if (STEP2(C,1).NE.0) print *,'not good'
E =0
if (STEP2(E,1).NE.0) print *,'not good'
F = 0
if (STEP2(F,1).NE.0) print *,'not good'
B = 0
if (STEP2(B,1).NE.0) print *,'not good'
C Program received signal SIGSEGV: Segmentation
C fault - invalid memory reference.
G = 0
if (STEP3(G,1).NE.0) print *,'not good'
D = 0
if (STEP3(D,1).NE.0) print *,'not good'
C Program received signal SIGSEGV: Segmentation
C fault - invalid memory reference.
RETURN
END
----- file B.c ----
struct my_data {
int value;
};
struct my_data2 {
int value;
};
int step2_(integer_address,the_one)
float **integer_address;
register struct my_data *the_one;
{
int return_value=0;
if(!*integer_address)
*integer_address=(float *)malloc(the_one->num*sizeof(float));
else
*integer_address=(float *)realloc(*integer_address,the_one->num*sizeof(float));
if(!*integer_address)return_value=1;
return(return_value);
}
int step3_(integer_address,the_one)
int **integer_address;
register struct my_data *the_one;
{
int return_value=0;
if(!*integer_address)
*integer_address=(int *)malloc(MAX(1, the_one->num)*sizeof(int));
else
*integer_address=(int *)realloc(*integer_address,MAX(1, the_one->num) *sizeof(int));
if(!*integer_address)return_value=1;
return(return_value);
}
int step5_(dest,src)
register struct my_data2 **dest,*src;
{
(*dest)->value=src->value;
/* Program received signal SIGSEGV: Segmentation fault - invalid memory reference.*/
return;
}
----- file C.f ----
SUBROUTINE STEP4(THEADDR)
INTEGER THEADDR,Z
C ... ... ...
C
C With THEADDR being set to some large value
C such as 1000000000 (this number is made up)
DO 10 Z=1,100
CALL STEP5(THEADDR+Z*4,0)
10 CONTINUE
RETURN
END
----- file STUFF ----
INTEGER A
INTEGER B,
& C,
& D,
& E,
& F,
& G
INTEGER H
INTEGER I
COMMON/INTEGER_ADDR/H,I,A,D,G
COMMON/REAL_ADDR/B,C,E,F