i have a function to calculate several different values:
int ASPfile::get_dimensions(int* Lat, int* Lon,
vector<double>* Latgrid, vector<double>* Longrid) {
_latsize = get_latsize();
_lonsize = get_lonsize();
Lat = &_latsize;
Lon = &_lonsize;
latgrid = read_latgrid();
longrid = read_longrid();
*Latgrid = latgrid;
*Longrid = longrid;
return 0;
}
This function is called as follows:
int* Latsize = NULL;
int* Lonsize = NULL;
vector<double>* Latgrid = NULL;
vector<double>* Longrid = NULL;
int res = asp->get_dimensions(Latsize,Lonsize,Latgrid,Longrid);
and then, I try to access the values as follows:
cout << (*Szagrid)[4];
or like this
cout << Szagrid->at(4);
The program compiles with no warnings. However, when I try to access the pointers which are supposed to be 'filled' by get_dimensions(), valgrind shows me the following:
==10531== Invalid read of size 8
==10531== at 0x633D5DA: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (in /home/myhome/src/libiup/Release/libiup.so)
==10531== by 0x633CADC: ASPfile::get_dimensions(int*, int*, int*, std::vector<double, std::allocator<double> >*, std::vector<double, std::allocator<double> >*, std::vector<double, std::allocator<double> >*) (in /home/myhome/src/libiup/Release/libiup.so)
==10531== by 0x41B90B: ASPfileTest::test_get_dimensions() (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41BACC: ASPfileTest::operator()() (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41E229: boost::function0<void>::operator()() const (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41E7C2: cute::runner<cute::eclipse_listener>::runit(cute::test const&) (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41D26A: runSuite() (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41DBC4: main (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==10531==
==10531== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==10531== Access not within mapped region at address 0x0
==10531== at 0x633D5DA: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (in /home/myhome/src/libiup/Release/libiup.so)
==10531== by 0x633CADC: ASPfile::get_dimensions(int*, int*, int*, std::vector<double, std::allocator<double> >*, std::vector<double, std::allocator<double> >*, std::vector<double, std::allocator<double> >*) (in /home/myhome/src/libiup/Release/libiup.so)
==10531== by 0x41B90B: ASPfileTest::test_get_dimensions() (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41BACC: ASPfileTest::operator()() (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41E229: boost::function0<void>::operator()() const (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41E7C2: cute::runner<cute::eclipse_listener>::runit(cute::test const&) (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41D26A: runSuite() (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== by 0x41DBC4: main (in /home/myhome/src/libiup_test/Release/libiup_test)
==10531== If you believe this happened as a result of a stack overflow in your
==10531== program's main thread (unlikely but possible), you can try to increase
==10531== the size of the main thread stack using the --main-stacksize= flag.
==10531== The main thread stack size used in this run was 8388608.
I don't quite understand what I'm doing wrong; probably something obvious, but I cannot seem to find the problem.
Any help is greatly appreciated!