- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello!
I need to call some C Routines from a Fortran program (see my earlier post about petsc on 64 bit). Therefore some pointer conversions are necessary, which work well as long as I'm compiling my program on a 32 bit machine. But when I compile the same code for a 64 bit machine, the pointer conversion fails with a segmentation fault. I've written an example program to illustrate the problem. At first an integer of size 8 Byte is declared in fortran . this integer should hold the address of a pointer on a C struct. In the first C routine ALLOCPOINTER memory is allocated for the pointer. in the call to the second C routine SETPOINTER some values should be written into the C struct. the output of the program running on a 32 bit machine and running on a 64 bit machine is given below. As you can see from the output, the conversion fails under 64 bit (intel itanium). It would be great if any of you has an idea, how I can work around this problem. I'm using the Intel fortran and C compilers (latest version).
Many thanks in advance
Felix
LISTING OF FORTRAN MAIN PROGRAM
==============================================================
program testpointerconversion
integer*8 :: cpointer
integer*8 :: n
! allocate memory for the C struct
call AllocatePointer(cpointer)
print*,"the value of cpointer in Fortran is:",cpointer
n=5
! assign a value to the C struct
call SetPointer(cpointer,n)
end program testpointerconversion
LISTING OF C ROUTINES
=============================================================
#include "stdio.h"
#include "stdlib.h"
struct test{
int a;
double e;
};
void ALLOCATEPOINTER(long* cpointer){
int a;
double e;
struct test* converted;
converted = malloc(sizeof(struct test));
*cpointer = (long)(converted);
printf("the value of the pointer is now:%i ",*cpointer);
printf("the value of converted is now:%p ",converted);
}
void SETPOINTER(long* cpointer,int* n){
int a;
double e;
struct test* converted;
converted=(struct test*) *(long*)(cpointer);
printf("the value of the pointer is now:%i ",*cpointer);
printf("the value of converted is now:%p ",converted);
converted->a=*n;
}
OUTPUT ON IA32
================================================================
the value of cpointer in Fortran is: 3092224
the value of the pointer is now:3092224
the value of converted is now:002F2F00
the value of the pointer is now:3092224
the value of converted is now:002F2F00
OUTPUT ON IA64
================================================================
the value of cpointer in Fortran is: 4283971232
the value of the pointer is now:-10996064
the value of converted is now:000006FBFF5836A0
the value of the pointer is now:-10996064
the value of converted is now:FFFFFFFFFF5836A0
....in the next step the program fails with segv.
I need to call some C Routines from a Fortran program (see my earlier post about petsc on 64 bit). Therefore some pointer conversions are necessary, which work well as long as I'm compiling my program on a 32 bit machine. But when I compile the same code for a 64 bit machine, the pointer conversion fails with a segmentation fault. I've written an example program to illustrate the problem. At first an integer of size 8 Byte is declared in fortran . this integer should hold the address of a pointer on a C struct. In the first C routine ALLOCPOINTER memory is allocated for the pointer. in the call to the second C routine SETPOINTER some values should be written into the C struct. the output of the program running on a 32 bit machine and running on a 64 bit machine is given below. As you can see from the output, the conversion fails under 64 bit (intel itanium). It would be great if any of you has an idea, how I can work around this problem. I'm using the Intel fortran and C compilers (latest version).
Many thanks in advance
Felix
LISTING OF FORTRAN MAIN PROGRAM
==============================================================
program testpointerconversion
integer*8 :: cpointer
integer*8 :: n
! allocate memory for the C struct
call AllocatePointer(cpointer)
print*,"the value of cpointer in Fortran is:",cpointer
n=5
! assign a value to the C struct
call SetPointer(cpointer,n)
end program testpointerconversion
LISTING OF C ROUTINES
=============================================================
#include "stdio.h"
#include "stdlib.h"
struct test{
int a;
double e;
};
void ALLOCATEPOINTER(long* cpointer){
int a;
double e;
struct test* converted;
converted = malloc(sizeof(struct test));
*cpointer = (long)(converted);
printf("the value of the pointer is now:%i ",*cpointer);
printf("the value of converted is now:%p ",converted);
}
void SETPOINTER(long* cpointer,int* n){
int a;
double e;
struct test* converted;
converted=(struct test*) *(long*)(cpointer);
printf("the value of the pointer is now:%i ",*cpointer);
printf("the value of converted is now:%p ",converted);
converted->a=*n;
}
OUTPUT ON IA32
================================================================
the value of cpointer in Fortran is: 3092224
the value of the pointer is now:3092224
the value of converted is now:002F2F00
the value of the pointer is now:3092224
the value of converted is now:002F2F00
OUTPUT ON IA64
================================================================
the value of cpointer in Fortran is: 4283971232
the value of the pointer is now:-10996064
the value of converted is now:000006FBFF5836A0
the value of the pointer is now:-10996064
the value of converted is now:FFFFFFFFFF5836A0
....in the next step the program fails with segv.
Link Copied
0 Replies

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page