英文:
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;
这让我感到困惑。
- 为什么在
exit
之后还有代码? - 这段代码什么时候会被执行?
- 它会直接引发 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.
- Why there are some code after
exit
? - When the code will be executed?
- It will panic directly, so Why it need a
for-loop
?
答案1
得分: 1
在提交的评论中提到了两个原因:
- 如果退出没有正确实现,解引用空指针将导致程序崩溃。
- 没有条件的
for
循环是一个终止语句,尽管我不明白为什么这是必要的。
英文:
The comments on the commit give two reasons...
- If exit is not implemented properly, dereferencing a null pointer will crash the program.
- A
for
loop with no condition is a terminating statement, though I don't understand why this is necessary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论