Variable Declaration & Memory Allocation

前端 未结 2 1565
轮回少年
轮回少年 2021-01-28 15:41

I want to know whether memory is allocated during the local variable declaration process.

Suppose I write this code inside function, int a =10; memory i

2条回答
  •  耶瑟儿~
    2021-01-28 16:30

    Local variables are usually stored on stack, so indeed bytes are allocated for int:

    int a;
    

    Because it simply uses default value (0), so it is the same as:

    int a = 0;
    

    int is a value type, so on stack is stored its value. If you would create local variable with reference type:

    SomeClass a;
    

    Then on stack it would be allocated only reference (with value null, as it is default value for reference types). For more information you can refer this question

提交回复
热议问题