你就可以在这项计划中

来源:互联网 发布:基于java的oa审批流程 编辑:程序博客网 时间:2024/04/30 12:08
// #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únjiāngjiàzhízhùzàigāinèicúnwèizhidetíngjiāngyìnzhídànhuìyìnshénmejiàzhíànshìshuízhīdaoměiyùnxínggāichéngshíàndōuhuìgǎibiàndāngzuòzhěrǎnběnchéngyòngVisualStudio2013biānstd:coutyìnzhìjiàzhí71777285277592xià

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àn0.6AshēngchéngpèizhìxìnzuòfǒushàngshùchéngyìnrènjiàzhídebiānchūshǐhuànèicúnVisualStudioshǐ使yòng-858993460

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éngyīnwèibiāozhìbiànliàngXwéiwèichūshǐhuàdebiànliànglá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ánzuì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ōngyǒ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表示产生了以下警告:

1234567891011121314int 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 valuesstd::cout << x + y << std::endl; // #3 std::cout << x << std::endl; // #4 int z;std::cout << z << std::endl; // #5


0 0
原创粉丝点击