is there a void pointer in go?

3,731 views
Skip to first unread message

Fabio Kaminski

unread,
Aug 24, 2010, 4:00:32 PM8/24/10
to golan...@googlegroups.com
is there a void pointer in go?

i have to point to something inside a struct that could be 2 different(not related) substructs...

can i user a void pointer like in C? 

chris dollin

unread,
Aug 24, 2010, 4:08:22 PM8/24/10
to Fabio Kaminski, golan...@googlegroups.com
On 24 August 2010 21:00, Fabio Kaminski <fabiok...@gmail.com> wrote:
> is there a void pointer in go?

No.

> i have to point to something inside a struct that could be 2 different(not
> related) substructs...

Can you be more specific about what you're trying to do? I suspect an
opportunity for using an interface.

Chris

--
Chris "allusive" Dollin

bemi

unread,
Aug 24, 2010, 4:45:39 PM8/24/10
to golang-nuts

> is there a void pointer in go?

Maybe package "unsafe.Pointer" and address operator '&' could help.

Address operator gives you pointer pointing to certain type, and any
pointer to any type can be cast to unsafe.Pointer. Vice-versa,
unsafe.Pointer can be cast to pointer to any type. Thus unsafe.Pointer
has similar behaviour to 'void *'.

Example:

package main

import "fmt"
import "unsafe"

func main() {
var i int = 1
var q *int
var p unsafe.Pointer

fmt.Println(i)

p = unsafe.Pointer(&i)
q = (*int)(p)
*q = 2

fmt.Println(i)
}

> i have to point to something inside a struct that could be 2 >different(not
> related) substructs...

I don't think that hiding the type and using void for 2 different
types would be good idea, you will not be able to map back from void
to original type. If you are passed in void how are you going to know
which of 2 types to cast to?

Fabio Kaminski

unread,
Aug 24, 2010, 5:35:33 PM8/24/10
to bemi, golang-nuts
>"I don't think that hiding the type and using void for 2 different
>types would be good idea, you will not be able to map back from void
>to original type. If you are passed in void how are you going to know
>which of 2 types to cast to?"

thats why a post the C union idea, to be a candidade for this too... 
given 2 pointers, but only one sector of memory.. would be better of course..
but the major question is , how i can do this, in Go.. 
i will try to hack more unsafe.. but this is not supposed to use for C interop?
i guess i will end with 2 pointers in the struct and saying goodbye to my memory anyway.. :s

Fabio Kaminski

unread,
Aug 24, 2010, 5:36:16 PM8/24/10
to golang-nuts
in unsafe.go i've found this :

// Pointer represents a pointer to an arbitrary type.  There are three special operations
// available for type Pointer that are not available for other types.
// 1) A pointer value of any type can be converted to a Pointer.
// 2) A uintptr can be converted to a Pointer.
// 3) A Pointer can be converted to a uintptr.
// Pointer therefore allows a program to defeat the type system and read and write
// arbitrary memory. It should be used with extreme care.

type Pointer *ArbitraryType

Russ Cox

unread,
Aug 24, 2010, 6:18:05 PM8/24/10
to Fabio Kaminski, golang-nuts
You don't want to use unsafe.Pointer.
If you have two different types that can
be stored in that field, give it type interface{}
and use a type switch (or comma ok check)
to test which one it is.

Russ

Namegduf

unread,
Aug 24, 2010, 6:02:29 PM8/24/10
to golan...@googlegroups.com

Set the value inside the struct to interface{}, and you can assign
either substruct to it, and use a type assertion in place of a cast to
get it back.

This is, generally, the replacement for where you'd use a void pointer
in C, and does not require using unsafe or any other package.

Fabio Kaminski

unread,
Aug 24, 2010, 6:52:07 PM8/24/10
to r...@golang.org, golang-nuts
Yes, thanks!

its something like that i was looking for.. what void* is to c and Object is to java.. 
the general abstraction that you could represent "anything" inside the language envorinment..

i forgot how the type system are soft, and about type switches.. 

just 3 days using the language .. i was looking for the idiomatic way to handle this.. and of course.. get into the unsafe environment only if i must to..
but its good to see that the hardware can be transparent if you want to using the unsafe  :)

Thanks
Reply all
Reply to author
Forward
0 new messages