【问题标题】:pointer being realloc'd was not allocated?没有分配重新分配的指针?
【发布时间】:2021-05-13 11:51:57
【问题描述】:

也许这是一个愚蠢的问题,但我在这里卡了一段时间。

假设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 **。我不知道是什么问题,谁能帮忙?

【问题讨论】:

  • str = temp 不会将temp 指向的缓冲区的内容复制到str 指向的缓冲区中。它只是将指针str 设置为与temp 相同的地址。也许你想要strcpy(str, temp)

标签: c realloc


【解决方案1】:

当你想要j = 3; 时,你已经完成了相当于i = j; i = 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; 更改调用者传递给您的指针的值。

如果你去掉所有的演员,这段代码会简单得多。如果elemchar ** 类型,您可以只使用*elem = temp;,代码会更容易理解。

【讨论】:

  • 感谢@David Schwartz。我用*(char **)stream = temp 测试过,它有效。但是我还是有点疑惑,如果栈中的str丢失了,为什么10%的情况下代码可以通过测试?
  • 并且测试总是在第三个元素“kiwi”处失败,这意味着对于前两个元素“apple”和“banana”,它可以在错误的代码str = temp下正常工作。这听起来很奇怪?
  • 这是否意味着对于前两个元素,realloc 只是扩大原始内存空间,对于第三个元素它尝试创建一个新的内存空间,在这种情况下我的str = temp 将失败。我说的对吗?
  • @shen 这是一个可能的理论。 realloc 函数并不总是改变指针,即使它改变了,当你溢出缓冲区时它仍然可能会正常工作。 UB 的后果可能取决于奇怪的平台怪癖。
猜你喜欢
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
相关资源
最近更新 更多