Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么用C++;打印意外结果? 我知道C++中的基础知识,或者我认为我知道。 昨天,我尝试将8元素的uint_8类型数组转换为2元素的uint_32类型数组_C++_Pointers - Fatal编程技术网

为什么用C++;打印意外结果? 我知道C++中的基础知识,或者我认为我知道。 昨天,我尝试将8元素的uint_8类型数组转换为2元素的uint_32类型数组

为什么用C++;打印意外结果? 我知道C++中的基础知识,或者我认为我知道。 昨天,我尝试将8元素的uint_8类型数组转换为2元素的uint_32类型数组,c++,pointers,C++,Pointers,我将我的值转换为32位格式,当我试图将它们显示为32位格式时,计算机会给我它们的地址,而不是它们的值。。您可以从注释部分看到我对这段代码感到困惑的地方 int main() { uint8_t info[8]; info[0] = '2'; info[1] = '0'; info[2] = '2'; info[3] = '0'; info[4] = '0'; info[5] = '0'; info[6] = '0'; info[7] = '0'

我将我的值转换为32位格式,当我试图将它们显示为32位格式时,计算机会给我它们的地址,而不是它们的值。。您可以从注释部分看到我对这段代码感到困惑的地方

int main()
{
  uint8_t info[8];
   info[0] = '2';
   info[1] = '0';
   info[2] = '2';
   info[3] = '0';
   info[4] = '0';
   info[5] = '0';
   info[6] = '0';
   info[7] = '0';
  uint32_t *divided = (uint32_t*)&info[0];
  uint32_t *dividedTwo = (uint32_t*)&info[4];
  std::cout << "Address of info " << &info << std::endl; //Output 0x7ffdd25cabe0 as expected.
  std::cout << "Expected value of info " << (uint8_t*)info<< std::endl; //Output 20200000 as expected.
  std::cout << "Expected value of divided " << (uint32_t*)divided << std::endl; //Output 0x7ffdd25cabe0 not as my expected. What is the reason?
  std::cout << "But why this return the true value? " << (uint8_t*)divided << std::endl; //Output 20200000 but why 8bit returns the true value instead of 32bit casting?
  std::cout << "Same here, my value was type of 32... " << (uint8_t*)dividedTwo << std::endl; //Output 0000

}
intmain()
{
uint8_t信息[8];
信息[0]=“2”;
信息[1]=“0”;
信息[2]=“2”;
信息[3]=“0”;
信息[4]=“0”;
信息[5]=“0”;
信息[6]=“0”;
信息[7]=“0”;
uint32_t*分割=(uint32_t*)和信息[0];
uint32_t*划分为两个=(uint32_t*)和信息[4];
标准::cout
这两者都是相同的。尝试打印以非null结尾的字符串会导致未定义的行为


这两者都是相同的。尝试打印以非null结尾的字符串会导致未定义的行为



许多打印语句会导致未定义的行为或没有意义

  • 请注意,数组中填充的是
    字符
    文字(例如
    '2'
    ),而不是整数文字-您可能需要修复此问题以使输出更清晰


    注意这种类型的铸造可能出现的对齐问题-严格来说,这是不合法的。

    您的许多打印语句会导致未定义的行为或没有意义

  • 请注意,数组中填充的是
    字符
    文字(例如
    '2'
    ),而不是整数文字-您可能需要修复此问题以使输出更清晰


    注意这种类型的强制转换可能存在的对齐问题-它(严格地说)不合法。

    @CarlNorum支持将其重新解释为char。不支持其他方式。在这种情况下,有没有更好的解决方案将8元素uint_8类型数组转换为2元素uint_32类型数组,而不是“移位(>等)”呢?因为我不想移动,我必须使用这种“指针”和“投射”@TürkerBerkeYıldırım我添加了一种不移位的方式来进行转换。@CarlNorum支持重新解释为char。不是其他方式。在这种情况下,有没有更好的解决方案来将8元素uint_8类型数组转换为2元素uint_32类型数组,而不是“移位(>等)”呢?因为我不想移动,所以我必须使用这种“指针”和“施法”。@türkerBerkeYıldırım我添加了一种不移动的方式来进行转换。你对“期望值除以”的期望值是什么?我期望值是2020。从逻辑上讲,我无法解释在(uin8_t*)施法后返回“2020”。你期望值是什么“分割的预期值”?我预期值为2020。在逻辑上,我无法解释在(uin8\u t*)铸造后返回“2020”。您对
    *(uint64\u t*)的建议info
    *divided
    *dividedTwo
    都是未定义的行为,因为存在严格的别名冲突。这与您提到的对齐问题是一个单独的问题。抱歉,回答太晚,我也遇到了这个问题,对于不完全理解逻辑的朋友,我在这里留下了一个链接,详细解释这个问题。您对
    *(uint64\u t*)的建议info
    *divided
    *dividedTwo
    都是未定义的行为,因为存在严格的别名冲突。这与您提到的对齐问题是一个单独的问题。抱歉,回答太晚,我也遇到了这个问题,对于不完全理解逻辑的朋友,我在这里留下了一个链接,详细解释这个问题。
    std::cout << "Expected value of info " << (uint8_t*)info<< std::endl; //Output 20200000 as expected.
    
    std::cout << "But why this return the true value? " << (uint8_t*)divided << std::endl; //Output 20200000 but why 8bit returns the true value instead of 32bit casting?
    std::cout << "Same here, my value was type of 32... " << (uint8_t*)dividedTwo << std::endl; //Output 0000
    
    std::cout << "Expected value of divided " << (uint32_t*)divided << std::endl; //Output 0x7ffdd25cabe0 not as my expected. What is the reason?
    
    uint8_t  info8 [8] = ...;
    uint32_t info32[sizeof info8 / sizeof(uint32_t)];
    std::memcpy(info32, info8, sizeof info32);
    
    std::cout << "Address of info " << &info << std::endl; //Output 0x7ffdd25cabe0 as expected.
    
    std::cout << "Expected value of info " << (uint8_t*)info<< std::endl; //Output 20200000 as expected.
    
    std::cout << "Expected value of divided " << (uint32_t*)divided << std::endl; //Output 0x7ffdd25cabe0 not as my expected. What is the reason?
    
    std::cout << "But why this return the true value? " << (uint8_t*)divided << std::endl; //Output 20200000 but why 8bit returns the true value instead of 32bit casting?
    
    std::cout << "Same here, my value was type of 32... " << (uint8_t*)dividedTwo << std::endl; //Output 0000
    
    #include <inttypes.h>
    
    printf("%p\n", (void *)info); // address of the array
    printf("%" PRIx64 "\n", *(uint64_t *)info); // entire 64-bit value
    printf("%" PRIx32 "\n", *divided); // first 32 bits of the 64-bit array
    printf("%" PRIx32 "\n", *dividedTwo); // second 32 bits of the 64-bit array