申请内存过大,堆栈溢出,哪位高手帮忙改一下。。

ji_qi_mao 2011-03-22 11:07:09
这是对Lena.bmp图片加上高斯噪声和椒盐噪声的程序,但是运行时不对,应该是溢出问题。哪位高手帮忙修改一下~
一些定义如下:
typedef struct
{
char id[2];
unsigned long fileSize;
unsigned long reserved0;
unsigned long bitmapDataOffset;
unsigned long bitmapHeaderSize;
unsigned long width;
unsigned long height;
unsigned short planes;
unsigned short bitsPerPixel;
unsigned long compression;
unsigned long bitmapDataSize;
unsigned long hRes;
unsigned long vRes;
unsigned long colors;
unsigned long importantColors;

} BMPHeader_t;


typedef struct
{
unsigned char Red;
unsigned char Green;
unsigned char Blue;
} Pixel_t;



class BmpImageUtil
{
public:
BMPHeader_t header;
BmpImageUtil(void);
~BmpImageUtil(void);
void ParseBmpHeader(ifstream & fin);

void ZoomIn(ofstream & fout, int multiple, bool useZero);
void ZoomOut(ofstream & fout, int multiple, bool useZero);

void GaussianNoise(ofstream & fout);

void GaussianFilter(ofstream & fout);
void GaussianFilter3(ofstream & fout);


void SaltPepperNoise(ofstream & fout);
void MedianFilter(ofstream & fout);

void verifyLoad(ofstream & fout);


private:
Pixel_t matrix[512][512];
Pixel_t buff[3072];

void loadMatrix(ifstream & fin);
void writeFileHeader(BMPHeader_t header, ofstream & fout);
void zero_matrix_out(ofstream & fout, int multiple);
void zero_matrix_operation(int multiple, ofstream & fout);
void writeFileContent(ofstream & fout, unsigned long pixel_number, Pixel_t * martix_new);
double Gaussian(double expection, double sigma);
unsigned char MidPixel(unsigned char * buff);
};

void BmpImageUtil::GaussianFilter3(ofstream & fout)
{

int templates[25] = { 1,1,1,
1,1,1,
1,1,1};
Pixel_t smooth[512][512]; ////////////////////////////////////这里溢出


memcpy ( smooth, this->matrix, sizeof(this->matrix));

for (int j=2;j<this->header.width-1;j++)
{
for (int i=2;i<this->header.height-1;i++)
{
int sum = 0;
int index = 0;
for ( int m=j-1; m<j+2; m++)
{
for (int n=i-1; n<i+2; n++)
{
sum += this->matrix[m][n].Blue * templates[index++] ;
}
}
sum /= 9;
if (sum > 255)
sum = 255;
smooth [j][i].Blue =
smooth [j][i].Green =
smooth [j][i].Red = sum;
}
}
this->writeFileHeader(this->header, fout);

for(int i = 0;i < this->header.width;i++)
for(int j = 0;j < this->header.height;j++)
fout.write((char *)&(smooth[i][j]), sizeof(Pixel_t));
fout.flush();
}
有问题的函数部分如下:

void BmpImageUtil::GaussianFilter(ofstream & fout)
{

int templates[25] = { 1, 4, 7, 4, 1,
4, 16, 26, 16, 4,
7, 26, 41, 26, 7,
4, 16, 26, 16, 4,
1, 4, 7, 4, 1 };
Pixel_t smooth[512][512];////////////////////////////////////这里溢出


memcpy ( smooth, this->matrix, sizeof(this->matrix));

for (int j=2;j<this->header.width-2;j++)
{
for (int i=2;i<this->header.height-2;i++)
{
int sum = 0;
int index = 0;
for ( int m=j-2; m<j+3; m++)
{
for (int n=i-2; n<i+3; n++)
{
sum += this->matrix[m][n].Blue * templates[index++] ;
}
}
sum /= 273;
if (sum > 255)
sum = 255;
smooth [j][i].Blue =
smooth [j][i].Green =
smooth [j][i].Red = sum;
}
}
this->writeFileHeader(this->header, fout);

for(int i = 0;i < this->header.width;i++)
for(int j = 0;j < this->header.height;j++)
fout.write((char *)&(smooth[i][j]), sizeof(Pixel_t));
fout.flush();
}
void BmpImageUtil::MedianFilter(std::ofstream & fout)
{
unsigned char square[9];
Pixel_t p[512][512];////////////////////////////////////这里溢出
this->writeFileHeader(this->header,fout);
memcpy(p, this->matrix, sizeof(this->matrix));

for(int i = 1;i < this->header.width - 1;i++)
{
for(int j = 1;j < this->header.height - 1;j++)
{
int index = 0;
for(int m = i - 1; m < i + 2;m++)
{
for(int n = j - 1;n < j + 2;n++)
{
square[index] = this->matrix[m][n].Blue;
index++;
}
}
p[i][j].Red = p[i][j].Blue = p[i][j].Green = this->MidPixel(square);
}
}

for( i = 0;i < this->header.width;i++)
{
for(int j = 0;j < this->header.height;j++)
{
fout.write((char *)&(p[i][j]), sizeof(Pixel_t));
}
}

fout.flush();
}
...全文
530 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
Pixel_t matrix[512][512]==> 512*512*3 = 786432. //sizeof(Pixel_t)=3
Pixel_t buff[3072]==> 3072*3 = 9216
Pixel_t smooth[512][512]==> 512*512*3 = 786432
合计:
786432*2+9216=1582080个byte
=1545K
=1.508M
Windows上的程序默认栈的大小是1-2M,单三个家伙就快2M了,考虑到你的程序中的其它地方使用栈的情况,很容易就Over了
建议用一个类将Pixel_t(指向char的指针数组)包装起来,构造函数中在堆上申请内存,析构函数中释放。
理论上堆的大小和系统虚拟内存大小一样大(在硬盘上虚拟内存)

pengzhixi 2011-03-23
  • 打赏
  • 举报
回复
Pixel_t matrix[512][512];
Pixel_t buff[3072];
有了这样2个成员,栈想不用尽都不行。
碎碎念 2011-03-23
  • 打赏
  • 举报
回复
栈空间只有1M...可能是溢出了...

比较大的数组用new分配在堆上...

不过,要记得在不用的时候free...
pathuang68 2011-03-22
  • 打赏
  • 举报
回复
估计是栈溢出了。

像Pixel_t p[512][512];都是静态地在栈内存分配,栈的特点就是一般不是很大,所以比较容易溢出,但它的速度比堆快。比如一般函数中的局部变量、参数都是存放在栈里面的。

通常情况下,堆比栈大很多,在堆上申请内存,需要用到C++中的new或者C中的malloc函数。

所以建议,将相关的二位数组的内存,从堆上申请。
hhh_hao 2011-03-22
  • 打赏
  • 举报
回复
vc设置选项,将栈空间改大点
wangtingguang 2011-03-22
  • 打赏
  • 举报
回复
局部变量都是放在栈上。是不是栈溢出了啊,可以在堆里面动态申请嘛 ,new 或者malloc ,

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧