Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8026747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:35:29+00:00 2026-06-04T23:35:29+00:00

I’m experiencing a bit of cognitive dissonance between C-style stack-based programming, where automatic variables

  • 0

I’m experiencing a bit of cognitive dissonance between C-style stack-based programming, where automatic variables live on the stack and allocated memory lives on the heap, and Python-style stack-based-programming, where the only thing that lives on the stack are references/pointers to objects on the heap.

As far as I can tell, the two following functions give the same output:

func myFunction() (*MyStructType, error) {
    var chunk *MyStructType = new(HeaderChunk)

    ...

    return chunk, nil
}


func myFunction() (*MyStructType, error) {
    var chunk MyStructType

    ...

    return &chunk, nil
}

i.e., allocate a new struct and return it.

If I’d written that in C, the first one would have put an object on the heap and the second would have put it on the stack. The first would return a pointer to the heap, the second would return a pointer to the stack, which would have evaporated by the time the function had returned, which would be a Bad Thing.

If I’d written it in Python (or many other modern languages except C#) example 2 would not have been possible.

I get that Go garbage collects both values, so both of the above forms are fine.

To quote:

Note that, unlike in C, it’s perfectly OK to return the address of a
local variable; the storage associated with the variable survives
after the function returns. In fact, taking the address of a composite
literal allocates a fresh instance each time it is evaluated, so we
can combine these last two lines.

http://golang.org/doc/effective_go.html#functions

But it raises a couple of questions.

  1. In example 1, the struct is declared on the heap. What about example 2? Is that declared on the stack in the same way it would be in C or does it go on the heap too?

  2. If example 2 is declared on the stack, how does it stay available after the function returns?

  3. If example 2 is actually declared on the heap, how is it that structs are passed by value rather than by reference? What’s the point of pointers in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:35:30+00:00Added an answer on June 4, 2026 at 11:35 pm

    It’s worth noting that the words “stack” and “heap” do not appear anywhere in the language spec. Your question is worded with “…is declared on the stack,” and “…declared on the heap,” but note that Go declaration syntax says nothing about stack or heap.

    That technically makes the answer to all of your questions implementation dependent. In actuality of course, there is a stack (per goroutine!) and a heap and some things go on the stack and some on the heap. In some cases the compiler follows rigid rules (like “new always allocates on the heap”) and in others the compiler does “escape analysis” to decide if an object can live on the stack or if it must be allocated on the heap.

    In your example 2, escape analysis would show the pointer to the struct escaping and so the compiler would have to allocate the struct. I think the current implementation of Go follows a rigid rule in this case however, which is that if the address is taken of any part of a struct, the struct goes on the heap.

    For question 3, we risk getting confused about terminology. Everything in Go is passed by value, there is no pass by reference. Here you are returning a pointer value. What’s the point of pointers? Consider the following modification of your example:

    type MyStructType struct{}
    
    func myFunction1() (*MyStructType, error) {
        var chunk *MyStructType = new(MyStructType)
        // ...
        return chunk, nil
    }
    
    func myFunction2() (MyStructType, error) {
        var chunk MyStructType
        // ...
        return chunk, nil
    }
    
    type bigStruct struct {
        lots [1e6]float64
    }
    
    func myFunction3() (bigStruct, error) {
        var chunk bigStruct
        // ...
        return chunk, nil
    }
    

    I modified myFunction2 to return the struct rather than the address of the struct. Compare the assembly output of myFunction1 and myFunction2 now,

    --- prog list "myFunction1" ---
    0000 (s.go:5) TEXT    myFunction1+0(SB),$16-24
    0001 (s.go:6) MOVQ    $type."".MyStructType+0(SB),(SP)
    0002 (s.go:6) CALL    ,runtime.new+0(SB)
    0003 (s.go:6) MOVQ    8(SP),AX
    0004 (s.go:8) MOVQ    AX,.noname+0(FP)
    0005 (s.go:8) MOVQ    $0,.noname+8(FP)
    0006 (s.go:8) MOVQ    $0,.noname+16(FP)
    0007 (s.go:8) RET     ,
    
    --- prog list "myFunction2" ---
    0008 (s.go:11) TEXT    myFunction2+0(SB),$0-16
    0009 (s.go:12) LEAQ    chunk+0(SP),DI
    0010 (s.go:12) MOVQ    $0,AX
    0011 (s.go:14) LEAQ    .noname+0(FP),BX
    0012 (s.go:14) LEAQ    chunk+0(SP),BX
    0013 (s.go:14) MOVQ    $0,.noname+0(FP)
    0014 (s.go:14) MOVQ    $0,.noname+8(FP)
    0015 (s.go:14) RET     ,
    

    Don’t worry that myFunction1 output here is different than in peterSO’s (excellent) answer. We’re obviously running different compilers. Otherwise, see that I modfied myFunction2 to return myStructType rather than *myStructType. The call to runtime.new is gone, which in some cases would be a good thing. Hold on though, here’s myFunction3,

    --- prog list "myFunction3" ---
    0016 (s.go:21) TEXT    myFunction3+0(SB),$8000000-8000016
    0017 (s.go:22) LEAQ    chunk+-8000000(SP),DI
    0018 (s.go:22) MOVQ    $0,AX
    0019 (s.go:22) MOVQ    $1000000,CX
    0020 (s.go:22) REP     ,
    0021 (s.go:22) STOSQ   ,
    0022 (s.go:24) LEAQ    chunk+-8000000(SP),SI
    0023 (s.go:24) LEAQ    .noname+0(FP),DI
    0024 (s.go:24) MOVQ    $1000000,CX
    0025 (s.go:24) REP     ,
    0026 (s.go:24) MOVSQ   ,
    0027 (s.go:24) MOVQ    $0,.noname+8000000(FP)
    0028 (s.go:24) MOVQ    $0,.noname+8000008(FP)
    0029 (s.go:24) RET     ,
    

    Still no call to runtime.new, and yes it really works to return an 8MB object by value. It works, but you usually wouldn’t want to. The point of a pointer here would be to avoid pushing around 8MB objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.