简体   繁体   English

指针地址如何在c ++中工作

[英]how does pointer adressing work in c++

I am confused about pointer pointing to address of variable 我对指向变量地址的指针很困惑 图片

it points to last two bytes how does this work 它指向最后两个字节这是如何工作的

#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    short *j  = (short*)&i;
    cout << *j << endl;
}

.

A pointer normally holds the address of the beginning of the referred-to item. 指针通常保存引用项的开头的地址。

From the sound of things, you're apparently using a little-endian system [edit: which is no surprise -- just for example, current (Intel) Macs and all Windows machines are little-endian], which means the least significant byte of your 4-byte int comes first in memory instead of last: 从事物的声音来看,你显然正在使用一个小端系统[编辑:这并不奇怪 - 例如,当前(英特尔)Mac和所有Windows机器都是小端],这意味着最不重要的字节您的4字节int 首先在内存中而不是在内存中:

0000001 00000000 00000000 00000000 0000001 00000000 00000000 00000000

When you use a pointer to short to look at the first two bytes, you get: 当你使用short指针查看前两个字节时,你得到:

0000001 00000000 0000001 00000000

which is exactly how it expects to see a value of 1 represented as a two-byte number, so that's what you get. 这正是它期望看到值1表示为两个字节的数字,所以这就是你得到的。

As implied by the name "little-endian" there are also big-endian systems, where the data would be laid-out as you've illustrated above. 正如名称“little-endian”所暗示的那样,还有big-endian系统,其数据将按照您上面的说明进行布局。 On such a machine, you'd probably get the results you expected. 在这样的机器上,你可能会得到你期望的结果。 Just to be complete, there are also a few systems that use rather strange arrangements that might run something like byte1 byte0 byte3 byte2. 为了完成,还有一些系统使用相当奇怪的安排,可能运行像byte1 byte0 byte3 byte2。

That depends on the architecture. 这取决于架构。 And the specific implementation should be completely irrelevant (if it isn't you are doing something wrong). 具体的实现应该是完全不相关的(如果不是你做错了什么)。

This has to do with byte order. 这与字节顺序有关。 Your architecture stores an int 1 as: 您的架构将int 1存储为:

00000001 00000000 00000000 00000000
^^^^^^^^
  this is the address that is stored in the pointer.
|------- -------- -------- --------| int (4 bytes)
|------- -------|                    short (2 bytes)

which, truncated to a short of 2 bytes still is a 1. 截断到2个字节的short数仍然是1。

Btw. 顺便说一句。 try printing all bytes on their own: 尝试自己打印所有字节:

unsigned char* p = (unsigned char*)&i;
for (unsigned j = 0; j < sizeof(int); j++) {
  std::cout << p[j] << " ";
}
std::cout << std::endl;

It should print 1 0 0 0 , and not 0 0 0 1 as suggested in your question. 它应按照您的问题中的建议打印1 0 0 0 ,而不是 0 0 0 1

It depends on the endianness of your system (x86 is typically little-endian). 这取决于系统的字节顺序(x86通常是little-endian)。

In big endian, the value of i will be stored with the most significant byte first : 在big endian中,i的值将首先存储在最重要的字节中:

00000000 00000000 00000000 00000001

In little endian, the value of i will be stored with the least significant byte first : 在little endian中,i的值将首先存储在最低有效字节中:

00000001 00000000 00000000 00000000

In any case, the pointer will point to the beginning of the number. 在任何情况下,指针都将指向数字的开头。 However, since your system is little-endian, casting that pointer to short and dereferencing to get a short value, you will get the first two bytes of the little endian value 但是,由于您的系统是little-endian,将该指针强制转换为short并取消引用以获得short值,您将获得little endian值的前两个字节

00000001 00000000

In little endian, this is the short value for 1, which is the result you get. 在小端,这是1的短值,这是你得到的结果。 If you ran the exact same code on a big-endian system, you would then get a value of 0. 如果您在big-endian系统上运行完全相同的代码,那么您将获得值0。

It doesn't point to bytes; 它没有指向字节; it points to an entity of type int . 它指向int类型的实体。 How an int is represented in memory depends on the implementation. 如何在内存中表示int取决于实现。 And if i is an int , then *((short*)&i) is undefined behavior; 如果i是一个int ,那么*((short*)&i)是未定义的行为; you could get anything. 你可以得到任何东西。 In practice, on common platforms, you can access things like this; 实际上,在通用平台上,您可以访问这样的东西; what you get still depends on the platform, and such manipulations are only useful for very low level, platform specific operations. 你得到的仍然取决于平台,这种操作只对非常低级别的平台特定操作有用。 (On an Intel, you'll get the results the others have posted. For other platforms and other values, it depends.) (在英特尔,您将获得其他人发布的结果。对于其他平台和其他值,它取决于。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM