PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

CUDA-x86.

Fortran and C inter-function call with interger as parameter

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
Warren Cheung



Joined: 21 Jun 2005
Posts: 2

PostPosted: Tue Jun 21, 2005 11:01 am    Post subject: Fortran and C inter-function call with interger as parameter Reply with quote

Please look at these two small functions. I passed an long interger into the C function and changed its value and passed back into fortran. but the value is not correct. If I didn't use the long interger, the value is correct. Why?
Fortran code:

program foo
external boo

integer*8 a
a = -10
call boo(a)
print*, a
end program foo

C code:

#include <stdio.h>

void boo_(long int* a)
{
printf("PASSIN VALUE: %d\n", *a);
*a= 100;
printf("Within boo subroutine a = %d\n", *a);
return;
}

You can try to run these two code and if you chang the long int and integer*8 as int and integer respectively, you will see the a value after boo function all is changed to 100 but this long int simply doesn't work.

Thank you.
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6134
Location: The Portland Group Inc.

PostPosted: Tue Jun 21, 2005 12:47 pm    Post subject: Reply with quote

Hi Warren,


Are you compiling on a 32-bit system? In 32-bits, a 'long int' has a size of 4 bytes while in 64-bits it has a size of 8 bytes. A 'long long int' is 8 bytes in both 32 and 64-bits so might work better here.
Code:

% cat tmp_c.c
#include <stdio.h>

void boo_(long int* a)
{
  int size;

  size = sizeof(long int);

  printf("PASSIN VALUE: %d,  Size of a long int: %d\n", *a, size);
  *a= 100;
  printf("Within boo subroutine a = %d\n", *a);
  return;
}
% cat tmp_f.f
      program foo
      external boo

      integer*8 a
      a = -10
      call boo(a)
      print*, a
      end program foo

%pgf90 tmp_f.f tmp_c.c -tp k8-64
tmp_f.f:
tmp_c.c:
% a.out
PASSIN VALUE: -10,  Size of a long int: 8
Within boo subroutine a = 100
                      100
% pgf90 tmp_f.f tmp_c.c -tp k8-32
tmp_f.f:
tmp_c.c:
% a.out
PASSIN VALUE: -10,  Size of a long int: 4
Within boo subroutine a = 100
              -4294967196


- Mat
Back to top
View user's profile
Warren Cheung



Joined: 21 Jun 2005
Posts: 2

PostPosted: Tue Jun 21, 2005 3:40 pm    Post subject: Reply with quote

thank you, the problem solved. Nice job.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group