您的位置:首页 > 其它

你就可以在这项计划中

2016-03-19 20:28 393 查看
// #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ài在zhè这zhǒng种qíng情kuàng况xià下
,jì计suàn算jī机huì会jiāng将yī一xiē些xián闲zhì置de的nèi内cún存
,tā它jiāng将bǎ把jià价zhí值jū居zhù住zài在gāi该nèi内cún存wèi位zhi置de的fǎ法tíng庭
,tā它jiāng将dǎ打yìn印zhí值
。dàn但tā它huì会dǎ打yìn印shén什me么jià价zhí值
?dá答àn案shì是
“shuí谁zhī知dao道

“měi每cì次yùn运x
17278
íng行gāi该chéng程xù序shí时
,dá答àn案dōu都huì会gǎi改biàn变
。dāng当zuò作zhě者rǎn冉běn本chéng程xù序yòng用
V
i
s
u
a
l
S
t
u
d
i
o
2
0
1
3biān编yì译

s
t
d
:

c
o
u
tyìn印zhì制jià价zhí值
7
1
7
7
7
2
8yī一cì次
,hé和
5
2
7
7
5
9
2xià下

A couple of notes if you want to run this program yourself:
如果你想自己运行这个程序的话,你就可以在这项计划中:
rú如guǒ果nǐ你xiǎng想zì自jǐ己yùn运xíng行zhè这ge个chéng程xù序de的huà话
,nǐ你jiù就kě可yǐ以zài在zhè这xiàng项jì计huà划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ǎo保nǐ你shǐ使yòng用fā发bù布bǎn版běn本de的pèi配zhì置
(jiàn见dì第
0
.
6
A
,shēng生chéng成pèi配zhì置xìn信xī息rú如hé何zuò做

。fǒu否zé则
,shàng上shù述chéng程xù序kě可yǐ以dǎ打yìn印rèn任hé何jià价zhí值
,nǐ你de的biān编yì译qì器chū初shǐ始huà化nèi内cú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为未初始化的变量,来绕过这个问题可能的解决方案是在评论部分指出。
rú如guǒ果nǐ你de的biān编yì译qì器bù不huì会ràng让nǐ你yùn运xíng行zhè这ge个chéng程xù序
,yīn因wèi为tā它biāo标zhì志biàn变liàng量
Xwéi为wèi未chū初shǐ始huà化de的biàn变liàng量
,lái来rào绕guò过zhè这ge个wèn问tí题kě可néng能de的jiě解jué决fāng方àn案shì是zài在píng评lùn论bù部fen分zhǐ指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òng用wèi未chū初shǐ始huà化de的biàn变liàng量shì是yī一gè个xīn新shǒu手chéng程xù序yuán员
,zuì最cháng常jiàn见de的cuò错wù误
,bù不xìng幸de的shì是
,tā它yě也kě可yǐ以shì是yī一gè个zuì最jù具tiǎo挑zhàn战xìng性de的tiáo调shì试
(yīn因wèi为chéng程xù序kě可néng能yùn运xíng行hǎo好rú如guǒ果wèi未chū初shǐ始huà化de的zhí值fā发shēng生dào到yī一diǎn点jì记yì忆zhōng中
,yǒu有yī一gè个hé合lǐ理de的zhí值
,rú如
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: