Eumel
Eumel

Reputation: 1433

initializing array pointer to a struct

I get the Error run-time check failure #3, and i have to initialize P and i know why but not how to do it.

Points is supposed to be a variable 2D array like float* points[3] for testing purposes its constant for now.

CVAPI(CvPOSITObject*)  lvCreatePOSITObject( float points[5][3], int point_count )
{
    CvPoint3D32f* P; //array of structs with int x,y,z 
    for(int i = 0; i< point_count; i++)
    {
        P[i].x = points[i][0];
        P[i].y = points[i][1];
        P[i].z = points[i][2];
    }
    return cvCreatePOSITObject(P,point_count);
}

Upvotes: 0

Views: 56

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

I don't know much about OpenCV, but I think you should allocate some memory to store the data.

#include <stdlib.h> // add this to the head of the file to use malloc

CVAPI(CvPOSITObject*)  lvCreatePOSITObject( float points[5][3], int point_count )
{
    CvPoint3D32f* P; //array of structs with int x,y,z
    P = malloc(sizeof(CvPoint3D32f) * point_count); // allocate some memory
    for(int i = 0; i< point_count; i++)
    {
        P[i].x = points[i][0];
        P[i].y = points[i][1];
        P[i].z = points[i][2];
    }
    return cvCreatePOSITObject(P,point_count);
}

This code may be bad because this may not free the allocated buffer.

Upvotes: 1

Related Questions