Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22037

bytes value of 8 bytes or 16 bytes number

$
0
0

I assume every byte should be valued 0 if a long var is initialized to 0. (same for __int128)

This is a piece of testing code:

    int64_t i64 = { 0 };
    auto addr = &i64;
    LOG(INFO) << "i64 address="<< addr << ": "<< i64;
    for (int i = 0; i < sizeof(i64); i++) {
      LOG(INFO) << addr + i << ": byte="<< (int)*reinterpret_cast<int8_t*>(addr + i);
    }

I expect to see all 0 for each byte. However, it is not the case, here is result by running on "Mac Mojave 10.14.6" with clang

Apple clang version 11.0.0 (clang-1100.0.33.12) Target: x86_64-apple-darwin18.7.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I1113 23:26:05.741184 103323072 TestCommon.cpp:498] i64 address=0x7ffeeb589a90: 0
I1113 23:26:05.741191 103323072 TestCommon.cpp:500] 0x7ffeeb589a90: byte=0
I1113 23:26:05.741196 103323072 TestCommon.cpp:500] 0x7ffeeb589a98: byte=0
I1113 23:26:05.741201 103323072 TestCommon.cpp:500] 0x7ffeeb589aa0: byte=0
I1113 23:26:05.741205 103323072 TestCommon.cpp:500] 0x7ffeeb589aa8: byte=1
I1113 23:26:05.741210 103323072 TestCommon.cpp:500] 0x7ffeeb589ab0: byte=0
I1113 23:26:05.741214 103323072 TestCommon.cpp:500] 0x7ffeeb589ab8: byte=-126
I1113 23:26:05.741219 103323072 TestCommon.cpp:500] 0x7ffeeb589ac0: byte=96
I1113 23:26:05.741223 103323072 TestCommon.cpp:500] 0x7ffeeb589ac8: byte=1

What might be reasons for this? Is it related to memory order or CPU cache sync? Or these are legal bytes value for a 0 long?


However, if I use a union to wrap it, I could see all bytes are valued in zero:

  union U {
    int128_t a;
    int8_t b[16];
  };

  U u = { 0 };
  LOG(INFO) << "value="<< *reinterpret_cast<int64_t*>(u.b + 8);
  for (int i = 0; i < 16; i++) {
    LOG(INFO) << "byte="<< (int)u.b[i];
  }

output: I1113 23:26:05.757899 103323072 TestCommon.cpp:539] value=0
I1113 23:26:05.757905 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757910 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757915 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757920 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757925 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757928 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757932 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757936 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757942 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757946 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757951 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757956 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757959 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757966 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757970 103323072 TestCommon.cpp:541] byte=0
I1113 23:26:05.757975 103323072 TestCommon.cpp:541] byte=0

Appreciate any insights and input...


Viewing all articles
Browse latest Browse all 22037

Trending Articles