M
Modular10mo ago
Gokul

How does memcpy work in 0.6.0 ?

Before, I wrote a helper function for string vector in 0.5.0 and memcpy was working fine, now I am unable to do so in 0.6.0, this is code snippet:
alias strtype = DTypePointer[DType.int8]
var data: Pointer[strtype]
...
self.data = self.data.alloc(self.capacity)

# previously this worked in 0.5.0
let str_data = strtype.alloc(len(str)+1)
memcpy(str_data, str._buffer.data, len(str)+1)

# In version 0.6.0 this statement fails:
memcpy(str_data, str._buffer.data, len(str)+1)

# so i tried this, but this also fails
alias strtype = DTypePointer[DType.int8, 0]
...
memcpy[DType.int8, 0](str_data, str._buffer.data, len(str)+1)
error i got was: candidate not viable: callee parameter #0 has 'AnyRegType' type, but value has type 'DType'
alias strtype = DTypePointer[DType.int8]
var data: Pointer[strtype]
...
self.data = self.data.alloc(self.capacity)

# previously this worked in 0.5.0
let str_data = strtype.alloc(len(str)+1)
memcpy(str_data, str._buffer.data, len(str)+1)

# In version 0.6.0 this statement fails:
memcpy(str_data, str._buffer.data, len(str)+1)

# so i tried this, but this also fails
alias strtype = DTypePointer[DType.int8, 0]
...
memcpy[DType.int8, 0](str_data, str._buffer.data, len(str)+1)
error i got was: candidate not viable: callee parameter #0 has 'AnyRegType' type, but value has type 'DType'
I have noticed, that a concept of address space is introduced, but I am not able to find solid examples to learn from. Need some help
8 Replies
sora
sora10mo ago
This seems to work, doesn't look idiomatic in anyway though.
fn main():
alias strtype = DTypePointer[DType.int8]
let str: String = "123"
let str_data = strtype.alloc(len(str) + 1)
let p = strtype(str._buffer.data.value)
memcpy(str_data, p, len(str) + 1)
print(str_data)
_ = str
fn main():
alias strtype = DTypePointer[DType.int8]
let str: String = "123"
let str_data = strtype.alloc(len(str) + 1)
let p = strtype(str._buffer.data.value)
memcpy(str_data, p, len(str) + 1)
print(str_data)
_ = str
Gokul
Gokul10mo ago
Thanks a lot for replying @sora , could you explain, why are we doing this line:
let p = strtype(str._buffer.data.value)
let p = strtype(str._buffer.data.value)
Michael K
Michael K10mo ago
Its an underscored method but I think you want ._as_ptr().
fn main():
alias strtype = DTypePointer[DType.int8]
let str: String = "123"
let newstr = strtype.alloc(len(str) + 1)
memcpy(newstr, str._as_ptr(), len(str) + 1)
str._strref_keepalive() # newstr is empty without this
print(String(newstr)) # prints "123"
fn main():
alias strtype = DTypePointer[DType.int8]
let str: String = "123"
let newstr = strtype.alloc(len(str) + 1)
memcpy(newstr, str._as_ptr(), len(str) + 1)
str._strref_keepalive() # newstr is empty without this
print(String(newstr)) # prints "123"
sora
sora10mo ago
Picking apart an AnyPointer and cast it to DTypePointer. Mojo pointers are confusing, i don't fully understand what i'm doing. I think you should go with @Michael K 's answer as that looks less hacky.
Gokul
Gokul10mo ago
What is Anypointer? Is it an abstract class for pointers or where can i learn more about it ?
sora
sora10mo ago
It's also not documented.
Gokul
Gokul10mo ago
No problem, Thanks for helping out @sora, @Michael K . The solution worked, will explore further. 👍
Michael K
Michael K10mo ago
Also if you just want the underlying data in a pointer and dont need the original String, you can use str._steal_ptr(). No memcpy needed.
Want results from more Discord servers?
Add your server