一个隐蔽的C内存越界错误

来源:互联网 发布:powermill软件下载 编辑:程序博客网 时间:2024/04/29 14:38
 void set3(int *tmp)
{
 *tmp = 6;
}
 
int main(int argc, char* argv[])
{
 int a = 0;
 char * pcTemp = NULL;
 int * piTemp = NULL;
 pcTemp = (char*)malloc(3);
 if( NULL == pcTemp )
 {
  return 0;
 }
// *(int *)pcTemp = 6;
// *pcTemp = (int)6; 
 set3((int *)pcTemp);
 
 printf("Hello World!/n");
 free(pcTemp);
 return 0;
}
 
 
////////////////////////////////////////////////
pcTemp 只malloc了3个字节,
但是在set3函数中,将其转换成int指针后,int指针所指的4个字节的内容被赋值了,造成内存越界。
原创粉丝点击