du59131 2014-04-17 15:03
浏览 25

声明struct VS的变量之间有什么区别

type person struct {
    age    int
    gender string
}
(1)var tom person
(2)var jim person = person{}

In line one,tom is declared as person,line 2,jim is declared as person and assigned an empty person to it,what is the difference between these two?The default value are the same in both ways.Will line 1 allocate memory storage once it is declared without assigning anything to it?

Same thing as

var i int

Does the code above allocate memory or just denote an address as doing this

var i *int = new(int)

I know the i variable has different meanings in these two code,the first is a variable of type int and the second is a pointer,but the first i should also represent some address in the memory which means it allocate a certain address to i and defaults "i" to 0?

  • 写回答

2条回答 默认 最新

  • douweiluo0600 2014-04-17 15:20
    关注

    Declaring a variable vs initializing it with zero value

    In Go, all variables declared without initializers are initialized to the so-called zero values appropriate for their types. Since for a struct person its zero value is the expression person{} both cases, (1) and (2) do the same thing. I reckon the compiler will generate the same code for them.

    To cite the spec:

    A variable declaration creates a variable, binds an identifier to it and gives it a type and optionally an initial value.

    If a list of expressions is given, the variables are initialized by assigning the expressions to the variables in order; all expressions must be consumed and all variables initialized from them. Otherwise, each variable is initialized to its zero value.

    Memory allocation by declaring a variable

    The declaration var i int declares a variable and initializes it. In some sense, this does mean allocating memory (and initializing it) but I'd not dig that deep: you should be fine with the fact the compiler will arrange for the variable to exist.

    As to whether that "denotes an address" or not depends on how you look at this: it's not the programmer's business to think about how the variable is laid out in memory—the name of the variable is a handle on its value, and the compiler is free to provide such access however it pleases. I mean, the compiler may internally store the address of this variable somewhere and use it but you should not be concerned with it.

    The var i *int = new(int) declaration declares a pointer to an anonymous variable (a memory block which has the semantics of a certain type "attached" to it), initializes the memory of that variable with the zero value appropriate for its type and then assigns the address of that memory block to the variable you're declaring.

    To cite the spec:

    The built-in function new takes a type T and returns a value of type *T. The memory is initialized as described in the section on initial values.

    In this case, you operate on a pointer, not on a variable itself. So these two declarations do quite different things as they declare different types of variables, and the resulting variables have different semantics.

    Possibly the chief difference between var i int and var p *int = new(int) lies in that in the former case the variable is created "right away" (statically) while in the latter case the memory for the anonymous variable is initialized dynamically. But again do not be too attached to this: when the execution hits the var i int declaration, you cannot know for sure what exactly happens in the program other than the variable named i of type int becomes available. For instance, the compiler is free to use "the heap" to allocate it, not "the stack" (assuming a typical H/W architecture).

    评论

报告相同问题?

悬赏问题

  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离