Mix-Language Program Problem: Array Access

I'm writing a C++ program that calls fortran subroutines. The C++ program also uses structs to access fortran common blocks. Here is the C++ code. Unfortunately, the Fortran code is to much to far to post but I think the problem originates in the C++ code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;

//external Fortran function prototype
extern"C" {
	void getrec_(int *irw,const char *stname,int *sampin,int *mofm,int *nparm,\
					int *pc,double *val,const char *flag,const char *dflg1,\
					const char *dflg2,int *osmin,int *oemin,int *amin,int *iflag);
	void rsif_(int *iflag);
	void getfn_(int *iflag);

	// fortran common block declaration
	extern struct
	{
		bool prompt;
	} pr_;
	extern struct
	{
		char fnsif[80],fndat[80],fnerr[80],fnonly[80];
		char fnhis[80],fncrp[80],fnnp[80],action[72];
	} c6_;
	extern struct
	{
		float lat, lon, elev;
		int cr210t, pcatot, valwin, sifem, sifsm, trlqc, trlsa, trlrrd;
		short int statn, staten, outtab[6][5], parttab[4][50];
	} c2_;
}

int main(int argc, char *argv[])
{
	int arg;
	char *inFilename;
	fstream rawFile;
	string rawData;
	char_separator<char> sep(", -");
	int iflag;
	int irw = 1;
	
	// handle command-line arguments
	if (argc == 3)
	{
		for (arg = 1; arg < argc; ++arg)
		{
			char *argCp = argv[arg];
			
			if (argCp[0] != '-')
				cerr << "Illegal switch ignored: " << argCp << '\n';
			else
				switch (argCp[1])
				{
					case 'f':
						inFilename = argv[++arg];
						break;
					default:
						cerr << "Illegal switch ignored: " << argCp << '\n';
						break;
				}
		}
	}
	else
	{
		cerr << "Specify Command-line Arguments (-h for help)" << '\n';
		return(EXIT_FAILURE);
	}

	// Prompt user for station ID
	pr_.prompt = true;
	getfn_(&iflag);
	if (iflag != 0)
	{
		cerr << "Error During getfn" << '\n';
		return(EXIT_FAILURE);
	}
	
	rsif_(&iflag);
	if (iflag != 0)
	{
		cerr << "Error During rsif" << '\n';
		return(EXIT_FAILURE);
	}
	
	// read data from inFile
	cout << "Reading data from File: " << inFilename << '\n';
	rawFile.open(inFilename);
	
	// tokenize data 
	while (rawFile.good())
	{
		getline(rawFile, rawData);
		tokenizer< char_separator<char> > data(rawData, sep);	
	}
	
	return(EXIT_SUCCESS);
}





The problem I'm encountering is that one of the common blocks contains an array named parttab. This array is populated by the fortran subroutine rsif_. when I try to access the array elements in parttab such as parttab[0][0] it is not the correct value but if I access element parttab[0][-12] i get the expected first element. The parttab variable in c2 is declared in fortran as parttab[4][50] so I believe the array is declared correctly in C++. Whats the deal here? I appreciate any help. Thanks.
What's your compiler? That struct might be missing __attribute_common__ or __attribute__((__common__)). or what-have-you
You could post the definition of your c2 common area, or even add a dummy rsif to make it a compilable test that others could run.
You remember that fortran arrays are column-major while C arrays are row-major, right?
Last edited on
I'm not sure what you mean by _attribute_common_?
Yes, the arrays have been switched such that:
Fortran: parttab[50][4]
C++:parttab[4][50]
Compiler is gfortran and g++
Last edited on
The problem was that my variable order did not match the order within the common block. Now that they match the byte alignment is correct.
Topic archived. No new replies allowed.