gpt4 book ai didi

c - NULL 宏什么时候不是 0?

转载 作者:太空狗 更新时间:2023-10-29 16:22:12 25 4
gpt4 key购买 nike

我依稀记得几年前读过这个,但我在网上找不到任何引用资料。

你能给我一个 NULL 宏没有展开为 0 的例子吗?

为清楚起见进行编辑:今天它扩展为 ((void *)0)(0)(0L)。然而,有些架构早已被遗忘,但事实并非如此,NULL 扩展到不同的地址。有点像

#ifdef UNIVAC
#define NULL (0xffff)
#endif

我正在寻找此类机器的示例。

解决问题的更新:

我的意思不是在当前标准的上下文中提出这个问题,也不是要用我不正确的术语来扰乱人们。然而,我的假设被接受的答案证实了:

Later models used [blah], evidently as a sop to all the extant poorly-written C code which made incorrect assumptions.

有关当前标准中空指针的讨论,请参阅 this question .

最佳答案

C FAQ 有一些具有非 0 NULL 表示的历史机器示例。

来自 The C FAQ List , question 5.17 :

Q: Seriously, have any actual machines really used nonzero null pointers, or different representations for pointers to different types?

A: The Prime 50 series used segment 07777, offset 0 for the null pointer, at least for PL/I. Later models used segment 0, offset 0 for null pointers in C, necessitating new instructions such as TCNP (Test C Null Pointer), evidently as a sop to [footnote] all the extant poorly-written C code which made incorrect assumptions. Older, word-addressed Prime machines were also notorious for requiring larger byte pointers (char *'s) than word pointers (int *'s).

The Eclipse MV series from Data General has three architecturally supported pointer formats (word, byte, and bit pointers), two of which are used by C compilers: byte pointers for char * and void *, and word pointers for everything else. For historical reasons during the evolution of the 32-bit MV line from the 16-bit Nova line, word pointers and byte pointers had the offset, indirection, and ring protection bits in different places in the word. Passing a mismatched pointer format to a function resulted in protection faults. Eventually, the MV C compiler added many compatibility options to try to deal with code that had pointer type mismatch errors.

Some Honeywell-Bull mainframes use the bit pattern 06000 for (internal) null pointers.

The CDC Cyber 180 Series has 48-bit pointers consisting of a ring, segment, and offset. Most users (in ring 11) have null pointers of 0xB00000000000. It was common on old CDC ones-complement machines to use an all-one-bits word as a special flag for all kinds of data, including invalid addresses.

The old HP 3000 series uses a different addressing scheme for byte addresses than for word addresses; like several of the machines above it therefore uses different representations for char * and void * pointers than for other pointers.

The Symbolics Lisp Machine, a tagged architecture, does not even have conventional numeric pointers; it uses the pair <NIL, 0> (basically a nonexistent <object, offset> handle) as a C null pointer.

Depending on the "memory model" in use, 8086-family processors (PC compatibles) may use 16-bit data pointers and 32-bit function pointers, or vice versa.

Some 64-bit Cray machines represent int * in the lower 48 bits of a word; char * additionally uses some of the upper 16 bits to indicate a byte address within a word.

关于c - NULL 宏什么时候不是 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2597142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com