I don't know how `castPointer0` is used. But my experience tells me it should
never have been used in the first place. You can only take the address of the
first element of a string when the string is not empty.
Create an issue in the issue tracker.
[https://github.com/cheatfate/asyncpg/issues]
Here is the code from asyncpg package in apg_core.nim, it works before and
doesn't now:
# cast[](addr [0])
proc castPointer0(n: NimNode, v: string): NimNode {.compileTime.} =
result = newNimNode(nnkCast).add(
newIdentNode(v),
newNimNode(nnkCommand).add(
var a = ""
With `a[0]` you expect to get the null terminator. Neither in old nor in new
Nim you are allowed to write to it. With `addr a[0]` you get the address of a
null terminator that you are not allowed to write to. You just should not do
that. Btw `addr a` is possible though. But
It just parameter, useless after call.
@slangmgh Did I understand? Are you setting a `char *` in a struct?
FYI, this is very memory unsafe. I'm not sure how long your struct will live,
but if the C implementation expects a heap allocated string or static char
array then you can have problems.
The Nim GC can't automatically track ref
This is shorter and a bit more clean in my opinion
var a: cstring = ""
let a_ptr = cast[ByteAddress](a)
echo a_ptr
I need to get the pointer, then set the pointer to a struct. So I cannot just
use FFI.
If you're using FFI and the proc accepts cstring type, you don't even need to
convert string to cstring at all:
proc printf(formatstr: cstring) {.importc: "printf", varargs, header:
"".}
printf("This works %s", "as expected")
@c0ntribut0r
Thank you, this is the method I use now.
Because I need to call c function, so I need the address of the string.
I'm not sure, but here b should contain ptr to empty string - am I right?
var a = ""
let b = a.cstring
echo $cast[int](b)
But why do you need this?
Maybe the question should be elevated to why would you need this? Now that the
distinction between empty strings and nil strings is gradually erased, your use
case could become a bigger problem.
The code following is fine in the prevous version, but it failed with the
current devel branch because access the terminator zero is disabled.
var a = ""
let b = addr a[0]
Now, there is any way I can get the address of the empty string?
12 matches
Mail list logo