我对 c 程序有一些奇怪的问题。我正在在线学习 c 编程并练习一些练习。其中关于一种称为侵蚀的图像技术。想象一下,有一幅图像具有两种类型的像素,分别用“.”符号表示。或者 '#'。当一个像素被 4 个 '#' 字符包围时,它会被保留,而在其他情况下,它会被一个 '.' 替换。特点。输入是 N 应用腐蚀的次数,H 和 L 是图像的高度和宽度,以及由“.”组成的字符矩形。和“#”字符。例如输入:
1 //N
4 //H
4 //L
....
.###
####
####
and the output is
....
....
.##.
....
问题是在线编译器(测试一系列随机输入)拒绝我的代码,告诉我内存溢出
这是代码
#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");
}
}
(代码远不是最优的,因为我试图调试它并使某些东西真正分解)
有谁知道为什么我有这个消息错误?