在C中为结构动态分配内存的问题

时间:2018-12-12 10:37:30

标签: c struct malloc realloc wchar-t

我得到了数量未知的“宽符号”。文本的格式设置为句子,我必须将其添加到结构“文本”中。

这些是我的结构:

struct Sentence {
    wchar_t *sentence;
    int amount_of_symbols;
};

struct Text {
    struct Sentence *sentences;
    int amount_of_sentences;
}; 

我为“句子”结构数组动态分配内存并添加它们。 这是我的输入代码:

int amount_of_sentences = 0;
struct Sentence *sentences = (struct Sentence *) malloc(amount_of_sentences * sizeof(struct Sentence));

struct Text text = {sentences, amount_of_sentences};

wchar_t symbol;
int buffer_size = 0;
wchar_t *buffer = (wchar_t *) malloc(buffer_size * sizeof(wchar_t));

bool sentence_begun = true;

while (true) {
    symbol = getwchar();

    if (symbol == '\n')
        break;

    if (sentence_begun && symbol == ' ') {
        sentence_begun = false;
        continue;
    }

    buffer = (wchar_t *) realloc(buffer, (++buffer_size) * sizeof(wchar_t));
    buffer[buffer_size - 1] = symbol;

    if (symbol == '.') {
        buffer[buffer_size] = '\0';

        text.amount_of_sentences++;
        text.sentences = (struct Sentence *) realloc(text.sentences, text.amount_of_sentences * sizeof(struct Sentence));
        text.sentences[text.amount_of_sentences - 1].amount_of_symbols = buffer_size;
        text.sentences[text.amount_of_sentences - 1].sentence = (wchar_t *) malloc(buffer_size * sizeof(wchar_t));
        text.sentences[text.amount_of_sentences - 1].sentence = buffer;

        buffer_size = 0;
        buffer = (wchar_t *) realloc(buffer, buffer_size * sizeof(wchar_t));
        sentence_begun = true;
    }
}

一切似乎都很好,但是当我尝试输出所有句子时,并不会全部显示出来,有些会被重复。

这是我的输出代码:

for (int i = 0; i < text.amount_of_sentences; i++) {
    wprintf(L"%ls\n", text.sentences[i].sentence);
}

输入输出示例:

adjsand. asdad.a.a. aaaa. adsa.


a.

adsa.
adsa.

此代码可能有什么问题,我应该更改什么?

2 个答案:

答案 0 :(得分:0)

首先,您的缓冲区太小1,并且不考虑终止'\0'。在程序顶部,执行以下操作:

int buffer_size = 1;
wchar_t *buffer = (wchar_t *) malloc(buffer_size * sizeof(wchar_t));
*buffer= '\0';

但是真正的问题在于:

    text.sentences[text.amount_of_sentences - 1].sentence =
                (wchar_t *) malloc(buffer_size * sizeof(wchar_t));
    text.sentences[text.amount_of_sentences - 1].sentence = buffer;

    buffer_size = 0;
    buffer = (wchar_t *) realloc(buffer, buffer_size * sizeof(wchar_t));

您为句子分配了内存,但是随后您用buffer指针覆盖了该指针。接下来,重置缓冲区大小并重新分配缓冲区。

分配不会复制缓冲区数据。为此,请执行以下操作:

    strcpy(text.sentences[text.amount_of_sentences - 1].sentence, buffer);

在这里也做:

buffer_size = 1;
buffer = (wchar_t *) realloc(buffer, buffer_size * sizeof(wchar_t));
*buffer= '\0';

答案 1 :(得分:0)

问题在这里。

    text.sentences[text.amount_of_sentences - 1].sentence = (wchar_t *) malloc(buffer_size * sizeof(wchar_t));
    text.sentences[text.amount_of_sentences - 1].sentence = buffer;

    buffer_size = 0;
    buffer = (wchar_t *) realloc(buffer, buffer_size * sizeof(wchar_t));

您用malloc分配了一个新句子,然后用buffer覆盖了它。这将泄漏内存。

然后将buffer分配给text.sentences[text.amount_of_sentences - 1].sentence,然后通过重新分配buffer释放该内存。

根据C标准...

  

realloc函数重新分配ptr指向的旧对象 ,并返回指向大小由size指定的新对象的指针。

text.sentences[text.amount_of_sentences - 1].sentence结束时指向已释放的内存。这将导致不确定的行为。

相反,指向buffer并分配一个新的buffer

    text.sentences[text.amount_of_sentences - 1].sentence = buffer;

    buffer_size = 0;
    buffer = malloc(buffer_size * sizeof(wchar_t));

其他一些注释...

As Paul noted,您需要为空字节分配一个额外的字节。

There's no need to cast the result of malloc or realloc

在堆栈上分配一个较大的缓冲区以读取输入(如果需要的话,增加它)是更简单,更快,更不容易出错的错误。然后将内容复制到适当大小的内存中。

I went ahead and coded up an improved version进行说明。如果这是家庭作业,请不要上交。