Quantcast
Channel: Intel® Software - Intel® Fortran Compiler for Linux* and macOS*
Viewing all articles
Browse latest Browse all 2746

corrupt value when passing integer from fortran to void * in C function

$
0
0

SYSTEM INFO:

ProductName:    Mac OS X
ProductVersion:    10.8.5
BuildVersion:    12F45

Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0 Build 20131010
Copyright (C) 1985-2013 Intel Corporation.  All rights reserved.

When I compile the Fortran program:

module test
 INTERFACE
     FUNCTION h5dwrite_f_c2(buf)
       USE, INTRINSIC :: ISO_C_BINDING
       INTEGER(C_INT) :: h5dwrite_f_c2
       TYPE(C_PTR), VALUE :: buf
     END FUNCTION h5dwrite_f_c2
  END INTERFACE

 INTERFACE
     FUNCTION h5dwrite_f_c3(buf) BIND(C,NAME="h5dwrite_f_c3")
       USE, INTRINSIC :: ISO_C_BINDING
       INTEGER(C_INT) :: h5dwrite_f_c3
       TYPE(C_PTR), VALUE :: buf
     END FUNCTION h5dwrite_f_c3
  END INTERFACE
end module test

PROGRAM fortranlibtest
  USE test
  USE, INTRINSIC :: ISO_C_BINDING
  IMPLICIT NONE
    INTEGER(C_INT) :: hdferr
    TYPE(C_PTR) :: f_ptr
    integer(C_INT), target :: buf2

    buf2 = 200
    f_ptr = C_LOC(buf2)

    hdferr = h5dwrite_f_c3(f_ptr)
    hdferr = h5dwrite_f_c2(f_ptr)

END PROGRAM fortranlibtest

AND THE C PROGRAM:

#include <stdio.h>

#define H5_FC_FUNC_(name,NAME) name ## _

#define nh5dwrite_f_c2             H5_FC_FUNC_(h5dwrite_f_c2, H5DWRITE_F_C2)
int nh5dwrite_f_c2(void *buf);

int
nh5dwrite_f_c2 (void *buf)
{
  int *name;
  name = (int*)buf;
  printf(" Buffer In nh5dwrite_f_c2 = %d \n", *name);
  return 0;
}

int
h5dwrite_f_c3 (void *buf)
{
  int *name;

  name = (int*)buf;
  printf(" Buffer In h5dwrite_f_c3 = %d \n", *name);
  return 0;
}

AND THE MAKEFILE:

# INTEL COMPILER
CC  = icc
F90 = ifort

F90FLAGS = -g
CFLAGS = -g

OBJ = ccode.o

OBJF90 = fcode.o

all: ex12

ex12: $(OBJ) $(OBJF90)
        $(F90) $(CFLAGS) $(LDFLAGS) $(OBJ) $(OBJF90) -o $@ $(LIB)

.SUFFIXES: .o .f90

.f90.o:
        $(F90) $(F90FLAGS) -c $< -o $@ $(LIB)

.SUFFIXES: .o .c

.c.o:
        $(CC)  $(CFLAGS) $(LDFLAGS)  -c  $< -o $@ $(LIB)

clean:
        rm -f *.o *.mod ex12

spotless:
        rm -f *.o *.mod ex12 *~

I GET AS OUPUT:

 Buffer In h5dwrite_f_c3 = 200
 Buffer In nh5dwrite_f_c2 = 1400015680

I.E. the second value pointed to by the pointer in C is wrong. It works with gnu compiler and in linux with

Intel(R) Fortran Compiler XE for applications running on IA-32, Version 13.1.3.192 Build 20130607
Copyright (C) 1985-2013 Intel Corporation.  All rights reserved.

Thanks,

Scot

 

I've also attached the files.

 

 

 

 

 

 

 

 

AttachmentSize
Downloadex12.tar4.5 KB

Viewing all articles
Browse latest Browse all 2746

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>