c++ - 变量的赋值和初始化有什么区别
高洛峰
高洛峰 2017-04-17 14:23:05
0
6
689

可以的话自动变量和静态变量分开讨论

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(6)
迷茫

Variable initialization is to define an initial value for a variable, which means that when the program is loaded into memory, the value stored at the address of this variable has already been determined.
The assignment is embodied as an instruction in the program. After the program is loaded into the memory, the address of an uninitialized variable still stores the previous content and will not be cleared. The assignment is an instruction that writes the value into the memory. instructions.
Static variables are global and are written in the global area of ​​​​the executable file. As the executable file is loaded into the memory, the value of the static variable is determined. Uninitialized static variables may be initialized to 0.
Local variables are generally located in stack space and heap space. This part is allocated by the system after being loaded into memory. The system will not clean up for you, so if you do not initialize it, the previous value will be in the memory. .

Peter_Zhu

Assignment is to give a value to a variable
The initialized value is not necessarily the value you need.

Ty80

Const variables can only be assigned using initialization expressions
The initialization expression of an object calls its copy constructor, and the assignment statement calls its equal sign operator.

迷茫

Initialization is the initialization of variables such as $name='Zhang San'. The most original value of the variable is the first assignment.
Assignment means that the program must give $name='Li Si' again during operation. Assignment, no matter what value $name is during initialization

大家讲道理

The initialization of variables is to allocate memory space to the variable and give it a specific value when defining the variable. Static variables must be initialized, while automatic variable initialization is optional.
The assignment of variables is to modify the value of a defined variable. Automatic variables are assignable, while static variables cannot be assigned. The value of a static variable is determined when it is defined. If an automatic variable is not initialized when it is defined, and If it is not assigned a value afterwards, it cannot be used.

黄舟

The first time you give a value to a variable is initialization, and the second, third... times it is assignment. There is initialization first, then assignment. The difference between statically typed and automatically typed variables is that the storage space of the variables is different. Automatic variables are generally stored on the stack. If they are statically typed, they are stored in the global/static variable area.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template