Inconsistent Memory Reference

93 Views Asked by At

I am doing some mixed programming between C++ and FORTRAN. A problem comes up about passing a character array from FORTRAN to C++ as shown in the code.

CDll.h:

// CDll.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif

#define DLLEXPORT __declspec(dllexport)

DLLEXPORT void testchararray(char arr[][10]);

#ifdef __cplusplus
}
#endif

CDll.cpp:

// CDll.cpp
#ifndef __cplusplus
#define __cplusplus
#endif

#include "CDll.h"
#include <iostream>

using namespace std;

void testchararray(char arr[][10])
{
    cout << arr << endl;
}

FMain.f90:

!DIR$ OBJCOMMENT LIB: "CDll.lib"
MODULE FModule
    IMPLICIT NONE

    INTERFACE

        SUBROUTINE testchararray(arr)
        !DIR$ ATTRIBUTES C, ALIAS: "testchararray" :: testchararray
            CHARACTER(10), DIMENSION(:), INTENT(IN) :: arr
            !DIR$ ATTRIBUTES REFERENCE :: arr ! <-- If the way is right, is the directive nessary to add ?
        END SUBROUTINE testchararray

    END INTERFACE

END MODULE FModule

PROGRAM Main
    USE FModule
    IMPLICIT NONE

    character(10), dimension(2) :: arr = ["1234567890", "0987654321"]

    write(*,"(Z)") loc(arr)
    call testchararray(arr)

END PROGRAM Main

CDll.h and CDll.cpp are generated into DLL and linked by FORTRAN main program.

The memory addresses before and after calling the subroutine are not consistent to each other thus the character array is not properly passed. Am I doing it the wrong way or is there anything I have not yet noticed ? Thanks for any help.

PS: The project was debugged on x64 platform.

0

There are 0 best solutions below