简体   繁体   中英

Segmentation Fault on 2D array in C

I'm making what we call an integral image, but I have a problem. When I reach a certain number I have an Segmentation fault. The image is 273*273 it works, 274*274 not working. So i tried to Malloc my double array. Same. I've read that arrays in a function are in the Stack, and the stack has small space. How can I make my array declared in the memory?

I am using C99 and SDL:

void integral_img(SDL_Surface * img)
{
    int **M;

    M = malloc(img->w * sizeof(int *));
    for (int i = 0; i < img->w; i++)
    {
        M[i] = malloc(img->h * sizeof(int));
    }
    int sum_int = 0;
    Uint8 sum = 0;

    for (int i = 0; i < img->h; i++)
    {
        for (int j = 0; j < img->w; j++)
        {
            SDL_GetRGB(getpixel(img, i, j), img->format, &sum, &sum, &sum);

            if (i == 0 && j == 0)
                sum_int = (int)sum;
            if (i == 0 && j > 0)
                sum_int = (int)sum + M[i][j - 1];
            if (j == 0 && i > 0)
                sum_int = (int)sum + M[i - 1][j];
            if (i > 0 && j > 0)
                sum_int = (int)sum + M[i - 1][j] + M[i][j - 1] - M[i - 1][j - 1];
            M[i][j] = sum_int;
        }
    }
}
  • You are allocating a pointer-based lookup table, not a 2D array. It will work, but it is wide-spread incorrect practice: it makes the program needlessly slow for no gain. Consider using a 2D array instead:

     int (*M)[w] = malloc( sizeof(int[h][w]) ); ... free(M); 
  • One bug is here:

     M = malloc(img->w * sizeof(int *)); for (int i = 0; i < img->w; i++) { M[i] = malloc(img->h * sizeof(int)); } 

    Here you declare the inner-most dimension of your lookup table to be w , width.

    But later on

     for (int i = 0; i < img->h; i++) { for (int j = 0; j < img->w; j++) ... M[i][j] = sum_int; 

    you say that the inner-most dimension is h , height.

  • Furthermore, your program leaks a lot of memory each time you call the function, since you don't free memory anywhere. In fact, you don't seem to even use the lookup table after filling it up.

A simplified version of your code, modified to illustrate 2D array initialization and cleanup (you must free what you malloc):

In short, your array initialization has to follow the way you create memory for row , column :

typedef struct  {
    int h;
    int w;
}SDL_Surface;
//if you change your prototype to pass the initializer then you can
//free it in the calling function.  Otherwise, as written in your OP
//you will have a memory leak.
void integral_img(SDL_Surface * img, int **m);

int main(int argc, char* argv[])
{
    SDL_Surface img;
    int **M; 

    img.h = 100;
    img.w = 50;
    //create memory for array in calling function
    M = malloc(img.w * sizeof(int *));
    if(!M){ /*handle error and leave*/ }//test results of malloc
    for (int i = 0; i < img.w; i++)//... If you create space for w rows and h columns...
    {
        M[i] = malloc(img.h * sizeof(int));// note modified prototype
        if(!M[i]) {/*handle error and leave*/} //test results of malloc
    }

    integral_img(&img, M);

    //use img...

    //free M :(no memory leaks)

    for (int i = 0; i < img.w; i++)
    {
        if(M[i]) free(M[i]);
    }
    if(M) free(M);


    return 0;
}

void integral_img(SDL_Surface * img, int **m)
{


    for (int i = 0; i < img->w; i++)//... Then you must access w rows and h columns
    {                               // (w & h were reversed in your code)
        for (int j = 0; j < img->h; j++)
        {
            m[i][j] = (i+1)*(j+1);
        }
    }
}

i don't think that your code is bogus, but i am suspecting this line:

SDL_GetRGB(getpixel(img, i, j), img->format, &sum, &sum, &sum);

any way, why you have to allocate a 2D array such way? why not just one continues block?

take a look to my code:

void integral_img(SDL_Surface * img)
{
    #define _IDX(x,y) ((x)+((y)*cx))
   int cx=img->w,
        cy=img->h;
    int *M;

    //allocating only one continues block of memory
    M = malloc(cx*cy * sizeof(int ));
    if(!M){
        perror("not enough memory");
        exit(1);
    }

    int sum_int = 0;
    Uint8 t sum = 0;

    for (int i = 0; i < cy; i++)
    {
        for (int j = 0; j < cx; j++)
        {
            SDL_GetRGB(getpixel(img, i, j), img->format, &sum, &sum, &sum);

            if (i == 0 && j == 0)
                sum_int = (int)sum;
            if (i == 0 && j > 0)
                sum_int = (int)sum + M[_IDX(i,j - 1)];
            if (j == 0 && i > 0)
                sum_int = (int)sum + M[_IDX(i- 1,j )];
            if (i > 0 && j > 0)
                sum_int = (int)sum + M[_IDX(i- 1,j )] + M[_IDX(i,j - 1)] - M[_IDX(i- 1,j - 1)];
            M[_IDX(i,j )] = sum_int;
        }
    }

    // easy to free
    free(M); 
    return;
}

it is cleaner and uses less iterations on allocating and freeing memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM