现在的位置: 首页 > 综合 > 正文

堆被破坏

2017年12月23日 ⁄ 综合 ⁄ 共 872字 ⁄ 字号 评论关闭
void CDlgConfig::CalcMap(void)
{
	m_pXofs = new int[m_dst->width];
	m_pYofs = new int[m_dst->height];
	double ifx = 1./inv_scale_x, ify = 1./inv_scale_y;
	int x ,y;
	// dst中位置对应src中的位置
	for( x = 0; x < m_dst->width; x++ )
	{
		int sx = saturate_cast<int>(x*ifx);
		m_pXofs[x] = std::min(sx, m_src->width-1);
	}
	for( y = 0; y < m_dst->height; y++ )
	{
		int sy = saturate_cast<int>(y*ify);
		m_pXofs[y] = std::min(sy, m_src->height-1);
	}

	// src 中的位置,对应dst的位置
	 // saturate_cast<int> std::min(x*inv_scale_x,m_dst->widht-1)

}

原因:

m_pXofs[y] = std::min(sy, m_src->height-1);

错误。y大于了 new int[] 分配的值。从而破坏了堆

问题的发现受http://zhidao.baidu.com/link?url=OV_SX1Iq7gA1LgQtSLU3w_Tl0a_4RpXtmTuyZfnbAv4ZPI87Lp8-l5o3SzBtSGNzi8aZx7lm5BDwtxQp4w1j1a 启发

从目前的代码看,应该就是以下问题了
从你的方法void Read1(int * data, long n);分析讨论
由于这个方法的第一个参数是data并非为const,也就是执行完这个函数后,main中data与Read1中的地址相同,到此时还未问题
由于没有给出Read2、Read3、Read5,无法判断,LZ可以自己看看这些函数
因为main中int * data=new int[n];即只分配了n个int大小的内存

如果在Read2、Read3、Read5执行后,数组变大了,就会出现这种问题

抱歉!评论已关闭.