我如何迭代Malloc?(OpenCV)

How do I iterate across malloc? (opencv)

本文关键字:Malloc OpenCV 何迭代 迭代      更新时间:2023-10-16

我正在使用此网站的代码

但是,该代码的一部分被删除了。

这是代码的相关部分

CvConvexityDefect* defectArray;
for (; defects; defects = defects->h_next)
            {
                int nomdef = defects->total; // defect amount
                                             //outlet_float( m_nomdef, nomdef );
                                             //printf(" defect no %d n",nomdef);
                if (nomdef == 0)
                    continue;
// Alloc memory for defect set.
                //fprintf(stderr,"mallocn");
                defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef);
                // Get defect set.
                //fprintf(stderr,"cvCvtSeqToArrayn");
                cvCvtSeqToArray(defects, defectArray, CV_WHOLE_SEQ);
                // Draw marks for all defects.
               //THIS IS WHERE THE MISTAKE IS
                    for (int i = 0; //THE REST OF THIS LINE IS MISSING
                { printf(" defect depth for defect %d %f n",i,defectArray[i].depth);
                    cvLine(img_8uc3, *(defectArray[i].start), *(defectArray[i].depth_point),CV_RGB(255,255,0),1, CV_AA, 0);
                    cvCircle(img_8uc3, *(defectArray[i].depth_point), 5, CV_RGB(0,0,164), 2, 8,0);
                    cvCircle(img_8uc3, *(defectArray[i].start), 5, CV_RGB(0,0,164), 2, 8,0);
                    cvLine(img_8uc3, *(defectArray[i].depth_point), *(defectArray[i].end),CV_RGB(255,255,0),1, CV_AA, 0);
                }

如您所见,缺少for循环的一部分。我无法弄清楚如何完成for循环,以便通过 defectArray

迭代。

我该怎么做?

for (int i = 0; i < nomdef; i++)