duanjing4667 2017-11-16 13:45
浏览 74
已采纳

了解Go工具的编译和链接命令

I understand that there are three steps involved in converting a high level language code to machine language or executable code namely - compiling, assembling, and linking.

As per go docs go tool compile does the following - It then writes a single object file named for the basename of the first source file with a .o suffix

So, the final object file must contain the machine language code (after compiling and assembling are run) of each file. If I pass go tool compile -S on the go file, it shows assembly language go generates.

After this when I run go tool link on the object file, it must link all required object files (if multiple) and then generate final machine language code (based on GOOS and GOARCH). It generates a file a.out

Have few basic questions here -

How do I know which variables and when are they going to be allocated memory in stack and heap? Does it matter if I generate executable for one machine and run on another with different architecture?

My test program

package main

func f(a *int, b *int) *int { 

    var c = (*a + *b); 
    var d = c;
    return &d;
}

func main() {
    var a = 2;
    var b = 6;

    f(&a,&b);
}

Result of go tool compile -m -l test.go

test.go:6: moved to heap: d
test.go:7: &d escapes to heap
test.go:3: f a does not escape
test.go:3: f b does not escape
test.go:14: main &a does not escape
test.go:14: main &b does not escape
  • 写回答

1条回答 默认 最新

  • dongxun6690 2017-11-16 13:57
    关注

    In which step is the memory getting allocated?

    It depends, some during linking, some during compiling, most during runtime (And some during loading).

    How do I know which variables and when are they going to be allocated memory in stack and heap?

    Ad hoc you do not know at all. The compiler decides this. If the compiler can prove that a variable does not escape it might keep it on the stack. Google for "golang escape analysis". There is a flag -m which makes the compiler output his decisions if you are interested in it.

    Does it matter if I generate executable for one machine and run on another with different architecture?

    No, but simply because this does not work at all: Executables are tied to architecture and won't run on a different one.

    It seems you are mixing up compilation/linking and memory allocation. The later is pretty different from the former two. (Technically your linked program may contain memory and during loading it might get even more, but this is highly technical and architecture specific and really nothing to be concerned of).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值