what's the purpose of loop at the end of go runtime main

huangapple go评论131阅读模式
英文:

what's the purpose of loop at the end of go runtime main

问题

当我阅读运行时源代码时,在runtime/proc.go func main的末尾找到了这段代码:

exit(0)
for {
	var x *int32
	*x = 0
}

我查看了该文件的历史记录,发现在c-impl中已经存在了。runtime/proc.c

runtime·exit(0);
for(;;)
    *(int32*)runtime·main = 0;

这让我感到困惑。

  1. 为什么在exit之后还有代码?
  2. 这段代码什么时候会被执行?
  3. 它会直接引发 panic,那为什么还需要一个for循环?

请注意,我只提供翻译服务,不会回答关于代码功能和设计的问题。

英文:

when I read the runtime source, I find this code at the end of runtime/proc.go func main

exit(0)
for {
	var x *int32
	*x = 0
}

I check the history of the file.
I find that from the c-impl, it already is. runtime/proc.c

runtime·exit(0);
for(;;)
    *(int32*)runtime·main = 0;

It confuses me.

  1. Why there are some code after exit?
  2. When the code will be executed?
  3. It will panic directly, so Why it need a for-loop?

答案1

得分: 1

提交的评论中提到了两个原因:

  1. 如果退出没有正确实现,解引用空指针将导致程序崩溃。
  2. 没有条件的for循环是一个终止语句,尽管我不明白为什么这是必要的。
英文:

The comments on the commit give two reasons...

  1. If exit is not implemented properly, dereferencing a null pointer will crash the program.
  2. A for loop with no condition is a terminating statement, though I don't understand why this is necessary.

huangapple
  • 本文由 发表于 2022年3月7日 12:06:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/71376310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定