Run-Time Check Failure #2 - Stack around the variable 'temp' was corrupted

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi Everyone,

Ive problem when I try to run the below code using Visual Studio 2010, Is there anyone can help to solve this problem ?? thanks in advace
<br/>
#include<stdio.h><br/>
#include<stdlib.h><br/>
#include<string.h><br/>
#include<conio.h><br/>
#include<math.h><br/>
#include<iostream><br/>
#include<algorithm><br/>
#include <fstream><br/>
<br/>
<br/>
using namespace std;<br/>
const int size=256,ih=256,iw=256;<br/>
void median(unsigned char **buf,unsigned char **out);<br/>
<br/>
<br/>
void input(char filename[100],unsigned char **buf,int ih,int iw)<br/>
{<br/>
<span style="white-space:pre FILE *fp;<br/>
<span style="white-space:pre int i,j;<br/>
<br/>
<br/>
if((fp=fopen(filename,"rb"))==NULL)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("File %s open error ! n",filename);<br/>
<span style="white-space:pre getch();<br/>
<span style="white-space:pre exit(1);<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre for(i=0;i<ih;i++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre for(j=0;j<iw;j++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre buf[j] = (unsigned char)fgetc(fp);<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre }<br/>
fclose(fp);<span style="white-space:pre <br/>
<br/>
<br/>
}<br/>
<br/>
<br/>
void output(char filename[100],unsigned char **buf,int ih,int iw)<br/>
{<br/>
<span style="white-space:pre FILE *fp;<br/>
<span style="white-space:pre int i,j;<br/>
<br/>
<br/>
<span style="white-space:pre if((fp=fopen(filename,"wb"))==NULL)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("nCant open file!!");<br/>
<span style="white-space:pre getch();<br/>
<span style="white-space:pre exit(1);<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre for(i=0;i<ih;i++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre for(j=0;j<iw;j++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre fputc(buf[j], fp);<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre } <br/>
<span style="white-space:pre fclose(fp);<br/>
}<br/>
<br/>
<br/>
void main()<br/>
{<br/>
<br/>
<br/>
<span style="white-space:pre char filename[100];<br/>
unsigned char **buf1,**buf2;<br/>
<span style="white-space:pre int i;<br/>
<br/>
<br/>
<span style="white-space:pre buf1=(unsigned char **) malloc(ih*sizeof(unsigned char *));<br/>
<span style="white-space:pre buf2=(unsigned char **) malloc(ih*sizeof(unsigned char *));<br/>
<span style="white-space:pre for(i=0;i<ih;i++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre buf1=(unsigned char *) malloc(iw*sizeof(unsigned char));<br/>
<span style="white-space:pre buf2=(unsigned char *) malloc(iw*sizeof(unsigned char));<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre <br/>
<span style="white-space:pre cout<<"Open File:"<<endl;<br/>
<span style="white-space:pre scanf("%s",filename);<br/>
<span style="white-space:pre input(filename, buf1, 256, 256);<br/>
<br/>
<br/>
median(buf1,buf2);<br/>
<br/>
<br/>
<span style="white-space:pre cout<<"..........................."<<endl;<br/>
<span style="white-space:pre cout<<"Save file as:"<<endl;<br/>
<span style="white-space:pre scanf("%s",filename);<br/>
<span style="white-space:pre output(filename, buf2, 256 ,256);<br/>
<span style="white-space:pre <br/>
<br/>
<br/>
}<br/>
<br/>
<br/>
<br/>
<br/>
void median(unsigned char **buf,unsigned char **out)<br/>
{<br/>
<br/>
<br/>
// ofstream of;<br/>
// of.open("c:\median filter.txt",ios::out);<br/>
<br/>
<br/>
<span style="white-space:pre int i,j,k,a,b,n;<br/>
<span style="white-space:pre int temp[9],buffer=0;<br/>
<br/>
<br/>
<span style="white-space:pre for(k=0;k<=9;k++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre temp[k]=0;<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre for(i=0;i<ih;i++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre for(j=0;j<iw;j++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre out[j]=0;<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre for(i=1;i<ih-1;i++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre for(j=1;j<iw-1;j++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre n=0;<br/>
<span style="white-space:pre for(a=-1;a<=1;a++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre for(b=-1;b<=1;b++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre temp[n]=buf[i+a][j+b];<br/>
<span style="white-space:pre n++;<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre //±Æ§Ç<br/>
<span style="white-space:pre for(n=0;n<5;n++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre for(k=n;k<9;k++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre if(temp[n]>temp[k])<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre buffer=temp[n];<br/>
<span style="white-space:pre temp[n]=temp[k];<br/>
<span style="white-space:pre temp[k]=buffer;<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre }<br/>
//<span style="white-space:pre of<<"out["<<i<<"]["<<j<<"]="<<temp[4]<<endl; <br/>
<br/>
<br/>
<span style="white-space:pre out[j]=temp[4];<br/>
<br/>
<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre <br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
}<br/>
<br/>
<br/>

<br/>


View the full article
 
Back
Top