0

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. I think the memory pointed by the pointer is allocated dynamically in the heap, since it is created by "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. Can anyone tell me what is wrong with the code?

Thank you.

7
  • Before returning from SVMFeatureExtraction try to print casted to int value of pNodes, also print SVMFeatureExtraction(inputData, &dataNum, &n, actWeight) without assigning. Commented Apr 17, 2013 at 15:23
  • A couple questions: Are you sure inputData->GetFrameSaved() returns a value > 0? Also, are you sure this pNodes is allocated memory inside SVMFeatureExtraction() Commented Apr 17, 2013 at 15:23
  • Your line with the error is referencing a variable that does not exist (dataNum). Is this intended? Commented Apr 17, 2013 at 15:29
  • @Tom I am 100% sure. When I debugged to the next line after "SVMNodes = SVMFeatureExtraction(inputData, &dataNum, &n, actWeight); ", both dataNum and n got the correct value. Both of them are output of the function SVMFeatureExtraction. Commented Apr 17, 2013 at 15:31
  • Roger that... Let me look at it again. I think @Nagasaki is close on this Commented Apr 17, 2013 at 15:34

3 Answers 3

4

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;
Sign up to request clarification or add additional context in comments.

2 Comments

I guess you're right! ...How could I overlook it!..Anyway, thanks for your answer!
Happens to the best of us ;-) Glad it helped!
-1
*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;

    }

5 Comments

no, in order to modify a pointed variable, you must dereference it. And the allocation creates an array of pointers, so the original code is correct.
@Nagasaki Sorry I don't understand your answer.. pFeatureNum and pFrameNum are pointers to int, why do you assign int values to them? Moreover, is this statement correct? svm_node** pNodes = new svm_node[pFrameNum]; It seems you assign a pointer to svm_node to a pointer to pointer to svm_node..
for example if you do this *i = 1, it's like says the address of i is 1. To change the value pointed by i, you just do i = 1;
if you want to create a pointer you do this svm_node* pNodes = new svm_node; exact? and a pointer of pointer is svm_node** pNodes = new svm_node[pFrameNum]; which is in fact an array of pointers.
@Nagasaki - the way to read *i = 1 is "assign 1 to the contents of i"
-1

Try this (slight modification to @Nagasaki's answer):

*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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.