0

I am currently working on a school assignment where I have to build a quadtree out of a picture's color variation. I have trouble as when I compile, everything's fine, only, when I execute, I get a segmentation fault. I've ran gdband backtrace to check where my errors could be.

Edit 2: After following the corrections given by 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

Edit 1: Here is the give_moments() function.

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];
    }
}

The give_moments() function entails the other following errors. But I can't find what's wrong with *moment_o0 = number_pix;.

Hopefully anyone could help me? I'd be so thankful!

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Julie
  • 63
  • 9
  • 2
    [don't cast malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Feb 12 '17 at 18:13
  • 3
    It's very confusing to use `typedef` to define the structure name as a pointer, not the structure. – Barmar Feb 12 '17 at 18:14
  • 1
    See http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers – Barmar Feb 12 '17 at 18:15
  • 1
    Why are you using `malloc()` for `m0`, `m1`, and `m2`. Since you allocate and free them in the same function, you can just use automatic variables. – Barmar Feb 12 '17 at 18:17
  • 1
    Thank you, I've removed the cast on the `malloc`. As for using `typedef`, it was a requirement of the assigment... If I don't `malloc()` them, I get the following warning: warning: `‘m1’ is used uninitialized in this function` – Julie Feb 12 '17 at 18:17
  • Usually it's `typedef struct quadtree quadtree`, this allows you to use `quadtree* tree` as a pointer declaration. It's not a good idea to hide which variables are pointers. Are you sure you didn't misunderstand the requirement? – Barmar Feb 12 '17 at 18:20
  • `valgrind` may be useful for debugging this. Maybe the problem is in `give_moments`, which you haven't shown. – Barmar Feb 12 '17 at 18:22
  • It asks to "define the type quadtree as a pointer on a struct quadtree." It would have been less confusing to do as you mentioned, but I think the assignment wants to hide it that way? – Julie Feb 12 '17 at 18:24
  • I tried to run `valgrind` only it doesn't work on my current computer. I did run it before, and it seemed like the issue was on the `create_quadtree()` function... I will put `give_moments` in my post, but I've already ran this function alone before, and I didn't have any apparent issues. – Julie Feb 12 '17 at 18:26
  • I don't see anything wrong in `create_quadtree()`. – Barmar Feb 12 '17 at 18:28
  • Now that I've removed the cast and the `malloc()` in `recursive_node()`, I get the following error in the backtrace for segmentation fault: (posted in a second edit). – Julie Feb 12 '17 at 18:33
  • Needs more [mcve]. – melpomene Feb 12 '17 at 18:49
  • What happened to all the original code in the question? – Barmar Feb 13 '17 at 06:16
  • @DitSyvala: you removed all of the original code in the question and rephrased the question several times... This makes the discussion in the comments inconsistent. You are not supposed to do this. Edits may alter the question text to make it more readable (fixing typos, grammar, code indentation and spacing...), but removing parts of the question that have already been commented or referred to in the answers is not advisable. Edits can add to the question with clearly marked clarifications, extra code and data, preferably at the end. – chqrlie Feb 13 '17 at 06:37

1 Answers1

0

The most likely cause for such an error in function give_moments is this:

  • arguments moment_o0, moment_o1 or moment_o2 could be NULL or point to arrays of double with a length less than 3.
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • You are right; it worked for a colored picture but not a black and white one. I will try to find a way to initialize depending on the different case. Thank you for your input! – Julie Feb 12 '17 at 19:57