Re: How to get the address of string

2018-05-15 Thread Krux02
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]

Re: How to get the address of string

2018-05-15 Thread slangmgh
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(

Re: How to get the address of string

2018-05-15 Thread Krux02
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

Re: How to get the address of string

2018-05-11 Thread slangmgh
It just parameter, useless after call.

Re: How to get the address of string

2018-05-11 Thread twetzel59
@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

Re: How to get the address of string

2018-05-09 Thread mratsim
This is shorter and a bit more clean in my opinion var a: cstring = "" let a_ptr = cast[ByteAddress](a) echo a_ptr

Re: How to get the address of string ""

2018-05-09 Thread slangmgh
I need to get the pointer, then set the pointer to a struct. So I cannot just use FFI.

Re: How to get the address of string ""

2018-05-08 Thread c0ntribut0r
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")

Re: How to get the address of string ""

2018-05-08 Thread slangmgh
@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.

Re: How to get the address of string ""

2018-05-08 Thread c0ntribut0r
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?

Re: How to get the address of string ""

2018-05-08 Thread yglukhov
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.

How to get the address of string ""

2018-05-08 Thread slangmgh
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?