[Go] return value is placed in Heap in Golang

Seunghyun Moon·2022년 10월 6일
0

Go

목록 보기
2/2

while learning Golang, I came across something that shows "stability" feature of Golang.

The instructor's quote

for Golang a return value of a function is placed in Heap memory rather than in Stack in Golang.

from an example of the following book, I tried calling a recursive with no break in it.
Tucker의 Go 언어 프로그래밍


resource moniorting using htop1

resource moniorting using htop2

the PrintNo function was called more than 5 million times and htop shows that the server is busy.

a similar code in C does not support such a number of recursives because the system's limit is set to a small value.
ulimit to see the server settings.

stack value is set only to 8k
example C code with no break

#include <stdio.h>

int sum(int i);

int main()
{
   printf("%d\n", sum(10));
}

int sum(int i)
{
   return i+sum(i-1);
}

the program shuts with Segmentation error

Takeaway

Keep learning Golang!

profile
I live fullest

0개의 댓글