C++将分配值8到变量
来源:互联网 发布:2017年中国网络购物 编辑:程序博客网 时间:2024/05/14 18:53
一份声明,如×= 5;似乎是明显的。正如你所猜测的,我们将分配5到X的值,但是确切的是什么?是一个变数。
A variable in C++ is a name for a piece of memory that can be used to store information. You can think of a variable as a mailbox, or a cubbyhole, where we can put and retrieve information. All computers have memory, called RAM (random access memory), that is available for programs to use. When a variable is defined, a piece of that memory is set aside for the variable.
C++中的变量是一块内存可以用来存储信息的一个名字。你可以把一个变量作为一个邮箱,或一间小屋里,我们可以把和检索信息。所有的计算机都有一种叫做随机存取记忆体的记忆体,可供程式使用。当定义一个变量时,一块内存被放置在变量中。
In this section, we are only going to consider integer variables. An integer is a whole number, such as 1, 2, 3, -1, -12, or 16. An integer variable is a variable that holds an integer value.
在这一节中,我们只考虑整型变量。整数是整数,如1、2、3、12、16或1。整型变量是一个变量,拥有一个整型值。
In order to define a variable, we generally use a declaration statement. Here’s an example of defining variable x as an integer variable (one that can hold integer values):
为了定义一个变量,我们通常使用声明语句。这里的一个例子,定义变量的×作为一个整数变量(一个可以容纳整型值):
int y; // define y as an integer variabley = 4; // 4 evaluates to 4, which is then assigned to yy = 2 + 5; // 2 + 5 evaluates to 7, which is then assigned to y int x; // define x as an integer variablex = y; // y evaluates to 7 (from before), which is then assigned to x.x = x; // x evaluates to 7, which is then assigned to x (useless!)x = x + 1; // x + 1 evaluates to 8, which is then assigned to x.
这使它明显,C++将分配值8到变量x。
Programmers don’t tend to talk about l-values or r-values much, so it’s not that important to remember the terms. The key takeaway is that on the left side of the assignment, you must have something that represents a memory address (such as a variable). Everything on the right side of the assignment will be evaluated to produce a value.
程序员不倾向于谈论左值或右值的多,因此它不是要记住的重要的。关键的外卖是,在左边的任务,你必须有一个代表一个内存地址(如一个变量)。在赋值的右边的每一个都将被赋值来生成一个值。
Initialization vs assignment
初始化与赋值
C++ supports two related concepts that new programmers often get mixed up: assignment and initialization.
C++支持两个相关的概念,新的程序员经常搞混了:分配和初始化。
After a variable is defined, a value may be assigned to it via the assignment operator (the = sign):
在一个变量被定义后,一个值可以通过赋值操作符(= =符号)分配给它:
一个变量只能在定义时被初始化。
Although these two concepts are similar in nature, and can often be used to achieve similar ends, we’ll see cases in future lessons where some types of variables require an initialization value, or disallow assignment. For these reasons, it’s useful to make the distinction now.
虽然这两个概念在本质上是相似的,并且通常可以用来实现类似的目的,我们将在本课的一些类型需要一个初始化变量值的情况,或不允许转让。因为这些原因,现在是有用的区别了。
Uninitialized variables
未初始化变量
Unlike some programming languages, C/C++ does not initialize variables to a given value (such as zero) automatically (for performance reasons). Thus when a variable is assigned to a memory location by the compiler, the default value of that variable is whatever garbage happens to already be in that memory location! A variable that has not been assigned a value is called an uninitialized variable.
不像一些编程语言,C / C++不初始化变量的给定值(如零)自动(出于性能原因)。因此,当一个变量被编译器分配给一个内存位置时,该变量的默认值是,无论发生了什么,都会发生在内存位置!一个变量没有被赋值为一个未初始化的变量。
Note: Some compilers, such as Visual Studio, will initialize the contents of memory when you’re using a debug build configuration. This will not happen when using a release build configuration.
注意:一些编译器,如可视化工作室,将在使用调试构建配置时初始化内存的内容。使用发布版本配置时不会发生这种情况。
Uninitialized variables can lead to interesting (and by interesting, we mean unexpected) results. Consider the following short program:
未初始化的变量可能会导致一些有趣的(和有趣的,我们的意思是意想不到的)结果。考虑以下短程序:
- C++将分配值8到变量
- C将变量映射到内存
- C --- 变量内存分配
- C环境将变量定义到RAM绝对地址之上
- C变量声明内存分配
- C语言变量内存分配
- 不能将 类型的值 分配到 类型的实体
- 将分解的C++RPG引擎分配到VC++6.0 DirectX9.0 AppWizard项目中
- 如何分配变量到指定的地址
- 如何分配变量到指定的地址
- Java应用程序将对象分配到堆
- C变量定义内存分配机制
- C语言变量声明内存分配
- C语言变量内存地址的分配
- c/c++语言变量声明内存分配
- C语言变量声明内存分配
- C语言变量声明内存分配
- c/c++语言变量声明内存分配
- VTK环境搭建注意事项
- 第四周项目4猜数字游戏
- map/list/set区别
- 进程之间的8种通信方式
- OpenCV 2.2.0及以上版本CvvImage的使用
- C++将分配值8到变量
- [网络流24题][COGS396]魔术球问题简化版(最小割)
- Android四大组件之Service工作原理
- 点击Button事件的四种写法
- 自己写的一个带indicator的viewpager
- Linux 各目录及每个目录的详细介绍
- 【BZOJ2456】mode,卡内存
- mysql数据库基础--数据库操作
- 判断单链表是否有环