redis에서 POD 구조체를 저장하면 const char *
으로 올바르게 작동하지만 std::string
이 포함 된 경우에는 작동하지 않습니다. 내가 선을 교체 할 경우키가 std :: string으로 정의 된 경우 redis에 저장된 pod 구조체의 비 직렬화가 실패합니다
const를 숯불 * 예를
#include <hiredis/hiredis.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
struct Test
{
const uint32_t id;
const char *name;
};
int main() {
redisContext *context = redisConnect("127.0.0.1", 6379);
if (context == NULL || context->err) {
if (context) {
printf("Error: %s\n", context->errstr);
} else {
printf("Can't allocate redis context\n");
}
exit(EXIT_FAILURE);
}
Test obj = {(uint32_t) 123, "foo bar"};
const size_t size = 2 * sizeof(uint32_t) + (strlen(obj.name) + 1);
cout << "object{id: " << obj.id << ", name:' " << obj.name << "'}; size: " << size << endl;
redisReply *reply = 0;
const char *key = strdup("some-key");
reply = (redisReply *) redisCommand(context, "SET %b %b", key, strlen(key), &obj, size);
if (!reply)
return REDIS_ERR;
freeReplyObject(reply);
reply = (redisReply *) redisCommand(context, "GET %s", key);
if (!reply)
return REDIS_ERR;
Test *res = (struct Test*) reply->str;
cout << "result{id: " << res->id << ", name:' " << res->name << "'}; size: " << size << endl;
freeReplyObject(reply);
redisFree(context);
}
:
const char *key = strdup("some-key");
reply = (redisReply *) redisCommand(context, "SET %b %b", key, strlen(key), &obj, size);
실행이 항상 분할 오류 끝나는
std::string key("some-key");
reply = (redisReply *) redisCommand(context, "SET %b %b", key.c_str(), key.size(), &obj, size);
와.
저는 혼자서 문제를 해결할 수 없으며 어떤 도움을 주셔서 감사합니다.
'std :: string'을 포함하는 구조체는 더 이상 POD가 아닙니다! –
이지만
struct
에는std::string
이 포함되어 있지 않습니다. 열쇠는std::string
개체입니다. – palik댓글에서 이와 같은 코드를 렌더링 할 수 없습니다. 백틱 사용'' ' –