首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

诡异的段异常

2013-07-11 
诡异的段错误#include stdio.h#include stdint.hstruct info_t {uint16_t i}int main(){struct info

诡异的段错误


#include <stdio.h>
#include <stdint.h>
struct info_t {
    uint16_t i;
};
int main()
{
    struct info_t info;
    printf("%d",sizeof(struct info_t));
    scanf("%x",&(info.i));
    printf("%x\n",info.i);
    return 0;
}


这么简单的程序都会有段错误,而且我还调试不出来!想去撞墙了

[解决办法]

scanf("%x",&(info.i));//越界了哦
//1
unsigned  int t;
scanf("%x",&t);
info.i = (uint16_t)t;

//2 
scanf("%hx",&(info.i));

[解决办法]
内存越界这些问题是不会在越界的时候就暴露出来的.
它只会默默的修改不属于它的内存.
等到内存的所有者去使用的时候, 里面已经不是它的东西了, 造成的错误也无法预测.

你的这个例子, 应该是会覆盖到 main 函数的返回地址, 导致 main 执行完后返回到一个无效的地址上去了.

热点排行