如何解决被重新分配的指针未分配?
也许这是一个愚蠢的问题,但我被困在这里了一段时间。
假设 freq_tostring()
将词频 freq
转换为字符串,然后 freq_intostream()
将该字符串附加到流的末尾。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
typedef struct {
char *word; // null-terminated
int freq;
} freq;
/**
* Constructor
*/
void new_freq(freq *fq,const char *word,const int freq) {
fq->word = (char *)malloc((strlen(word) + 1) * sizeof(char)); // +1 for null-terminator
strcpy(fq->word,word);
fq->freq = freq;
}
/**
* Free memory
*/
void dispose_freq(void *fq) {
freq *p = (freq *)fq;
free(p->word);
p->word = NULL;
}
/**
* snprintf() will terminate the string with a null character,unless buf_size is zero.
*/
char *freq_tostring(freq *fq) {
size_t wordlen = strlen(fq->word);
char *buffer = (char *)malloc(wordlen + 16); // maximum integer has 10 digits
snprintf(buffer,wordlen + 16,"[%s,%d]\n",fq->word,fq->freq);
return buffer;
}
/**
* Append the string of freq to the end of stream.
*/
void freq_intostream(void *elem,void *stream) {
freq *fq = (freq *)elem;
char *str = *(char **)stream;
size_t strsize = strlen(str);
// printf("Stream = \"%s\",length = %lu\n",str,strsize);
char *word = freq_tostring(fq);
size_t wordsize = strlen(word);
// printf("Element = \"%s\"%lu\n",word,wordsize);
char *temp = (char *)realloc(str,strsize + wordsize + 1);
strcpy(temp + strsize,word);
temp[strsize + wordsize] = '\0';
// printf("After strcpy(): \"%s\"\n",temp);
str = temp;
free(word);
}
int main(void) {
freq apple,banana,kiwi;
new_freq(&apple,"apple",3);
new_freq(&banana,"banana",2);
new_freq(&kiwi,"kiwi",5);
char *buffer = (char *)malloc(1);
buffer[0] = '\0';
freq_intostream(&apple,&buffer);
freq_intostream(&banana,&buffer);
freq_intostream(&kiwi,&buffer);
assert(strlen(buffer) == 33);
assert(strcmp(buffer,"[apple,3]\n[banana,2]\n[kiwi,5]\n") == 0);
dispose_freq(&apple);
dispose_freq(&banana);
dispose_freq(&kiwi);
free(buffer);
}
奇怪的是,当我运行 10 次时,它给了我大约 9 pointer being realloc'd was not allocated
,但可能在 1~2 种情况下,一切正常。
如果我注释掉 printf()
,它表明在附加第三个元素 kiwi
之前,流是空的,这可能是 realloc 失败的原因。但我确定我将 char * stream
的指针传递给 freq_intostream()
函数,这肯定是一个 char **
。我不知道是什么问题,有人可以帮忙吗?
解决方法
当您想要 i = j; i = 3;
时,您已经完成了与 j = 3;
等效的操作。显然,这些不做同样的事情。仔细看看这个函数中的标记线:
/**
* Append the string of freq to the end of stream.
*/
void freq_intostream(void *elem,void *stream) {
freq *fq = (freq *)elem;
char *str = *(char **)stream;
size_t strsize = strlen(str);
// printf("Stream = \"%s\",length = %lu\n",str,strsize);
char *word = freq_tostring(fq);
size_t wordsize = strlen(word);
// printf("Element = \"%s\"%lu\n",word,wordsize);
char *temp = (char *)realloc(str,strsize + wordsize + 1);
strcpy(temp + strsize,word);
temp[strsize + wordsize] = '\0';
// printf("After strcpy(): \"%s\"\n",temp);
str = temp; // OOPS!!
free(word);
}
您更改了 str
的值,但 str
是该函数的局部变量,一旦函数结束,它的值就会被丢弃。
您想要:*(char**)stream = temp;
更改调用者传递给您的指针的值。
如果你去掉所有的转换,这段代码会简单得多。如果 elem
是 char **
类型,您只需执行 *elem = temp;
,代码就会更容易理解。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。