int* 转double* 时的内存越界访问

来源:互联网 发布:淘宝怎么免费装修店铺 编辑:程序博客网 时间:2024/05/29 10:42

         今天一同事让我帮看一个程序, 最后定位到的问题是内存越界:

int main(){int a = 0;*(double*)&a = 0; // 访问了非法内存, 运行时出错return 0;}

#include <iostream>using namespace std;int main(){int a = -1;int b = -1;*(double*)&b = 0; // 访问了a的内存, 其实也危险, 运行时侥幸没有出错cout << a << endl; // 0return 0;}

     总之, 像这样大小不一的转化, 要杜绝。

0 0
原创粉丝点击