header information:
/* This is the reply object retued by redisCommand() */
typedef struct redisReply {
int type; /* REDIS_REPLY_* */
long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
int len; /* Length of string */
char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;
void *redisCommand(redisContext *c, const char *format, ...);
The program:
#include <stdio.h
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "hiredis.h"
int main(void) {
redisReply *reply;
long int i;
// Start measuring time
clock_t start = clock();
// For local coections:
redisContext *c = redisCoect("127.0.0.1", 6379);
// For coections to a remote Redis server:
if (c->err) {
printf("Error: %sn", c->errstr);
}else{
printf("Coection Made! n");
}
// Get all keys for testing
reply = redisCommand(c, "keys %s", "*");
printf("type is: %d, integer is: %d, len is: %d, str is %s, elements is: %zu", reply->type, reply->integer, reply->len, reply->str, reply->elements);
if ( reply->type == REDIS_REPLY_ERROR )
printf( "Error: %sn", reply->str );
else if ( reply->type != REDIS_REPLY_ARRAY )
printf( "Unexpected type: %dn", reply->type );
else {
printf("elements equals %zun", reply->elements);
for ( i=0; i< reply->elements; ++i ){
printf( "elements is %zu,...Result:%d: %sn", reply->elements, i,
reply->element[i]->str );
}
}
printf( "Total Number of Results: %dn", i );
// Output Elapsed time
printf ( "%f Secondsn", ( (double)clock() - start ) /
CLOCKS_PER_SEC );
freeReplyObject(reply);
}
The problem: On cygwin the output is: type is: 2, integer is: 0, len is: 0, str is (null), elements is: 0
elements equals 15 elements is 15,...Result:0: user:2 elements is 15,...Result:1: user:3 elements is 15,...Result:2: foo:rand:000000000000 elements is 15,...Result:3: counter elements is 15,...Result:4: mylist elements is 15,...Result:5: users elements is 15,...Result:6: foo elements is 15,...Result:7: auths elements is 15,...Result:8: bar elements is 15,...Result:9: users:GeorgeWashington elements is 15,...Result:10: next_user_id elements is 15,...Result:11: counter:rand:000000000000 elements is 15,...Result:12: key1 elements is 15,...Result:13: user:1 elements is 15,...Result:14: users_by_time Total Number of Results: 15 0.000000 Seconds
on ubuntu the output is: Coection Made! type is: 2, integer is: 0, len is: 0, str is (null), elements is: 0
elements equals 0 Total Number of Results: 0 0.000482 Seconds
Ubuntu does not iterate through the for loop because the value of elements is 0. Cygwin starts with a value of 0 for elements but the value changes to 15 without an explicit assignment. So the for loop shows the value of each key.
Two questions: 1. Why does the value of elements change in cygwin? 2. The elements value should be 15 on both machines, why does is it 0?
