Trademark
Trademark

Reputation: 151

Sharing dynamically allocated arrays between C++ source files

I am trying to compile a code with two (more actually, but that is not important) source codes. In one of them I declared a variable size array like this:

const int number_of_equidistant = spike_location[number_of_spikes-1];
point_type* equidistant = NULL;
equidistant = new point_type[number_of_equidistant];

It works fine as long as I only use it in the same source file where I declare it. However, I would like to access it elsewhere as well (at this point just to print out the content to check it does what it is supposed to do) and that's where the dynamic size causes problems (I tried declaring the size as extern const int but that did not work). Is there a workaround or is this a particularly stupid approach and should it be done somehow differently?

The particular error report is this:

debug.cpp: In function ‘void debug_initialization()’:
debug.cpp:71:56: error: storage size of ‘equidistant’ isn’t constant
extern point_type equidistant[number_of_equidistant];

In the debug.cpp, the array and the size are declared like this:

extern const int number_of_equidistant;
extern point_type equidistant[number_of_equidistant];

I could of course change it to a list (or a heap or something like that), but I'd rather be able to easily access the data by their index. Any other approach is beyond my knowledge, so I welcome any suggestions :)

Upvotes: 0

Views: 281

Answers (2)

David Schwartz
David Schwartz

Reputation: 182847

You have to be consistent:

point_type* equidistant = NULL;

This says equidistant is a pointer.

extern point_type equidistant[number_of_equidistant];

This says equidistant is an array. Pick one and stick with it.

Upvotes: 1

Micah
Micah

Reputation: 452

When you allocate with new, you always use a pointer after that, you are using

extern point_type equidistant[number_of_equidistant];

where you should be using

extern point_type* equidistant;

a.cpp:

#include <iostream>

extern const int number_of_equidistant;
extern double* equidistant;
extern void debuginit();

int main()
{
    debuginit();
    for(size_t ii=0; ii<number_of_equidistant; ii++) {
        std::cerr << ii << std::endl;
    }
}

b.cpp:

extern const int number_of_equidistant = 100;
double equidistant[number_of_equidistant];

void debuginit()
{
    for(int ii=0; ii<number_of_equidistant; ii++) 
        equidistant[ii] = ii;
}

I would recommend you use a vector instead. It only makes sense to store the length and pointer together, especially for a global:

I have in a.cpp:

#include <vector>

const int number_of_equidistant = 100;
std::vector<double> equidistant(number_of_equidistant);

void debuginit()
{
    for(int ii=0; ii<equidistant.size(); ii++) 
        equidistant[ii] = ii;
}

and in b.cpp:

#include <vector>
#include <iostream>

extern std::vector<double> equidistant;
extern void debuginit();

int main()
{
    debuginit();
    for(size_t ii=0; ii<equidistant.size(); ii++) {
        std::cerr << ii << std::endl;
    }
}

It would be preferable to declare your variable in main() then pass it around as necessary because it is much less confusing to see a variable if you know where it came from (the argument list) rather than some global variable which you have no idea if anyone else has modified.

Upvotes: 0

Related Questions