#unsafe package

146 messages · Page 1 of 1 (latest)

acoustic star
#
package main

import "fmt"
import "unsafe"

func main() {
    var myname string = "hello world"

    ptr := (*uint8)(unsafe.Add(unsafe.Pointer(&myname), 8))

    *ptr = 1


    fmt.Println("the value :",myname)
    fmt.Println("the size  :",unsafe.Sizeof(myname))

}
``8`` is the pointer ``size`` in this case we are dealing with ``ascii`` english characters we should keep the pointer size ``8`` and lastly we did type conversion because we are returning a pointer that has no type so we need to cast the pointer and give it a type but what confuses me ``unsafe.Add`` this meant to do ``slicing`` as i worked with a little bit and from what i understood !!
sweet iris
#

this code is illegal, strings are immutable

acoustic star
sweet iris
#

I don't understand the description

#

but fwiw the code is wrong so, probably

acoustic star
sweet iris
#

any of it

acoustic star
#

i have mentioned that the number 8 is the size of the pointer

sweet iris
#

that's not totally correct

#

but most of the time, yes

acoustic star
#

what i understand from you

#

depending on the context

#

is that what you are trying to say ?

sweet iris
#

it depends on the target architecture

acoustic star
#

i see

#

is the length of the pointer should equal to the type it's beencasted to ?

sweet iris
#

I don't understand what that means, pointers don't have a length

#

the size of a pointer value is architecture dependent

acoustic star
#

i know but you set the size of the pointer

sweet iris
#

are you referring to the length of the string?

#

I do not understand what you are trying to say

acoustic star
#

i am trying to express the 8 i wrote

#

let me try to do better

#

(*uint16)(unsafe.Add(unsafe.Pointer(&myname)), 8)

we got the pointer here of the string so now we have the location of the pointer in memory

#

but 8 means the length the pointer

#

if we have

#

hello world

#

8 refers to the first element of the string

sweet iris
#

no

acoustic star
#

which is h

sweet iris
#

you are assuming the size of a pointer is 8, which is a problematic assumption as the size of a pointer is architecture dependant

acoustic star
#

but

#

i get different results to what u are saying

#

let me please write some codes and the results

sweet iris
#

I have not given you any results

acoustic star
#

no no

#

u misunderstood

#

my results that is what i meant

sweet iris
#

I'm just stating that:
1 - the code is just wrong
2 - you are assuming 64 bit pointers

#

number 2 is just a note

acoustic star
#

let me show

#

you

#

see

#

i told you

#

if i do chinese characters

#

that would not work

#

if i do 16 that would work

#

because chinese take more than 1 byte

#

and 8 is a byte

sweet iris
#

the code is still both illegal and incorrect

#

this has nothing to do with the string data

acoustic star
#

why is it illegal and not correct explain more please

#

is it a ub ?

sweet iris
#

it is illegal because strings are immutable

#

it is incorrect because it will only work on little endian machines

acoustic star
#

how immutable i don't understand ?

sweet iris
#

100% immutable

acoustic star
#

is there anything i can do to check ?

sweet iris
#

check what?

acoustic star
#

if it is immutable

sweet iris
#

read the spec

acoustic star
#

it seems mutable to me

sweet iris
#

with unsafe everything is mutable

acoustic star
#

but anyways i will check what you said

sweet iris
#

I'm not talking about variables of type string

#

I'm talking about strings

sweet iris
acoustic star
#

of course they are immutable

sweet iris
#

ok so stop trying to mutate them

acoustic star
#

??!!

#

i still don't understand you

#

if i mutate them i will get unknown stuff for sure

#

which mutate are you talking about ?

sweet iris
#

not sure there is more than one definition 🙂

acoustic star
#

very strange mutating a constant value

#

?!!!!!

#

what

#

crazy

sweet iris
#

what are you trying to do

acoustic star
#

i am trying to understand what you said

#

it is very strange

sweet iris
#

what are you trying to do with the code

acoustic star
#

just practice

#

and making sure of the information

sweet iris
#

what are you trying to accomplish with the code

acoustic star
#

to be honest

#

i want to do slicing

#

using pointers

#

like

#

string

#

and stringdata

sweet iris
#

sliced := str[3:42]

acoustic star
#

but differently

#

no

#

using pointers

#

i know that

sweet iris
#

there you go

#

stop using unsafe, learn the language first

#

then you can do whatever you want

acoustic star
#

what about

#

pointer arithmetic

sweet iris
#

go doesn't have pointer arithmetic

acoustic star
#

it does

#

unsafe

sweet iris
#

unsafe is not go

acoustic star
#

???

#

what

#

what do you mean

#

they told me

#

unsafe

sweet iris
#

who's they

acoustic star
#

google

sweet iris
#

google told you unsafe?

acoustic star
#

damn they wasted my time

acoustic star
#

fo uintptr

#

and then

#

they told me it is for pointers

#

and then

#

i encountered arithmetic pointers

#

and then

#

started digging

#

and then

#

i found unsafe

#

see ? it is like a loop

sweet iris
#

and then we told you to drop unsafe and learn the language

#

and you kept digging

acoustic star
#

damn

sweet iris
#

stop digging, learn the language

#

learning unsafe is not part of learning the language

#

and learning the language (among other thigs) is prerequisite knowledge to learn unsafe

acoustic star
#

what can i use unsafe for tho ?

sweet iris
#

nothing, unsafe is useless, drop it

#

learn the language

#

then we talk

acoustic star
#

ok brother

#

you are amazing brother

elfin relic
acoustic star
elfin relic
#

the only case you will need uintptr is finalizer, or operate with C library

#

but finalizer is also recommend only use with C lib as well

acoustic star
#

i see