本文介绍了以下语句是否将物理地址或虚拟地址分配给指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

来自 https://stackoverflow.com/a/2761494/156458

uintptr_t address = 0;
void *p = (void *) address;

请注意,这与这样做不同

Note, that this is not the same as doing

void *p = 0;

后者总是产生空指针值,而前者在 一般情况没有.前者通常会产生一个指向 物理地址0,它可能是也可能不是空指针值 在给定的平台上.

The latter always produces the null-pointer value, while the former in general case does not. The former will normally produce a pointer to physical address 0, which might or might not be the null-pointer value on the given platform.

我很惊讶地发现,void *p = 0没有分配物理地址或虚拟地址0,而是将void的空指针分配给了指针.

I am surprised to find it out that void *p = 0 doesn't assign either physical or virtual address 0 but a null pointer of void to the pointer.

引号还说,显式整数到指针的转换"可以为指针分配地址.

The quote also says that the "explicit integer-to-pointer conversion" can assign an address to a pointer.

问题:

    void *p = 0中的
  1. ,是否存在从0void*的隐式转换?

  1. in void *p = 0, is there implicit conversion from 0 to void*?

隐式转换是否与显式转换(void *)0相同,即void *p = 0void *p = (void*) 0相同吗?

Is the implicit conversion the same as explicit conversion (void *)0, i.e. is void *p = 0 the same as void *p = (void*) 0?

void *p = (void*) 0是否产生一个指向物理或虚拟地址0的指针还是void的空指针?

Does void *p = (void*) 0 produce a pointer to either physical or virtual address 0or a null pointer of void?

如果我使用非零数字,例如void *p = 123,是否存在隐式从123转换为void *?

If I use a nonzero number, e.g. void *p = 123, is there implicitconversion from 123 to void *?

隐式转换与显式转换(void *) 123相同吗?

Is the implicit conversion the same as explicit conversion (void *) 123?

void *p = 123void *p = (void *)123设置为p a指向物理地址还是虚拟地址123的指针?

Will either void *p = 123 or void *p = (void *)123 make p apointer to either physical or virtual address 123?

如果void *p = (void *)123无法生成指向物理地址或虚拟地址123的指针,int addr = 123; void *p = (void *)addr;可以吗?我通过在引号的第一个示例中将unitptr_t替换为int来创建它.

If void *p = (void *)123 can't generate a pointer to either physical or virtual address 123, can int addr = 123; void *p = (void *)addr;? I create it by replacing unitptr_t with int in the first example in the quote.

谢谢.

推荐答案

如果你说

char *p = 0x12345;

您可能会分配p指向地址0x12345.无论是虚拟地址还是物理地址,我们都不能说.这取决于您的计算机及其配置. (但是,如果您的计算机使用虚拟内存,并且正在编写普通的用户程序,那么它肯定是虚拟地址.)

you will probably assign p to point to address 0x12345. Whether that's a virtual or a physical address we can't say; that depends on your machine and how it's configured. (But if your machine uses virtual memory, and if you're writing an ordinary user program, then it's certainly a virtual address.)

在上面我说大概",部分原因是严格来讲,为指针分配整数并不明确.为了安全起见,您应该写

In the above I said "probably", in part because assigning an integer to a pointer is not, strictly speaking, well-defined. So to be on the safe side, you would write

char *p = (char *)0x12345;

这将再次指定p指向地址0x12345.

This will, again, assign p to point to address 0x12345.

但是接下来我们来谈谈特殊情况.如果你写

But then we come to the special cases. If you write

char *p = 0;

char *p = (char *)0;

问题是,您是否已分配p指向地址0?答案可能是在任何传统机器上,但不能保证,因为空指针存在特殊情况.

the question is, have you assigned p to point to address 0? And the answer is, probably, on any conventional machine, but this is not guaranteed, because there's a special case for null pointers.

不是p = 0 不会分配p指向地址0,而是可能.在空指针的内部表示形式不是全零的机器上,赋值p = 0p设置为该空指针值,因此p not 指向地址为0.

It's not that p = 0 will not assign p to point to address 0 -- it's that it might not. On a machine where the internal representation of a null pointer is not all-bits-0, the assignment p = 0 will set p to that null pointer value, and p will therefore not point to address 0.

这篇关于以下语句是否将物理地址或虚拟地址分配给指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 07:10