简体   繁体   English

指针无法获取数据

[英]Pointer can't get data

I want get the row number of data, but the pointer CvMat* data, CvMat* responses get nothing in main() . 我想得到数据的行数,但指针CvMat* data, CvMat* responsesmain()什么也得不到。 The minimal, complete and varifiable example is showed as follow: 最小,完整和可变的示例如下所示:

#include "opencv2/core/core_c.h"
#include "opencv2/ml/ml.hpp"

#include <cstdio>
#include <fstream>  
#include <iomanip>

#define ATTRIBUTES_PER_SAMPLE 9
#define NUM_OF_ALL_SAMPLES 950

using namespace std;

int read_data( CvMat* data, CvMat* responses )
{
    float temp=1.0;

    data = cvCreateMat( NUM_OF_ALL_SAMPLES, ATTRIBUTES_PER_SAMPLE, CV_32F );
    responses = cvCreateMat( NUM_OF_ALL_SAMPLES, 1, CV_32F );

    for(int line = 0; line < NUM_OF_ALL_SAMPLES; line++)
        for(int attribute = 0; attribute <= ATTRIBUTES_PER_SAMPLE ; attribute++){

            if(attribute < ATTRIBUTES_PER_SAMPLE){
                CV_MAT_ELEM(*data, float, line, attribute) = temp;
            }
            else if(attribute == ATTRIBUTES_PER_SAMPLE){
                CV_MAT_ELEM(*responses, float, line, 0) = temp;
            }

        }


    return 1;
}

///////////////////////////////////////////////////////////////////////////
int main()
{
    CvMat* data = 0;
    CvMat* responses = 0;

    int ok = read_data(data, responses);

    int nsamples_all = data->rows; // <--------- error happens here 

    cvReleaseMat(&data);
    cvReleaseMat(&responses);

    return 0;
    return 0;
}

The error is 错误是

Unhandled exception at 0x013715c2 in opencv_pointer.exe: 0xC0000005: Access violation reading location 0x00000014. opencv_pointer.exe中0x013715c2处的未处理异常:0xC0000005:访问冲突读取位置0x00000014。

My compiler is VS2008. 我的编译器是VS2008。 Why do the the pointer CvMat* data, CvMat* responses get nothing? 为什么指针CvMat* data, CvMat* responses什么都没得到?

Because the arguments are passed by value. 因为参数是按值传递的。 You can use reference to have callee modify caller's local variables like this (add & ): 您可以使用引用让被调用者修改调用者的本地变量,如下所示(add & ):

int read_data( CvMat*& data, CvMat*& responses )
{
    // the same code
}

The parameter data and responses are declared to be passed by value, which means any modification on themselves (not the pointee) inside read_data() would not affect the variables in main() , their value are still the initialized value 0 . 声明参数dataresponses按值传递,这意味着对read_data()内部的自身(不是指针对象)的任何修改都不会影响main()的变量,它们的值仍然是初始化值0

You could change the parameter type to be passed by reference (or passed by pointer, relevant code needs adjustment for it). 您可以更改参数类型以通过引用传递(或通过指针传递,相关代码需要调整它)。

int read_data( CvMat*& data, CvMat*& responses )
                     ~             ~
{
    ...
}

The parameters are passed by copy so the pointer CvMat* data in the function is copy of the pointer you pass in from main. 参数通过复制传递,因此CvMat* data的指针CvMat* data是从main传入的指针的副本 Any changes you make to it are local to the function. 您对其所做的任何更改都是函数的本地更改。 When you assign it a value using cvCreateMat() that value is not reflected back to the CvMat* data in main() so it gets lost when the function returns. 当您使用cvCreateMat()为其赋值时,该值不会反映回main()CvMat* data ,因此当函数返回时它会丢失。

If you want changes to the parameter to be "passed back" you need to pass in the pointers by reference : 如果要将参数的更改“传回”,则需要通过引用传递指针:

// pass variables by reference using &
int read_data(CvMat*& data, CvMat*& responses)

Now changes made to the parameters inside the function happen to the variables you passed in from outside the function - the pointers are not copied, they are referenced. 现在,对函数内部参数所做的更改发生在从函数外部传入的变量中 - 指针不会被复制,它们会被引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM