使用指针从函数中获取值(c ++)

and*_*s-h 0 c++ pointers function

我有一个函数来计算几个不同的值:

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;
}
Run Code Online (Sandbox Code Playgroud)

该函数调用如下:

int* Latsize = NULL;
int* Lonsize = NULL;
vector<double>* Latgrid = NULL;
vector<double>* Longrid = NULL;
int res = asp->get_dimensions(Latsize,Lonsize,Latgrid,Longrid);
Run Code Online (Sandbox Code Playgroud)

然后,我尝试访问如下值:

cout << (*Szagrid)[4];
Run Code Online (Sandbox Code Playgroud)

或者像这样

cout << Szagrid->at(4);
Run Code Online (Sandbox Code Playgroud)

该程序编译时没有任何警告.但是,当我尝试访问get_dimensions()应该"填充"的指针时,valgrind会向我显示以下内容:

==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.
Run Code Online (Sandbox Code Playgroud)

我不太明白我做错了什么; 可能是显而易见的东西,但我似乎无法找到问题.

任何帮助是极大的赞赏!

Mic*_*ael 5

你在get_dimensions中写入内存,但你实际上从未分配过那个内存.相反,您只是传递NULL指针.

一个可能的解决方法是将您的调用代码更改为:

int Latsize, Lonsize;
vector<double> Latgrid;
vector<double> Longrid;
int res = asp->get_dimensions(&Latsize,&Lonsize,&Latgrid,&Longrid);
Run Code Online (Sandbox Code Playgroud)

这会在堆栈上自动为变量分配空间,然后将分配的空间的地址传递给get_dimensions,这将修改值.


Tyl*_*nry 5

你的问题是由别人回答得很好,但我想指出的是,因为这是C++,不C,这一切混乱的可能,如果你只是使用引用代替指针避免.这通常是你应该在这种情况下做的事情(如果不需要NULL返回值)

int ASPfile::get_dimensions(int& Lat, int& Lon, 
        vector<double>& Latgrid, vector<double>& Longrid) {

    Lat = get_latsize();
    Lon = get_lonsize();

    Latgrid = read_latgrid();
    Longrid = read_longrid();

    return 0;
}

// Call like this

int Latsize;
int Lonsize;
vector<double> Latgrid;
vector<double> Longrid;
int res = asp->get_dimensions(Latsize,Lonsize,Latgrid,Longrid);
Run Code Online (Sandbox Code Playgroud)