Home  >  Q&A  >  body text

c++ - 变量的赋值和初始化有什么区别

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

高洛峰高洛峰2715 days ago647

reply all(6)I'll reply

  • 迷茫

    迷茫2017-04-17 14:25:05

    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. .

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:25:05

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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:25:05

    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.

    reply
    0
  • 迷茫

    迷茫2017-04-17 14:25:05

    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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 14:25:05

    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.

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:25:05

    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.

    reply
    0
  • Cancelreply