Malloc() 不起作用 + 清除字符数组
Malloc() doesn't work + char array clear
第一个问题:
我在 Redis 数据库中有一些键和值。我使用以下命令阅读它们:redisReply *reply = redisCommand(c, "HGETALL %s", test_id);
然后我想创建一个 char 数组,我想把它们放在这样的地方:"key=value; key2=value2;" 等。
所以我试图使用 malloc() 为 char 数组分配内存。我执行以下操作:
int token_length;
for (i = 0; i < reply->elements; i = i + 2 )
token_length = strlen(reply->element[i]->str)+strlen(reply->element[i+1]->str)+3;
tokens = (char*)malloc(sizeof(char)*token_length);
memset(tokens, '[=10=]' , sizeof(tokens));
for (i = 0; i < reply->elements; i = i + 2 ) {
strcat(tokens,reply->element[i]->str);
strcat(tokens,"=");
strcat(tokens,reply->element[i+1]->str);
strcat(tokens,"; ");
}
我用 token_length 得到了正确的数字,但是如果我在 malloc(...) 中使用它,我会得到内存错误等。相反,如果我只输入 150 而不是 token_length在 malloc() 中,效果很好。这是为什么?
第二个问题:如果我不做memset(tokens, '[=12=]' , sizeof(tokens));
在 malloc()
之后,我在令牌数组中得到了很多 "rubbish"。有人应该总是在使用 malloc()
分配后清除 char 数组吗?是否还有其他更好的方法?
第三题:当我输出数组而不是字符“;”时,我得到\073,它是“;”的八进制表示在 ASCII table.
有一些问题 - 我不会单独描述每个错误,而是 post 一个固定版本,您可以将其与原始代码进行比较:
int token_length = 0; // <<< initialise
for (i = 0; i < reply->elements; i = i + 2 )
token_length += strlen(reply->element[i]->str)+strlen(reply->element[i+1]->str)+3; // <<< use += to accumulate length
tokens = malloc(token_length + 1); // <<< need extra char for '[=10=]' terminator
tokens[0] = '[=10=]'; // (also don't cast result of malloc() in C !)
for (i = 0; i < reply->elements; i = i + 2 ) {
strcat(tokens,reply->element[i]->str);
strcat(tokens,"=");
strcat(tokens,reply->element[i+1]->str);
strcat(tokens,"; ");
}
int token_length=0;
for (i = 0; i < reply->elements; i = i + 2 )
token_length += strlen(reply->element[i]->str)+strlen(reply->element[i+1]->str)+3;
第一个问题:
我在 Redis 数据库中有一些键和值。我使用以下命令阅读它们:redisReply *reply = redisCommand(c, "HGETALL %s", test_id);
然后我想创建一个 char 数组,我想把它们放在这样的地方:"key=value; key2=value2;" 等。
所以我试图使用 malloc() 为 char 数组分配内存。我执行以下操作:
int token_length;
for (i = 0; i < reply->elements; i = i + 2 )
token_length = strlen(reply->element[i]->str)+strlen(reply->element[i+1]->str)+3;
tokens = (char*)malloc(sizeof(char)*token_length);
memset(tokens, '[=10=]' , sizeof(tokens));
for (i = 0; i < reply->elements; i = i + 2 ) {
strcat(tokens,reply->element[i]->str);
strcat(tokens,"=");
strcat(tokens,reply->element[i+1]->str);
strcat(tokens,"; ");
}
我用 token_length 得到了正确的数字,但是如果我在 malloc(...) 中使用它,我会得到内存错误等。相反,如果我只输入 150 而不是 token_length在 malloc() 中,效果很好。这是为什么?
第二个问题:如果我不做memset(tokens, '[=12=]' , sizeof(tokens));
在 malloc()
之后,我在令牌数组中得到了很多 "rubbish"。有人应该总是在使用 malloc()
分配后清除 char 数组吗?是否还有其他更好的方法?
第三题:当我输出数组而不是字符“;”时,我得到\073,它是“;”的八进制表示在 ASCII table.
有一些问题 - 我不会单独描述每个错误,而是 post 一个固定版本,您可以将其与原始代码进行比较:
int token_length = 0; // <<< initialise
for (i = 0; i < reply->elements; i = i + 2 )
token_length += strlen(reply->element[i]->str)+strlen(reply->element[i+1]->str)+3; // <<< use += to accumulate length
tokens = malloc(token_length + 1); // <<< need extra char for '[=10=]' terminator
tokens[0] = '[=10=]'; // (also don't cast result of malloc() in C !)
for (i = 0; i < reply->elements; i = i + 2 ) {
strcat(tokens,reply->element[i]->str);
strcat(tokens,"=");
strcat(tokens,reply->element[i+1]->str);
strcat(tokens,"; ");
}
int token_length=0;
for (i = 0; i < reply->elements; i = i + 2 )
token_length += strlen(reply->element[i]->str)+strlen(reply->element[i+1]->str)+3;