分段故障使用功能。但该功能可在独立设置中使用

时间:2017-02-12 18:11:58

标签: c

我目前正在完成一项学校作业,我必须根据图片的颜色变化来构建四叉树。 我编译时遇到麻烦,一切都很好,只有当我执行时,我才会遇到分段错误。我已经运行gdbbacktrace来检查我的错误可能在哪里。

编辑2:遵循Barmar给出的更正。

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401227 in give_moments (picture=0x21ec010, x_min=0, y_min=0, x_max=255, y_max=255,
    moment_o0=0x400ace <create_quadtree+18>, moment_o1=0x400000000, moment_o2=0x21ec010) at image_utile.c:89
89         *moment_o0 = number_pix;
(gdb) bt
#0  0x0000000000401227 in give_moments (picture=0x21ec010, x_min=0, y_min=0, x_max=255, y_max=255,
    moment_o0=0x400ace <create_quadtree+18>, moment_o1=0x400000000, moment_o2=0x21ec010) at image_utile.c:89
#1  0x0000000000400be5 in recursive_node (picture=0x21ec010, limit=100, tree=0x21ec050, x_min=0, x_max=255, y_min=0,
    y_max=255) at quadtree.c:126
#2  0x0000000000400e48 in split_image (picture=0x21ec010, limit=100) at quadtree.c:162

编辑1:这是give_moments()功能。

extern void give_moments(image picture, int x_min, int y_min, int x_max, int y_max,
                         double *moment_o0, double *moment_o1, double *moment_o2) {
    /* Moment order 0 declarations. */
    double number_pix;

    /* Moment order 1 declarations. */
    unsigned char composants1[] = { 0, 0, 0 };
    double moment1[] = { 0, 0, 0 };
    int i, j, m;

    /* Moment order 2 declarations. */
    unsigned char composants2[] = { 0, 0, 0 };
    double moment2[] = { 0, 0, 0 };
    int k, l, n;

    /* Calculates moment order 0. */
    number_pix = (x_max - x_min + 1) * (y_max - y_min + 1);
    *moment_o0 = number_pix;

    /* Calculates moment order 1. */
    for (j = x_min; j < x_max; j++) {
        for (i = y_min; i < y_max; i++) {
            image_read_pixel(picture, i, j, composants1);

            moment1[0] += (double)composants1[0];

            if (image_give_dim(picture) == 3) {
                moment1[1] += (double)composants1[1];
                moment1[2] += (double)composants1[2];
            }
        } 
    }

    for (m = 0; m < 3; m++) {
        moment_o1[m] = moment1[m];
    }

    /* Calculates moment order 2. */
    for (l = x_min; l < x_max; l++) {
        for (k = y_min; k < y_max; k++) {
            image_read_pixel(picture, k, l, composants2);
            moment2[0] += (double)(composants2[0] * composants2[0]);
            if (image_give_dim(picture) == 3) {  
                moment2[1] += (double)composants2[1] * (double)composants2[1];
                moment2[2] += (double)composants2[2] * (double)composants2[2];
            }
        }
    } 
    for (n = 0; n < 3; n++) {
        moment_o2[n] = moment2[n];
    }
}

give_moments()函数需要跟随其他错误。但我找不到*moment_o0 = number_pix;的错误。

希望有人能帮帮我吗?我会非常感激!

1 个答案:

答案 0 :(得分:0)

函数give_moments中出现此类错误的最可能原因是:

  • 参数moment_o0moment_o1moment_o2可以是NULL或指向长度小于3的double数组。