On Mon, Aug 6, 2018 at 11:34 PM, nicolas_boiteux via golang-nuts <golang-nuts@googlegroups.com> wrote: > > yes i used the code from this page: > > func Float64bits(f float64) uint64 { > return *(*uint64)(unsafe.Pointer(&f)) > } > > adapted to my use case > > *(*int)(unsafe.Pointer(limit)
As the docs say, that is only permissible when T2 is no larger than T1 and the two share an equivalent memory layout. In your case T2 is the Go type `int` and T1 is the C type `int`. On 64-bit systems the C type `int` is 32 bits and the Go type `int` is 64 bits. So T2 is larger than T1 and your conversion is not permitted by the unsafe rules. Ian > Le lundi 6 août 2018 19:53:07 UTC+2, Ian Lance Taylor a écrit : >> >> On Sun, Aug 5, 2018 at 10:29 PM, nicolas_boiteux via golang-nuts >> <golan...@googlegroups.com> wrote: >> > >> > thanks you, the A3 community finaly help me to solve the last problem >> > >> > for index := C.int(0); index < argc; index++ { >> > out = append(out, C.GoString(*argv)) >> > argv = (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + >> > offset)) >> > } >> > >> > I dont really understand why but it seems there was a problem with my >> > casting method with unsafe >> > >> > *(*int)(unsafe.Pointer(limit) >> >> See https://golang.org/pkg/unsafe for the exact ways that you are >> permitted to use unsafe.Pointer. >> >> Ian >> >> > Le dimanche 5 août 2018 15:02:47 UTC+2, Jan Mercl a écrit : >> >> >> >> On Sun, Aug 5, 2018 at 2:50 PM nicolas_boiteux via golang-nuts >> >> <golan...@googlegroups.com> wrote: >> >> >> >> > not sure to understand cause the iteration in your example is done on >> >> > os >> >> > interface from golang not from c char array :( >> >> >> >> Not sure what you mean by 'os interface'. Here's the part that iterates >> >> **C.char, except pointers are transformed to uintptrs. Other that that >> >> it's >> >> what you're, I think, after >> >> >> >> // Xmain is defined at main.c:3:5 >> >> func Xmain(tls crt.TLS, _argc int32, _argv uintptr /* **int8 */) (r >> >> int32) >> >> { >> >> var _p uintptr // **int8 >> >> >> >> _p = _argv >> >> _1: >> >> if (*(*uintptr)(unsafe.Pointer(_p))) == 0 { >> >> goto _3 >> >> } >> >> >> >> crt.Xprintf(tls, ts+0 /* "%s\n" */, *(*uintptr)(unsafe.Pointer(_p))) >> >> _p += 8 >> >> goto _1 >> >> >> >> _3: >> >> return r >> >> } >> >> >> >> Manually converting (untested again): >> >> >> >> func foo(argv **C.char) []string { >> >> var a []string >> >> for p := argv; *p != nil; *(*uintptr)(unsafe.Pointer(p)) += >> >> unsafe.Sizeof(*p) { >> >> a = append(a, GoString(*p)) >> >> } >> >> rerurn a >> >> } >> >> >> >> >> >> -- >> >> >> >> -j >> > >> > -- >> > 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/d/optout. > > -- > 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+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.