Question

I have some weird issue with c program. I am learning c programming on-line and I practise some exercise. On of them is about a imagery technic called erosion. Imagine there is an image with two types of pixels symbolized by either '.' or '#'. When a pixel is surrounded by 4 '#' character, it is preserved, and in the other case it is replaced by a '.' character. The input is N the number of times the erosion is applied, H and L the height and width of the image, and a rectangle of characters composed by '.' and '#' character. For instance input :

1  //N
4  //H
4  //L
....
.###
####
####

and the output is 
....
....
.##.
....

The problem is that the on-line compiler (that tests a random serie of inputs) rejects my code telling that the memory is overflowed

Here is the code

#include <stdlib.h>
#include <stdio.h>

//test wether a pixel is surrounded by 4 '#' characters
char test(int i, int j,int H, int L, char c[H][L]){

    int k=0;
    int l=0;
    char result='-';
    if((i==0)||(i==H-1)||(j==0)||(j==L-1)){
        result='+';
    }
    else{

    for(k=0;k<2;k++){
        for(l=0;l<2;l++){
              if(c[i+(1-2*k)*l][j+(1-2*k)*(1-l)] =='.'){
                result='+';

                break;
            }

            else{

            }
        }
        if(result=='+'){break;}
        else{}
    }
    }

    return result;
}


//The erode function that replaces the image by one in which '#' characters are replaced by '.' characters when it is not surrounded by 4 '#' characters

char **erode(int H, int L, char c[H][L]){

    int i;
    int j;
    char ch='-';

    char **d = malloc (H * sizeof (int *));

    for (i = 0; i < H; i++) {
        d[i] = malloc (L * sizeof (int));
    }

    i=0;

    for (i=0;i<H;i++)
    {
        for (j=0;j<L;j++)
        {
            ch=test(i,j,H,L,c);

            if(ch=='+'){

                d[i][j]='.';

            }
            else{
                d[i][j]=c[i][j];
            }
            ch='-';
        }

    }


for (i= 0; i < H; i++) {
        free(d[i]);
       }
       free(d);

    return d;

}


//here are computed the inputs and outputs
int main()
{   

    int i=0;
    int j=0;


    int N;
    int H;
    int L;
    char o;

    scanf("%d",&N);
    scanf("%d",&H);
    scanf("%d",&L);
    scanf("%c",&o);

    char c[H][L];
    char d[H];
    char ero[H][L];


    while (i<H)
    {
        while (j<L)
        {
            scanf("%c",&c[i][j]);
            j++;
        }
        j=0;
        scanf("%c",&d[i]);
        i++;
    }

    int l;
    int m;
    int n;





    for(l=0;l<N;l++){
    for (i=0;i<H;i++)
    {
        for (j=0;j<L;j++)
        {
        ero[i][j]=erode(H,L,c)[i][j];

        }

    }
        for (m=0;m<H;m++)
        {
            for (n=0;n<L;n++){
                c[m][n]=ero[m][n];
            }

        }

    }


    for (i=0;i<H;i++)
     {
         for (j=0;j<L;j++){
             printf("%c",c[i][j]);
         }
         printf("\n");
     }

}

(The code is far from being optimal because I tried to debugg it and to make something really decomposed)

Does anyone has an idea why I have this message error?

Was it helpful?

Solution

This is probably because within your erode function you are using malloc a number of times but never calling free. You actually call erode inside a triple loop from main. This suggests it could be getting called many times but as the allocated memory never gets released it is entirely possible that your memory is getting exhausted which explains the error message. Take more care to release the memory using free when you no longer need it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top