两个指针表示的困境 [英] Two pointer representation dilemma

查看:104
本文介绍了两个指针表示的困境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白一台机器如何具有多个指针表示. GNU的以下内容

I don't understand how a machine can have more than one pointer representation. The following from GNU says

如果目标计算机具有两个不同的指针表示形式,则 编译器将不知道该参数使用哪种表示形式.

if the target machine has two different pointer representations, the compiler won't know which representation to use for that argument.

怎么可能?俗语和#define SEM_FAILED ((sem_t*)-1)有什么关系?后者做什么?我知道这是具有恒定值的空指针.但是由于它指向-1,因此如何在内存中表示它?如果它指向正确的位置怎么办?

How is it possible? What is the relationship between the saying and #define SEM_FAILED ((sem_t*)-1)? What does the latter do? I know it is null pointer which has constant value. But how is it represented in a memory since it points to -1? What about if it points to a right location?

推荐答案

C最早针对的体系结构之一是某些具有36位或18位单词单词(类型为int)的体系结构.使用本地指针,只有单词可以直接在诸如0、1、2之类的地址上寻址.但是,每个字符用一个单词会浪费太多内存,因此添加了9位char类型,一个单词中包含2个或4个字符.由于无法通过单词指针寻址这些地址,因此char *由两个单词组成:一个指向该单词,另一个告诉该单词中的哪个字节应进行操作.

One of the very first architectures that C targeted were some with 36-bit or 18-bit words words (the type int). Only the words were directly addressable at addresses like 0, 1, 2 using the native pointers. However one word for one character would have wasted too much memory, so a 9-bit char type was added, with 2 or 4 characters in one word. Since these would not have been addressable by the word pointer, char * was made from two words: one pointing to the word, and another telling which of the bytes within the word should be manipulated.

当然,现在的问题是char *是两个单词,而int *只是一个单词,这在调用没有原型或带有省略号的函数时很重要-而(void*)0的表示形式与(char *)0,它与(int *)0不兼容,因此需要显式强制转换.

Of course now the problem is that char * is two words wide, whereas int * is just one, and this matters when calling a function without prototype or with ellipsis - while (void*)0 would have a representation compatible with (char *)0, it wouldn't be compatible with (int *)0, hence an explicit cast is required.

NULL还有另一个问题.尽管GCC似乎可以确保NULL的类型为void *,但是C标准不能保证如此,因此即使在execl之类的函数调用中使用NULL且期望将char * s作为变量参数也是错误的没有强制转换,因为实现可以定义

There is another problem with NULL. While GCC seems to assure that NULL will be of type void *, the C standard does not guarantee that, so even using NULL in a function call like execl that expects char *s as variable arguments is wrong without a cast, because an implementation can define

#define NULL 0


(sem_t*)-1不是NULL指针,它是整数-1转换为具有实现定义的结果的指针.在POSIX系统上,(必然)将导致其地址永远不能为任何sem_t的位置.


(sem_t*)-1 is not a NULL pointer, it is the integer -1 converted to pointer with implementation-defined results. On POSIX systems it will (by necessity) result in an address that can never be a location of any sem_t.

在这里使用-1实际上是一个非常糟糕的约定,因为生成的地址很可能没有>的正确对齐,因此整个构造本身具有未定义的行为

It is actually a really bad convention to use -1 here since the resulting address most likely doesn't have a correct alignment for sem_t, so the entire construct has undefined behaviour in itself.

这篇关于两个指针表示的困境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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