用C地址零初始化变量 [英] Initializing variable at address zero in C

查看:142
本文介绍了用C地址零初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是pretty基本问题。据我了解,有一个C约定以空指针的值设置为零。有没有可能是你所能分配空间,在Windows一个新的变量,而分配空间的地址恰好是零?如果不是,通常占该地址区域?

This may be a pretty basic question. I understand that there is a C convention to set the value of null pointers to zero. Is it possible that you can ever allocate space for a new variable in Windows, and the address of that allocated space happens to be zero? If not, what usually occupies that address region?

推荐答案

在MS-DOS空指针是一个相当有效的指针,并因的实模式它实际上可能覆盖垃圾和腐败内核0x0的地址。你可以这样做:

On MS-DOS the null pointer is a fairly valid pointer and due to the OS running in real mode it was actually possible to overwrite the 0x0 address with garbage and corrupt the kernel. You could do something like:

int i;
unsigned char* ptr = (unsigned char *)0x0;
for(i = 0; i < 1024; i++)
    ptr[i] = 0x0;

保护模式,从来没有让你直接访问物理存储器。

Modern operating systems (e.g. Linux, Windows) run in protected mode which never gives you direct access to physical memory.

处理器将将虚拟地址的,你的程序将使用的物理地址映射。

The processor will map the physical addresses to virtual addresses that your program will make use of.

这也使你访问哪些敢跟踪你摸不属于东西给你,你就麻烦了(你的程序将的段错误)。这最肯定包括试图取消引用0x0的地址。

It also keeps track of what you access and dare you touch something not belonging to you will you be in trouble (your program will segfault). This most definitely includes trying to dereference the 0x0 address.

这篇关于用C地址零初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆