Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:55:14+00:00 2026-06-08T05:55:14+00:00

I have some weird issue with c program. I am learning c programming on-line

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:55:16+00:00Added an answer on June 8, 2026 at 5:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird issue. I have some icons that are being built based
I seem to have some weird issue going on that I am sure will
I am having some weird issue here. I have a database table which has
My first issue: Because of some weird problem with Firefox I have to put
I have a weird issue with session variables. I'm storing some credentials in sessions
I have been going through this weird issue on some of my virtual machines.
I have some weird issue here as somehow I am unable to pass NSString
I have spent some time debugging a weird issue with ARC and custom dealloc
I have a very weird issue. I have a UserControl that has some controls
I have a large data.frame displaying some weird properties when plotted. I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.