简体   繁体   English

谁能解释这个C ++代码在做什么?

[英]Can anyone please explain what this C++ code is doing?

char b = 'a';
int *a = (int*)&b;
std::cout << *a;

What could be the content of *a ? *a的内容*a什么? It is showing garbage value. 它显示了垃圾值。 Can you anyone please explain. 谁能解释一下。 Why? 为什么?

Suppose char takes one byte in memory and int takes two bytes (the exact number of bytes depends of the platform, but usually they are not same for char and int). 假设char在内存中占用一个字节,而int占用两个字节(确切的字节数取决于平台,但是char和int通常不相同)。 You set a to point to the memory location same as b . 您将a设置为指向与b相同的存储位置。 In case of b dereferencing will consider only one byte because it's of type char. b情况下,取消引用将仅考虑一个字节,因为它的类型为char。 In case of a dereferencing will access two bytes and thus will print the integer stored at these locations. 在的情况下, a解引用将访问两个字节,因此将打印存储在这些位置中的整数。 That's why you get a garbage: first byte is 'a' , the second is random byte - together they give you a random integer value. 这就是为什么会产生垃圾:第一个字节为'a' ,第二个字节为随机字节-它们一起为您提供了一个随机整数值。

You initialize a variable with the datatype char ... a char in c++ should have 1 Byte and an int should contain 2 Byte. 您可以使用数据类型char初始化变量。c++中的char应该具有1个字节,而int应该包含2个字节。 Your a points to the address of the b variable... an adress should be defined as any hexadecimal number. 您的a指向b变量的地址...地址应定义为任何十六进制数。 Everytime you call this "program" there should be any other hexadecimal number, because the scheduler assigns any other address to your a variable if you start this program new. 每次调用此“程序”时,都应该有其他十六进制数,因为如果您重新启动该程序,则调度程序会将其他任何地址分配给您的变量。

Either the first or the last byte should be hex 61 depending on byte order. 根据字节顺序,第一个或最后一个字节应为十六进制61。 The other three bytes are garbage. 其他三个字节是垃圾。 best to change the int to an unsigned int and change the cout to hex. 最好将int更改为unsigned int并将cout更改为十六进制。

I don't know why anyone would want to do this. 我不知道为什么有人会这样做。

Think of it as byte blocks. 将其视为字节块。 Char has one byte block (8 bits). 字符有一个字节块(8位)。 If you set a conversion (int*) then you get the next 7 byte blocks from the char's address. 如果设置了转换(int *),则将从char地址中获取接下来的7个字节块。 Therefore you get 7 random byte blocks which means you'll get a random integer. 因此,您将获得7个随机字节块,这意味着您将获得一个随机整数。 That's why you get a garbage value. 这就是为什么您获得垃圾值。

The code invokes undefined behavior, garbage is a form of undefined behavior, but your program could also cause a system violation and crash with more consequences. 该代码调用未定义的行为,垃圾是未定义的行为的一种形式,但是您的程序也可能会导致系统冲突并崩溃,从而带来更多后果。

int *a = (int*)&b; initializes a pointer to int with the address of a char . 使用char地址初始化指向int的指针。 Dereferencing this pointer will attempt to read an int from that address: 取消引用此指针将尝试从该地址读取一个int

  • If the address is misaligned and the processor does not support misaligned accesses, you may get a system specific signal or exception. 如果地址未对齐并且处理器不支持未对齐的访问,则可能会收到系统特定的信号或异常。
  • If the address is close enough to the end of a segment that accessing beyond the first byte causes a segment violation, that's what you can get. 如果地址离段的末尾足够近,访问超出第一个字节会导致段冲突,那就是您可以得到的。
  • If the processor can read the sizeof(int) bytes at the address, only one of those will be a , ( 0x61 in ASCII) but the others have undetermined values (aka garbage ). 如果处理器可以读取sizeof(int)在地址字节,只有那些之一将是a ,( 0x61在ASCII),但该有的都有了待定值(又名垃圾 )。 As a matter of fact, on some architectures, reading from uninitialized memory may cause problems: under valgrind for example, this will cause a warning to be displayed to the user. 实际上,在某些体系结构上,从未初始化的内存读取可能会引起问题:例如在valgrind下,这将导致向用户显示警告。

All the above are speculations, undefined behavior means anything can happen. 以上都是推测,未定义的行为表示可能发生任何事情。

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

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