你就可以在这项计划中

// #include "stdafx.h" // Uncomment if Visual Studio user
#include <iostream>
 
int main()
{
    // define an integer variable named x
    int x;
    
    // print the value of x to the screen (dangerous, because x is uninitialized)
    std::cout << x;
 
    return 0;
}

在这种情况下,计算机会将一些闲置的内存,它将把价值居住在该内存位置的法庭,它将打印值。但它会打印什么价值?答案是“谁知道!“每次运行该程序时,答案都会改变。当作者冉本程序用Visual Studio 2013编译,std::cout印制价值7177728一次,和5277592下。

zàizhèzhǒngqíngkuàngxià suànhuìjiāngxiēxiánzhìdenèicún jiāngjiàzhízhùzàigāinèicúnwèizhidetíng jiāngyìnzhí dànhuìyìnshénmejiàzhí ànshì shuízhīdao měiyùnxínggāichéngshí àndōuhuìgǎibiàn dāngzuòzhěrǎnběnchéngyòng V i s u a l S t u d i o 2 0 1 3biān s t d : c o u tyìnzhìjiàzhí 7 1 7 7 7 2 8 5 2 7 7 5 9 2xià

A couple of notes if you want to run this program yourself:

如果你想自己运行这个程序的话,你就可以在这项计划中:

guǒxiǎngyùnxíngzhègechéngdehuà jiùzàizhèxiànghuàzhōng

Make sure you’re using a release build configuration (see section 0.6a -- Build configurations for information on how to do that). Otherwise the above program may print whatever value your compiler is initializing memory with (Visual Studio uses -858993460).

确保你使用发布版本的配置(见第0.6A,生成配置信息如何做)。否则,上述程序可以打印任何价值,你的编译器初始化内存(Visual Studio使用- 858993460)。

quèbǎoshǐ使yòngbǎnběndepèizhì jiàn 0 . 6 A shēngchéngpèizhìxìnzuò fǒu shàngshùchéngyìnrènjiàzhí debiānchūshǐhuànèicún V i s u a l S t u d i oshǐ使yòng - 8 5 8 9 9 3 4 6 0

If your compiler won’t let you run this program because it flags variable x as an uninitialized variable, a possible solution to get around this issue is noted in the comments section.

如果你的编译器不会让你运行这个程序,因为它标志变量X为未初始化的变量,来绕过这个问题可能的解决方案是在评论部分指出。

guǒdebiānhuìràngyùnxíngzhègechéng yīnwèibiāozhìbiànliàng Xwéiwèichūshǐhuàdebiànliàng láiràoguòzhègewènnéngdejiějuéfāngànshìzàipínglùnfenzhǐchū

Using uninitialized variables is one of the most common mistakes that novice programmers make, and unfortunately, it can also be one of the most challenging to debug (because the program may run fine anyway if the uninitialized value happened to get assigned to a spot of memory that had a reasonable value in it, like 0).

使用未初始化的变量是一个新手程序员,最常见的错误,不幸的是,它也可以是一个最具挑战性的调试(因为程序可能运行好如果未初始化的值发生到一点记忆中,有一个合理的值,如0)。

shǐ使yòngwèichūshǐhuàdebiànliàngshìxīnshǒuchéngyuán zuìchángjiàndecuò xìngdeshì shìzuìtiǎozhànxìngdetiáoshì yīnwèichéngnéngyùnxínghǎoguǒwèichūshǐhuàdezhíshēngdàodiǎnzhōng yǒudezhí 0

Fortunately, most modern compilers will print warnings at compile-time if they can detect a variable that is used without being initialized. For example, compiling the above program on Visual Studio 2005 express produced the following warning:

幸运的是,大多数现代编译器都会在编译时发出警告,如果它们能够检测到不被初始化的变量。例如,编制上述程序对视觉工作室2005表示产生了以下警告:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int x = 5;
x = x - 2;
std::cout << x << std::endl; // #1
 
int y = x;
std::cout << y << std::endl; // #2
 
// x + y is an r-value in this context, so evaluate their values
std::cout << x + y << std::endl; // #3
 
std::cout << x << std::endl; // #4
 
int z;
std::cout << z << std::endl; // #5


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值