Unable to access to Global/External

In a file I declare as global an array of structures that is being dynamically allocated:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct series {
        int num;
        int dayNum;
        string date;
};

series *alldata;

int prepSeries()
{
int nr;
// calculate nr, then:
...
alldata = new series[nr];
...
// store data into alldata[].xxx
}

All good for now. Within this function I can assign and read the data in alladata.

Now, I need to read this same alldata in the main() function which resides in another file. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
extern struct series
{
        int num;
        int dayNum;
        string date;
} alldata[];

int main()
{
int dummy;
...
dummy = alldata[i].num;
}


I'm really not able to see the alldata array in the main() function.

The syntax here above is fine (g++ -Wall is happy) but when launching the executable it goes in segmentation fault, telling me that I'm trying to access something unavailable.

I would greatly appreciate some help. This is a very tricky one.

Thanks in advance.
Last edited on
I am assuming that you call at prepSeries before trying to access alldata.
Also, that you have a function that deallocates the memory. (and that you are calling it properly)

Wouldn't be better if you encapsulate that behaviour in a class?
You could avoid the global variable too.
ne555: Thanks for the hint.

I'm taking a total different direction and using a map instead of an array.

regards.
Topic archived. No new replies allowed.