Problem with Hiredis Binding

Mike Parker aldacron at gmail.com
Thu Jan 5 20:17:55 PST 2012


On 1/6/2012 11:38 AM, Puming wrote:
> On Thursday, 5 January 2012 at 22:02:25 UTC, Andrej Mitrovic wrote:
>
>> Your problem is that you're calling printf on a static char array. If
>> you're going to use printf you have to use it on the .ptr (pointer)
>> field of the static array. Replace this call:
>>
>> printf("Connection error: %s\n", c.errstr);
>> with
>> printf("Connection error: %s\n", c.errstr.ptr);
>>
>> On my end I get:
>> Connection error: Connection refused
>>
>> Also I think you should replace the "int integer" in struct redisReply
>> to "long integer". I think `long long` in C is 8 bytes, which is the
>> equivalent to D's long type.
>
> Thanks for the tip. I've changed code as you suggested.
> And for the type `long long`, I changed `int` to `c_long` (in
> core.stdc.config) according to http://dlang.org/interfaceToC.html
>
> But there is still problem. My code does actually connect, as I have a
> redis-server running on my machine, and the commands actually runned, as
> I use a redis-cli and found that "foo : bar" is actually set into redis.
> But
>
> ---
> reply = cast(redisReply*) redisCommand(c, "GET foo");
> writefln("GET foo: %s", *reply);
> writefln(to!string(reply.str));
> ----
>
> output these:
>
> ---
> GET foo: redisReply(1, 0, 0, 2, 152397072, 0)
> 80B1F42
> 段错误 // (which is Chinese for Segfault)
> ---
>
> So maybe I got it wrong when converting char* to string?
>
>
>

I think the problem still lies with your declaration of the integer 
field. c_long in D is supposed to be equivalent to the long type in C, 
*not* long long. It is declared as 32 bits on a 32-bit system and 64 
bits on a 64-bit system. I believe that in practice, most C compilers 
implement long long as 64 bits on both 32- and 64-bit systems. I 
recommend you use long on the D side, rather than c_long. And, actually, 
it would be best to compile a test with the same C compiler used to 
compile redis to verify the size of long long.



More information about the Digitalmars-d-learn mailing list