The problem of struct in golang / c;

I haven"t been in contact with go for long. Today, I would like to discuss several questions about the literal quantity of structure, which can be regarded as a kind of record.

first question:


type Point struct{X,Y float64}

func pot(x,y float64) Point {
    return Point{x,y}
}

func pt(x,y float64) *Point {
    return &Point{x,y}
}

func main() {
    pot(2,3).X = 4  // cannot assign to point(2, 3).X
    pt(2,3).X = 4 //ok
}

reason for confusion: if the return is of * Point pointer type, the variable Point in the pt function will be assigned to the heap, and the Point scope will be expanded, so there is no problem in modifying the value.

but if Point is returned, why not change the value again?

my own idea : take a closer look at the code pot (2p3). X = 4 when performing the .X operation; the value of the Point type is still in the stack of the local function, and the value of the Point type returned from the pot function may be destroyed at some later time, if the .x is executed again; it is possible that the Point address has been reclaimed.

< hr >

I look forward to your answer as follows:

1. It"s my own idea, so we can discuss
2. Since the above ideas are all conjectured based on the results, there may be errors, one-sided and lack of basis; if your ideas are well-founded, it would be better to have a reference source

Jun.22,2021

the first problem goes deeper is the compilation principle and CPU and memory hardware. To put it shallowly, there are the following basic computer knowledge to understand:

  1. the premise of "getting an address" is to have an address. What does "address" mean? Refers to a segment in the process address space.
    what is the process address space? Each process has an independent virtual memory model, which is mapped to physical memory.
    finally, the variables in the program will exist in the form of "register + offset" at run time. The offset can be ignored first. It is recommended that you understand the concept of stack frame (stack frame) in C language. If you are interested, you can learn more about assembler and compiler.
    conclusion: a variable name is the identifier of an address, which you can simply understand as a shortcut like a windows system. The operation of getting an address is to find its real address in the process address space through this shortcut.
  2. what is "literal"? For example, a number 8, first of all, it is a data, is a data must be somewhere, either memory, or registers.
    but why can't you take an address for it and write it as & 8? Because it is an entity! Not shortcuts!
    from the point of view of actual execution, this number has no addressing operation, it will be used directly, and when the instruction it is in has been executed, it will have no effect. There is no meaning for you to keep its address.

answer your question: p: = & Point {1jue 2} is called compound literal quantity, Point {1jue 2} itself is not an entity, it is also a shortcut to a certain address, so you can take address operations such as & Point {1jue 2} .
then leave a question: for literal strings, C language can take addresses, but go does not allow them. You can think about which part of the implementation differences between the two languages affect the results. ^ _ ^

  
  1. correct you, but the literals of the basic types cannot take addresses because they do not allocate specific memory space. But objects of compound type can be addressed. So there is nothing wrong with the first example
  2. is exactly what you said. The security of go can be done
  3. .
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3646c-2c009.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3646c-2c009.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?