c++ - 指针和结构导致段错误

标签 c++ pointers struct

我一直在使用 openCV 使用 ColorSegmentationAlgorithm。我完成了分割,但现在我正在按颜色收集图片上的对象。为了做到这一点,我在 RLE 中压缩了图像的“行”,并且它们通过指针相互引用。

此时(我认为)一切正常,因为我在控制台上打印了用 RLE 压缩的“集群”结果,一切似乎都正常。然后我查看结构列表以创建一个新变量来表示对象(使用弹跳框、质心等)。但是,这就是问题所在,我现在不明白为什么在“随机情况”中调用指针的内容时加载错误(就像变量被移动了):例如,如果结构具有以下内容:

(int i, int je, int js, "pointer"*child) --> {1, 1 , 1, 0x0AB0231A}

已加载 --> {1, 0x0AB0231A, 1, 1}

如果调用下一个“子”,则会引发段错误(核心转储)错误

所以...这是代码的一部分:

struct LineObjRLE {
    unsigned int i;
    unsigned int js;
    unsigned int je;
    unsigned int size;
    unsigned int color;
    struct LineObjRLE *parent;
    struct LineObjRLE *child;
};

struct LineObjRLE auxRLE = aRLE[i][j];
vector<struct LineObjRLE> obj;
while (1) {
    obj.push_back(auxRLE);
    printf("auxRLE: parent: %d  --  child: %d\n",
            auxRLE.parent, auxRLE.child);
    if (auxRLE.child == NULL)
        break;
    auxRLE = *auxRLE.child;
}
  • aRLE(RLE 数组)是在分割时创建的 vector 变量。

这里我得到了一些结果:(指针显示为int,但它们是正确的)

---------------------------------
k: 1 
auxRLE: parent: 0  --  child: 55035088
auxRLE: parent: 55036080  --  child: 55505600
auxRLE: parent: 55035088  --  child: 0
---------------------------------
k: 2 
auxRLE: parent: 0  --  child: 55035248
auxRLE: parent: 0  --  child: 0
---------------------------------
k: 3 
auxRLE: parent: 0  --  child: 55035288
auxRLE: parent: 55036200  --  child: 55505720
auxRLE: parent: 55035288  --  child: 0
---------------------------------
k: 4 
auxRLE: parent: 0  --  child: 55035528
auxRLE: parent: 0  --  child: 55505840
auxRLE: parent: 55035528  --  child: 0
---------------------------------
k: 5 
auxRLE: parent: 0  --  child: 55035688
auxRLE: parent: 0  --  child: 0
---------------------------------
k: 6 
auxRLE: parent: 0  --  child: 55035808
auxRLE: parent: 18  --  child: 6
Segmentation fault (core dumped)

分割发生在我之前描述的“转变”发生时。当我检查结构的所有内容时,我发现结构的其他变量中的指针和指针具有其他值(因此指向 6 或 18 会产生错误)。

PD:我没有保存“parents”指针,而是只使用“child”方向来查看结构。

我希望我解释了自己,有人可以帮助我。 提前致谢!

代码:

    while (waitKey(1)) {

        t1 = clock(); // Time counter1
        device >> frame; // Get the frame from the camera
        imshow("Ori", frame); // Show the original frame
        //medianBlur(frame, frame, 5);
        imageBGR2HSV(&frame); // Transform image from BGR to HSV

        int n = frame.channels(); // Count the number of image's channels to use the pointer

        int color;
        short int js, colorRLE = -1, jRLE = -1; // Variables for RLE encode
        for (int i = 0; i < frame.rows; i++) {
            uchar* ptr = frame.ptr<uchar>(i); // Pointer to i row
            vector<struct LineObjRLE> temp;
            for (int j = 0; j < frame.cols; j++) {
                // Proximate the color to a cluster
                color = CS.whichColorHSV(
                        (color3cInt ) { ptr[n * j], ptr[n * j + 1], ptr[n
                                        * j + 2] });
                // RLE encoding
                if (!j) {
                    colorRLE = color;
                    jRLE = 1;
                    js = 0;
                } else {
                    if (j == frame.cols - 1) {
                        temp.push_back((LineObjRLE ) { i, js, j, j - js, color,
                                NULL, NULL });
                    } else if (color == colorRLE) {
                        jRLE = jRLE + 1;
                    } else if (color != colorRLE) {
                        temp.push_back((LineObjRLE ) { i, js, j, j - js, color,
                                NULL, NULL });
                        colorRLE = color;
                        jRLE = 1;
                        js = j;
                    }
                }
                // Change the color (Improve assigning directly the BGR color, save from using imageHSV2BGR)
                if (color != -1) {
                    ptr[n * j] = CS.ColorCluster[color].a;
                    ptr[n * j + 1] = CS.ColorCluster[color].b;
                    ptr[n * j + 2] = CS.ColorCluster[color].c;
                } else {
                    ptr[n * j] = CS.ColorCluster[0].a;
                    ptr[n * j + 1] = CS.ColorCluster[0].b;
                    ptr[n * j + 2] = CS.ColorCluster[0].c;
                }
            }
            aRLE.push_back(temp);
            if (i) { // Except the first line that can't be child of any object (only parent) start joining objects grouped in LineObjRLE variables
                unsigned int j = 0, jp = 0; // Pointer to current and previous LineObjRLE
                unsigned int pp = aRLE[i - 1][jp].size, pc = aRLE[i][j].size; // Pointer to previous and current col
                bool end = false; // Flag to manage the loop
                while (!end) {
                    if ((aRLE[i - 1][jp].je > aRLE[i][j].js
                            && aRLE[i - 1][jp].js < aRLE[i][j].je)
                            && aRLE[i - 1][jp].color == aRLE[i][j].color) {
                        aRLE[i][j].parent = &(aRLE[i - 1][jp]);
                        aRLE[i - 1][jp].child = &(aRLE[i][j]);
                        //printf("Parent is %d and says that child is: %d\n", &aRLE[i - 1][jp], aRLE[i - 1][jp].child);
                        //printf("Child is %d and says that parent is: %d\n", &aRLE[i][j], aRLE[i][j].parent);
                    }
                    if (j == aRLE[i].size() - 1 || jp == aRLE[i - 1].size() - 1)
                        end = true;
                    if (pp > pc) {
                        pc += aRLE[i][j].size;
                        j++;
                    } else {
                        pp += aRLE[i - 1][jp].size;
                        jp++;
                    }
                }
            }
        }
        // Run vertically to identifies the parents of the objects

        int k = 0; //Index for final object
        for (unsigned int i = 0; i < aRLE.size(); i++) {
            for (unsigned int j = 0; j < aRLE.at(i).size(); j++) {
                if (aRLE[i][j].parent == NULL && aRLE[i][j].child != NULL) {
                    printf("---------------------------------\n");
                    printf("k: %d \n", k);
                    printf("aRLE: parent: %d  --  child: %d\n",
                            aRLE[i][j].parent, aRLE[i][j].child);
                    // Grow from the seed
                    struct LineObjRLE auxRLE = aRLE[i][j];
                    vector<struct LineObjRLE> obj;
                    while (1) {
                        obj.push_back(auxRLE);
                        printf("auxRLE: parent: %d  --  child: %d\n",
                                auxRLE.parent, auxRLE.child);
                        if (auxRLE.child == NULL)
                            break;
                        auxRLE = *auxRLE.child;
                    }
                    k = k + 1;
                }
            }
        }

        // Calculate increment of time
        t2 = clock();
        float dif = (((float) t2 - (float) t1) / 1000000.0F) * 1000;
        printf("FINISHED IN %f \n", dif);

        imageHSV2BGR(&frame);

        imshow("Viewer", frame); // Show the image segmented
        aRLE.clear();
        objs.clear();
    }
    return 0;
}

最佳答案

查看代码,您似乎在 std::vector<RLE> 中存储指向对象的指针.例如,我发现:

aRLE[i][j].parent = &(aRLE[i - 1][jp]);

同时我没有看到任何对 aRLE.reserve() 的调用这将确保对象不会在添加新对象时重新定位。我怀疑, vector 需要重新分配内存,释放已经指向的内存,你会得到一堆陈旧的指针。

如果您创建了陈旧的指针,那么检验该理论的最简单方法是:

  • 如果你提前知道数据结构的最终大小,你可以使用std::vector<T>::reserve(size_type n)告诉数据结构分配足够的空间来容纳 n元素。
  • 如果你不知道大小但你只需要一些看起来像数组的东西并且你只追加在末尾或开头,你可以替换std::vector<RLE>通过 std::deque<RLE> : 虽然在两端添加对象可能会改变迭代器,但不会改变对象的位置。

关于c++ - 指针和结构导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18176884/

相关文章:

c++ - 如何让 g_print() 出现?

C++ 指针行为异常

c++ - 引用变量和 const 指针变量有什么区别?

c# - 将引用传递给集合元素

delphi - 动态结构的大小

c++ - 关于QList类型实例的复制构造函数的奇怪问题

c++ - 使用 winsock 通过网络发送 Unicode (win32) 文本包?

c++ - QWebFrame load() 在初始页面加载时不发送 cookie

c# - 最快的数组寻址

c - 数组中的 append 值不是 c 中所需的值