簡體   English   中英

帶有布爾轉換的變量的原始打印:意外行為

[英]raw print of variable with bool casting: unexpected behaviour

我試圖將int作為原始數據打印,只是出於好奇,通過將變量指針轉換為布爾值並增加指針“ sizeof(int)* 8”的次數,但是我沒有得到預期的行為。 你能解釋一下為什么嗎? 我想念什么?

這是我的代碼:

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int integer = 0;
    int *puntInt = 0;
    bool *puntBool = 0;
    puntInt = &integer;
    for (int j = 0; j < 100; j++)
    {
        integer = j;
        puntBool = (bool*) puntInt;
        printf("%02d => ", integer);
        for(unsigned int i = 0; i < (sizeof(int) * 8); i++)
        {
            printf("%d", (*puntBool ? 1 : 0));
            puntBool++;
        }
        printf("\n");
    }
   getchar();
   return 0;
}

這是我的輸出:

00 => 00000000100011111100111111000000
01 => 10001000100011111100111111000000
02 => 10001000100011111100111111000000
03 => 10001000100011111100111111000000
04 => 10001000100011111100111111000000
05 => 10001000100011111100111111000000
06 => 10001000100011111100111111000000
07 => 10001000100011111100111111000000
08 => 10001000100011111100111111000000
09 => 10001000100011111100111111000000
10 => 10001000100011111100111111000000
11 => 10001000100011111100111111000000
12 => 10001000100011111100111111000000
13 => 10001000100011111100111111000000
14 => 10001000100011111100111111000000
15 => 10001000100011111100111111000000
16 => 10001000100011111100111111000000
17 => 10001000100011111100111111000000
18 => 10001000100011111100111111000000
19 => 10001000100011111100111111000000
20 => 10001000100011111100111111000000
21 => 10001000100011111100111111000000
22 => 10001000100011111100111111000000
23 => 10001000100011111100111111000000
24 => 10001000100011111100111111000000
25 => 10001000100011111100111111000000
26 => 10001000100011111100111111000000
27 => 10001000100011111100111111000000
28 => 10001000100011111100111111000000
29 => 10001000100011111100111111000000
30 => 10001000100011111100111111000000
31 => 10001000100011111100111111000000
32 => 10001000100011111100111111000000
33 => 10001000100011111100111111000000
34 => 10001000100011111100111111000000
35 => 10001000100011111100111111000000
36 => 10001000100011111100111111000000
37 => 10001000100011111100111111000000
38 => 10001000100011111100111111000000
39 => 10001000100011111100111111000000
40 => 10001000100011111100111111000000
41 => 10001000100011111100111111000000
42 => 10001000100011111100111111000000
43 => 10001000100011111100111111000000
44 => 10001000100011111100111111000000
45 => 10001000100011111100111111000000
46 => 10001000100011111100111111000000
47 => 10001000100011111100111111000000
48 => 10001000100011111100111111000000
49 => 10001000100011111100111111000000
50 => 10001000100011111100111111000000
51 => 10001000100011111100111111000000
52 => 10001000100011111100111111000000
53 => 10001000100011111100111111000000
54 => 10001000100011111100111111000000
55 => 10001000100011111100111111000000
56 => 10001000100011111100111111000000
57 => 10001000100011111100111111000000
58 => 10001000100011111100111111000000
59 => 10001000100011111100111111000000
60 => 10001000100011111100111111000000
61 => 10001000100011111100111111000000
62 => 10001000100011111100111111000000
63 => 10001000100011111100111111000000
64 => 10001000100011111100111111000000
65 => 10001000100011111100111111000000
66 => 10001000100011111100111111000000
67 => 10001000100011111100111111000000
68 => 10001000100011111100111111000000
69 => 10001000100011111100111111000000
70 => 10001000100011111100111111000000
71 => 10001000100011111100111111000000
72 => 10001000100011111100111111000000
73 => 10001000100011111100111111000000
74 => 10001000100011111100111111000000
75 => 10001000100011111100111111000000
76 => 10001000100011111100111111000000
77 => 10001000100011111100111111000000
78 => 10001000100011111100111111000000
79 => 10001000100011111100111111000000
80 => 10001000100011111100111111000000
81 => 10001000100011111100111111000000
82 => 10001000100011111100111111000000
83 => 10001000100011111100111111000000
84 => 10001000100011111100111111000000
85 => 10001000100011111100111111000000
86 => 10001000100011111100111111000000
87 => 10001000100011111100111111000000
88 => 10001000100011111100111111000000
89 => 10001000100011111100111111000000
90 => 10001000100011111100111111000000
91 => 10001000100011111100111111000000
92 => 10001000100011111100111111000000
93 => 10001000100011111100111111000000
94 => 10001000100011111100111111000000
95 => 10001000100011111100111111000000
96 => 10001000100011111100111111000000
97 => 10001000100011111100111111000000
98 => 10001000100011111100111111000000
99 => 10001000100011111100111111000000

bool不僅僅是1位類型。 它的大小是1個字節。

字符是最小的可尋址單元,您不能低於C / C ++中的任何一個。
打印puntBool擁有的價值:

for(unsigned int i = 0; i < (sizeof(int) * 8); i++)
{
        //printf("%d", (*puntBool ? 1 : 0));
        printf("%p\n",puntBool++);

}

給我:

0xbfbb7d80
0xbfbb7d81
0xbfbb7d82
0xbfbb7d83
0xbfbb7d84
0xbfbb7d85
0xbfbb7d86
0xbfbb7d87
0xbfbb7d88
0xbfbb7d89
0xbfbb7d8a
0xbfbb7d8b

因此,您bool*本質上是充當char* ,這就是為什么您的結果不符合預期的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM