是什么0xdeadbeef在地址0x0是什么意思? [英] what does 0xdeadbeef in address 0x0 mean?

查看:338
本文介绍了是什么0xdeadbeef在地址0x0是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在一个$ C $看到c中的以下行:

I just saw in a code the following line :

#define ERR_FATAL( str, a, b, c ) {while(1) {*(unsigned int *)0 = 0xdeadbeef;} }

我知道0xdeadbeef意味着错误,但是这个值是什么意思时,把它在地址0?
什么地址0重新presents?

I know that 0xdeadbeef means error, but what putting this value mean when it's in address 0 ? What address 0 represents ?

推荐答案

在地址0x0是由编译器识别为一个的 NULL指针,然后将是一个实现定义无效的内存地址,在一些系统上是毫不夸张地在系统内存中寻址的起始地址,但在其他它只是在code对于一些通用的无效的内存地址的占位符。从查看C code,我们不知道会是什么地址的地步,只知道它是无效的访问。从本质上讲这是什么code片断试图做的是写一个非法的内存地址,以及值0xdeadbeef。本身的价值是一些十六进制的阐述了死牛,因此表明程序是死牛肉(即有问题),如果你不是以英语为母语,我可以看到这可能不是那么清楚:)。这个想法是,这将触发分段错误或相似,通知您有该程序没有在过渡期间(宏名 ERR_FATAL进行清理或其他操作,立即终止问题的意图在这个提示)。大多数操作系统不给程序在系统中所有的内存和code presumes操作系统不会让你直接访问的位于地址0x0直接访问内存。既然你标记这个问题与Linux标签,这是你会看到(因为Linux将不允许该内存地址的访问)的行为。请注意,如果你在类似的嵌入式系统那里没有这样的保证工作那么这可能会导致一堆问题,因为你可能会覆盖一些重要的东西。

The address 0x0 is recognized by the compiler as a NULL pointer and is going to be an implementation defined invalid memory address, on some systems is quite literally at the first addressable location in the system memory but on others it's just a placeholder in the code for some generic invalid memory address. From the point of view of the C code we don't know what address that will be, only that it's invalid to access it. Essentially what this code snippet is trying to do is to write to an "illegal" memory address, with the value 0xdeadbeef. The value itself is some hex that spells out "dead beef" hence indicating that the program is dead beef (ie. a problem), if you aren't a native english speaker I can see how this might not be so clear :). The idea is that this will trigger a segmentation fault or similar, with the intention of informing you that there is a problem by immediately terminating the program with no cleanup or other operations performed in the interim (the macro name ERR_FATAL hints at that). Most operating systems don't give programs direct access to all the memory in the system and the code presumes that the operating system won't let you directly access memory that's located at address 0x0. Given that you tagged this question with the linux tag this is the behavior you will see (because linux will not allow an access to that memory address). Note that if you are working on something like an embedded system where there's no such guarantee then this could cause a bunch of problems as you might be overwriting something important.

请注意,有将是更好的出路有报道比这个不依赖于特定类型的未定义行为造成一定的副作用的问题。像断言使用的东西是要可能是一个更好的选择。如果你想使用终止程序中止()是一个在标准库更好的选择,因为它和你想要做什么。请参阅从ComicSansMS回答所有关于为什么这是preferable。

Note that there's going to be better ways out there to report problems than this that don't depend on certain types of undefined behaviors causing certain side effects. Using things like assert is going to likely be a better choice. If you want to terminate the program using abort() is a better choice as it in the standard library and does exactly what you want. See the answer from ComicSansMS for more about why this is preferable.

这篇关于是什么0xdeadbeef在地址0x0是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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