【问题标题】:strdup error: 'Operation now in progress'strdup 错误:“操作正在进行中”
【发布时间】:2011-11-22 14:47:22
【问题描述】:

错误是什么意思?我只需要返回我从 redis 命令得到的值。

int getReply(char* result)
{
   redisContext *c;
   redisReply *reply;

   c = redisConnect((char*)"127.0.0.2", 6379);
   reply = redisCommand(c,"GET %s", "somekey");
   if (reply->str != NULL)
   {
       result = strdup(reply->str); 
       strerror(errno); // <-------- 'Operation now in progress'. result = null
   }

   freeReplyObject(reply);

   reply = redisCommand(c, "QUIT");
   printf("Disconnecting redis: %s\n", reply->str);

   freeReplyObject(reply);

   return 0;  
}

即使我用调试器慢慢地单步执行它也会发生这种情况(人们会假设任何阻塞操作都早已完成)。 Redis 特定的错误字符串为空,reply->str 有我想要的正确字符串。

【问题讨论】:

  • strdup 错误?你是认真的吗?
  • 是吗?如果我这样做真的很愚蠢,请告诉我:(
  • strdup 唯一能返回的错误是内存不足。

标签: c redis strdup


【解决方案1】:

strdup 返回 NULL 时出现错误。

我想你想要

       result = strdup(reply->str); 
       if (!result) strerror(errno);

【讨论】:

    【解决方案2】:

    好的,大家跟进一下:这是我的错,我需要传入一个指针的地址,否则它只会传入值。所以这样做:

    int getReply(char** result)
    {
      *result = "yes";
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多