用C指针实现细节 [英] Pointer implementation details in C

查看:118
本文介绍了用C指针实现细节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的架构违反假设我在下面列出。另外,我想知道是否有任何的假设是所有的架构(即,如果任何人都只是完全错误的)。

I would like to know architectures which violate the assumptions I've listed below. Also, I would like to know if any of the assumptions are false for all architectures (that is, if any of them are just completely wrong).


  1. 的sizeof(INT *)==的sizeof(字符*)==的sizeof(void *的)==的sizeof(func_ptr *)

  1. sizeof(int *) == sizeof(char *) == sizeof(void *) == sizeof(func_ptr *)

所有的指针为给定的架构在内存中再presentation是一样的,不管数据类型的指向。

The in-memory representation of all pointers for a given architecture is the same regardless of the data type pointed to.

的指针的内存中重新presentation是一样的相同的比特长度作为体系结构的一个整数。

The in-memory representation of a pointer is the same as an integer of the same bit length as the architecture.

乘法和指针数据类型划分只由编译器禁止的。注:是的,我知道这是荒谬的。我的意思是 - ?有没有硬件支持禁止这种不正确使用

Multiplication and division of pointer data types are only forbidden by the compiler. NOTE: Yes, I know this is nonsensical. What I mean is - is there hardware support to forbid this incorrect usage?

所有指针值可强制转换为一个整数。换句话说,什么样的架构仍然使用段和偏移?

All pointer values can be casted to a single integer. In other words, what architectures still make use of segments and offsets?

递增一个指针相当于增加的sizeof(有针对性的数据类型)通过指针存储在内存地址。如果 P INT32 * 然后 P + 1 是等于4个字节后 p 的内存地址。

Incrementing a pointer is equivalent to adding sizeof(the pointed data type) to the memory address stored by the pointer. If p is an int32* then p+1 is equal to the memory address 4 bytes after p.

我最常用的,以在一个连续的,虚拟内存空间被使用指针。对于使用情况,我一般可以通过在数轴上他们思想为地址得到的。见堆栈溢出问题的 指针比较

I'm most used to pointers being used in a contiguous, virtual memory space. For that usage, I can generally get by thinking of them as addresses on a number line. See Stack Overflow question Pointer comparison.

推荐答案

我不能给你所有这些的具体例子,但我会尽我所能。

I can't give you concrete examples of all of these, but I'll do my best.

sizeof(int *) == sizeof(char *) == sizeof(void *) == sizeof(func_ptr *)

我不知道我在哪里的知道的任何系统的的这个是假的,但考虑到:

I don't know of any systems where I know this to be false, but consider:

移动设备通常具有其中程序code和这样被存储的只读存储器的一些量。只读值(常量变量)可以设想被存储在只读存储器。并且由于ROM地址空间可大于普通RAM地址空间较小,指针尺寸可以是不同的,以及。同样地,函数指针可以具有不同的大小,因为它们可能指向这个只读加载程序到哪个存储器,并能以其他方式不被修改(从而数据不能被存储在它)。

Mobile devices often have some amount of read-only memory in which program code and such is stored. Read-only values (const variables) may conceivably be stored in read-only memory. And since the ROM address space may be smaller than the normal RAM address space, the pointer size may be different as well. Likewise, pointers to functions may have a different size, as they may point to this read-only memory into which the program is loaded, and which can otherwise not be modified (so your data can't be stored in it).

所以我不知道哪个我观察到上面并没有持有任何平台,但我可以想象系统中可能出现这种情况。

So I don't know of any platforms on which I've observed that the above doesn't hold, but I can imagine systems where it might be the case.

所有的指针的一个给定体系结构内的存储器重新presentation是相同的,无论该数据类型的指向

The in-memory representation of all pointers for a given architecture is the same regardless of the data type pointed to.

想想成员指针VS普通指针。它们不具有相同的重presentation(或大小)。成员指针由一个这个指针和一个偏移量。

Think of member pointers vs regular pointers. They don't have the same representation (or size). A member pointer consists of a this pointer and an offset.

和如上述,可以想到,一些CPU的将常数数据加载到存储器的单独的区域,其中使用一个单独的指针格式

And as above, it is conceivable that some CPU's would load constant data into a separate area of memory, which used a separate pointer format.

的指针的内存中重新presentation是一样的相同的比特长度作为体系结构的一个整数。

The in-memory representation of a pointer is the same as an integer of the same bit length as the architecture.

取决于该比特长度是如何定义的。 :)
一个 INT 在许多64位平台还是32位。但指针是64位。
前面已经说过,CPU与存储器分段模式将有由一对数字的指针。同样,会员指针由一对数字的。

Depends on how that bit length is defined. :) An int on many 64-bit platforms is still 32 bits. But a pointer is 64 bits. As already said, CPU's with a segmented memory model will have pointers consisting of a pair of numbers. Likewise, member pointers consist of a pair of numbers.

乘法和指针数据类型划分只由编译器禁止的。

Multiplication and division of pointer data types are only forbidden by the compiler.

最后,在编译器的指针数据类型只的存在的。是什么CPU可以与不是指针,但整数和内存地址。因此,有无处凡指针类型这些操作的可能的被禁止。你还不如索要CPU禁止C ++字符串对象的串联。它不能这样做,因为C ++字符串类型只存在于C ++语言,而不是在生成的机器code。

Ultimately, pointers data types only exist in the compiler. What the CPU works with is not pointers, but integers and memory addresses. So there is nowhere else where these operations on pointer types could be forbidden. You might as well ask for the CPU to forbid concatenation of C++ string objects. It can't do that because the C++ string type only exists in the C++ language, not in the generated machine code.

不过,要回答你的的意思是的,抬头摩托罗拉68000处理器。我相信他们有整数和内存地址独立的寄存器。这意味着他们可以轻松地禁止这种荒谬的行动。

However, to answer what you mean, look up the Motorola 68000 CPUs. I believe they have separate registers for integers and memory addresses. Which means that they can easily forbid such nonsensical operations.

所有指针值可强制转换为一个整数。

All pointer values can be casted to a single integer.

您是安全的在那里。 C和C ++标准的保证,这是总是可能的,无论是内存空间布局,CPU架构和其他任何东西。具体来说,他们保证有实现定义映射的。换句话说,您可以将指针总是转换为整数,然后将其转换回整数拿到原来的指针。但是,C / C ++语言只字不提中间整数值应该是什么。这是由个人编译器和硬件,它的目标。

You're safe there. The C and C++ standards guarantee that this is always possible, no matter the memory space layout, CPU architecture and anything else. Specifically, they guarantee an implementation-defined mapping. In other words, you can always convert a pointer to an integer, and then convert that integer back to get the original pointer. But the C/C++ languages say nothing about what the intermediate integer value should be. That is up to the individual compiler, and the hardware it targets.

递增一个指针相当于增加的sizeof(有针对性的数据类型)通过指针存储在内存地址。

Incrementing a pointer is equivalent to adding sizeof(the pointed data type) to the memory address stored by the pointer.

再次,这是保证。如果您认为概念,一个指针不指向一个地址,它指向一个的对象的,那么这非常有意义。再增加一个指针显然将使它指向的下次的对象。如果一个对象是20字节长,则递增指针将其移动20个字节,使得它移动到下一个的对象

Again, this is guaranteed. If you consider that conceptually, a pointer does not point to an address, it points to an object, then this makes perfect sense. Adding one to the pointer will then obviously make it point to the next object. If an object is 20 bytes long, then incrementing the pointer will move it 20 bytes, so that it moves to the next object.

如果一个指针仅仅是在一个线性地址空间中的存储器地址,如果是基本上是一个整数,则递增它将加1的地址 - 即,它会移动到下一个的字节

If a pointer was merely a memory address in a linear address space, if it was basically an integer, then incrementing it would add 1 to the address -- that is, it would move to the next byte.

最后,我在你的问题的评论中提到,要记住,C ++只是一种语言。它不关心它的架构被编译成。许多这些限制似乎对现代CPU的晦涩难懂。但是,如果你的目标是什么昔日的CPU的?如果您的目标,未来十年的CPU的是什么?你甚至不知道他们会如何工作的,所以你不能承担太多关于他们。如果你的目标虚拟机是什么?编译器已经存在,生成字节code为Flash,准备从网站上运行。如果你想编译C ++到Python源$ C ​​$ C是什么?

Finally, as I mentioned in a comment to your question, keep in mind that C++ is just a language. It doesn't care which architecture it is compiled to. Many of these limitations may seem obscure on modern CPU's. But what if you're targeting yesteryear's CPU's? What if you're targeting the next decade's CPU's? You don't even know how they'll work, so you can't assume much about them. What if you're targeting a virtual machine? Compilers already exist which generate bytecode for Flash, ready to run from a website. What if you want to compile your C++ to Python source code?

在标准保证指定的规则,你的code将工作中的所有的这些案件中停留。

Staying within the rules specified in the standard guarantees that your code will work in all these cases.

这篇关于用C指针实现细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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