简体   繁体   English

函数返回指针指针

[英]Function returning pointer to pointer

Please look at my code below: 请看下面的代码:

class SVMClassifier : public LibHAR
{
 public:
  ...
   //This is my function returning a pointer to pointer to svm_node structure
  svm_node** SVMFeatureExtraction(SkeData* inputData, int* pFrameNum, int* pFeatureNum, double wt);

  //This function calls SVMFeatureExtraction
  virtual bool FeatureExtraction(SkeData* inputData, const double* dataLabels = NULL, int labelNum = 0); //This function calls SVMFeatureExtraction
  ...
 private:
 svm_node** SVMNodes;
 int dataNum;
 ...
}

svm_node** SVMClassifier::SVMFeatureExtraction(SkeData* inputData, int* pFrameNum, int* pFeatureNum, double wt)
{
    *pFeatureNum = FEATURENUM;
    *pFrameNum = inputData->GetFrameSaved();
    svm_node** pNodes = new svm_node*[*pFrameNum];

    for (int i = 0; i < *pFrameNum; i++)
    {
        pNodes[i] = new svm_node[FEATURENUM + 1];
        for (int j = 0; j < FEATURENUM / 3; j++)
        {
            FEATURE_VEC* pVec = new FEATURE_VEC;
            if (!CalFeatureVector(inputData, i+1, j+1, pVec, wt))
                return NULL;

            pNodes[i][j*3].index = j*3 + 1;
            pNodes[i][j*3].value = pVec->x;

            pNodes[i][j*3 + 1].index = j*3 + 2;
            pNodes[i][j*3 + 1].value = pVec->y;

            pNodes[i][j*3 + 2].index = j*3 + 3;
            pNodes[i][j*3 + 2].value = pVec->z;

            delete pVec;
        }
        pNodes[i][FEATURENUM].index = -1;
        pNodes[i][FEATURENUM].value = 0;

    }
    return pNodes;
}

bool SVMClassifier::FeatureExtraction(SkeData* inputData, const double* dataLabels, int labelNum)
{

    CleanNodes();
    int n;
    SVMNodes = SVMFeatureExtraction(inputData, &dataNum, &n, actWeight);  //Error here!
        ...
}

The class method FeatureExtraction calls another method SVMFeatureExtraction which returns a pointer to pointer. 类方法FeatureExtraction调用另一个方法SVMFeatureExtraction ,它返回指向指针的指针。 I think the memory pointed by the pointer is allocated dynamically in the heap, since it is created by " new " . 我认为指针指向的内存是在堆中动态分配的,因为它是由“ new ”创建的。 But when I debugged the program, the address returned by SVMFeatureExtraction can not be successfully assigned to SVMNodes (SVMNodes is always "NULL"), although the content of pNodes is correct. 但是当我调试程序时,虽然pNodes的内容是正确的,但SVMFeatureExtraction返回的地址无法成功分配给SVMNodes (SVMNode始终为“NULL”)。 Can anyone tell me what is wrong with the code? 谁能告诉我代码有什么问题?

Thank you. 谢谢。

It might be a silly suggestion, but are you absolutely certain that this part never happens? 这可能是一个愚蠢的建议,但你绝对肯定这一部分永远不会发生吗?

if (!CalFeatureVector(inputData, i+1, j+1, pVec, wt))
    return NULL;
*pFeatureNum = FEATURENUM;
    *pFrameNum = inputData->GetFrameSaved();
    svm_node** pNodes = new svm_node*[*pFrameNum];

this is strange, you try to change the value of the pointer instead of the pointed value. 这很奇怪,您尝试更改指针的值而不是指向的值。 And why do you pass two arguments by pointer and reinitialized its in your function? 为什么你通过指针传递两个参数并在函数中重新初始化它?

try this: 尝试这个:

pFeatureNum = FEATURENUM;
pFrameNum = inputData->GetFrameSaved();
svm_node** pNodes = new svm_node[pFrameNum];
for (int i = 0; i < pFrameNum; i++)
    {
        pNodes[i] = new svm_node[FEATURENUM + 1];
        for (int j = 0; j < FEATURENUM / 3; j++)
        {
            FEATURE_VEC* pVec = new FEATURE_VEC;
            if (!CalFeatureVector(inputData, i+1, j+1, pVec, wt))
                return NULL;

            pNodes[i][j*3].index = j*3 + 1;
            pNodes[i][j*3].value = pVec->x;

            pNodes[i][j*3 + 1].index = j*3 + 2;
            pNodes[i][j*3 + 1].value = pVec->y;

            pNodes[i][j*3 + 2].index = j*3 + 3;
            pNodes[i][j*3 + 2].value = pVec->z;

            delete pVec;
        }
        pNodes[i][FEATURENUM].index = -1;
        pNodes[i][FEATURENUM].value = 0;

    }

Try this (slight modification to @Nagasaki's answer): 试试这个(对@ Nagasaki的回答略有修改):

*pFeatureNum = FEATURENUM;
*pFrameNum = inputData->GetFrameSaved();
svm_node** pNodes = new svm_node[*pFrameNum];
for (int i = 0; i < *pFrameNum; i++)
    {
        pNodes[i] = new svm_node[FEATURENUM + 1];
...

(and, if it works, accept @Nagasaki's answer and I'll edit it so they get credit) (并且,如果它有效,接受@长崎的答案,我将对其进行编辑,以便获得信用)

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

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