简体   繁体   English

是内存问题吗? c ++ [代码块] [Windows]

[英]Is the a memory issue? c++ [code blocks][Windows]

My code blocks of able to compile the code below and worked well: 我的代码块能够编译以下代码,并且运行良好:

unsigned long int a=100000000000;

My code blocks of able to compile the code below But my program crashed immediately after i enter the same number(100000000000) : 我的代码块能够编译下面的代码, 但是当我输入相同的数字(100000000000)后,我的程序立即崩溃

unsigned long int a;
 cin>>a;

how to fix? 怎么修? Why the fist one is working !why not the second case? 为什么第一个起作用?第二个为什么不起作用?

if i entered a number more than max unsigned long int size! 如果我输入的数字大于最大无符号long int大小! then why did the first work? 那为什么要先做呢?

There is no reason for crash here. 这里没有崩溃的原因。

The initialization causes a warning on my compiler because of truncation of an unsigned long long value, and the read attempt simply result in an failed read and does not change the value of the variable. 初始化会由于无符号的long long值被截断而在编译器上发出警告,并且读取尝试只会导致读取失败,并且不会更改变量的值。

Code demonstrating it (on a 32 bit machine): 演示它的代码(在32位计算机上):

#include <iostream>
#include <iomanip>

using std::cin;
using std::cout;
using std::endl;

int main() {
    unsigned long int b=100000000000; // warning here

    unsigned long int a = 0;
    cin>>a;

    cout << "b:" << b << "(" << std::hex << b << ")" << " a:" << std::dec << a << endl;

    return 0;
}

The output is as expected 输出符合预期

b:1215752192(4876e800) a:0

because as an ULL on my architecture 100000000000 is 0x174876E800 and the high order bytes have been truncated... 因为作为我的体系结构100000000000上的ULL是0x174876E800并且高阶字节已被截断...

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

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