Re: [go-nuts] Can we check if a pointer if pointer to a stack addr or heap addr during the compiling time?

104 views
Skip to first unread message

Scott Lawrence

unread,
Feb 26, 2013, 11:55:19 PM2/26/13
to Robert Sandra, golan...@googlegroups.com
No. Why do you want to?

Go doesn't distinguish - returning from the function won't invalidate the
pointer, even if you think it should be on the stack.

On Tue, 26 Feb 2013, Robert Sandra wrote:

> Hi all,
>
> I am now reading the go compiler code. I am wondering if we can check
> whether a pointer is pointing to a stack address or a heap address?
>
> Take the following codes as an example:
>
> type Vertex struct{
> X, Y int
> }
>
> p := V{1,2}
> q := new(Vertex)
> m := &p
>
> then we know that m is a pointer pointing to a local variable in the stack,
> while q is pointing to a heap address.Can we differentiate these two
> different types of pointer statically? By saying statically, I mean during
> the compiling time.
>
> Thanks.
> Robert
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

--
Scott Lawrence

go version go1.0.3
Linux baidar 3.7.9-1-ARCH #1 SMP PREEMPT Mon Feb 18 02:13:30 EET 2013 x86_64 GNU/Linux

Rob Pike

unread,
Feb 26, 2013, 11:55:49 PM2/26/13
to Robert Sandra, golan...@googlegroups.com
On Tue, Feb 26, 2013 at 8:52 PM, Robert Sandra
<robert.s...@gmail.com> wrote:
> Hi all,
>
> I am now reading the go compiler code. I am wondering if we can check
> whether a pointer is pointing to a stack address or a heap address?
>
> Take the following codes as an example:
>
> type Vertex struct{
> X, Y int
> }
>
> p := V{1,2}
> q := new(Vertex)
> m := &p
>
> then we know that m is a pointer pointing to a local variable in the stack,
> while q is pointing to a heap address.Can we differentiate these two
> different types of pointer statically? By saying statically, I mean during
> the compiling time.

There is no way to know this, since the compiler is free to move
things from stack to heap and vice versa. In your example, q could
point to the stack and p to a variable in the heap. It depends on
unknowable, internal criteria of the compiler.

-rob

Robert Sandra

unread,
Feb 27, 2013, 12:17:35 AM2/27/13
to golan...@googlegroups.com, Robert Sandra
I assume that these movements between stack and heap are happening during the compiler optimization step, right? If yes, what is the case in the steps before optimization? For example, on the abstract syntax tree level, p is surely pointing to the stack and q to the heap, am I right?

Rémy Oudompheng

unread,
Feb 27, 2013, 2:10:17 AM2/27/13
to Robert Sandra, golan...@googlegroups.com
On 2013/2/27 Robert Sandra <robert.s...@gmail.com>:
> I assume that these movements between stack and heap are happening during
> the compiler optimization step, right? If yes, what is the case in the steps
> before optimization? For example, on the abstract syntax tree level, p is
> surely pointing to the stack and q to the heap, am I right?

No, at the AST level, there is no notion of stack and heap. p and q
are just variable names.

Rémy.

Rob Pike

unread,
Feb 27, 2013, 2:26:06 AM2/27/13
to Robert Sandra, golan...@googlegroups.com
Your question is fine from an implementation standpoint but it's
important to understand that it's the compiler's business. It's like
asking, how can I tell if this variable in R0 rather than R1?

-rob
Reply all
Reply to author
Forward
0 new messages