gpt4 book ai didi

c - 结构体数组内存 realloc stderr

转载 作者:行者123 更新时间:2023-11-30 15:45:09 24 4
gpt4 key购买 nike

当我将元素放入其中时,我试图在空间不足时重新分配结构数组,但我不断收到重新分配的标准错误。 struct 数组最终将包含 235,000 个元素。当我将初始启动大小设置为 100,000 时,我在尝试重新分配时收到了 stderr。如果我将初始启动大小设置为 300,000,它不会给出错误,因为它永远不会到达 realloc 语句。

#define _XOPEN_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#define BUFFERLEN 200
#define START_SIZE 100000
#define GROW 1000
#define TRUE 1
#define FALSE 0

typedef struct{
char *forw;
char *back;
} word;

typedef struct{
char *entry;
} single_words;


FILE *words;
/*-------------Function Prototypes------------*/
void reverse(char* string, char* revstr, int len);
int search_struct(char* find, word* words, int length);
int compare(const void* eventa, const void* eventb);
int length(char* string);


int main(void)
{
char *buffer;
int letter_index[26];
char alpha[] = "abcdefghijklmnopqrstuvwxyz";
int i=0, found = FALSE, strlen=0, letter=0;
word *word_storage;
single_words *output_storage;
int num_words = 0, size = 0;
int num_output = 0, output_size = 0;

/*buffer for the input strings of the words in the input file*/
buffer = (char*) malloc (sizeof(char)*BUFFERLEN);
if(!buffer){
fprintf(stderr, "Error in buffer string mem alloc\n");
exit(1);
}

/*Initializing the array of structs to store the forward and reverse of each word*/
word_storage = (word*) malloc (sizeof(word)*START_SIZE);
if(!word_storage){
fprintf(stderr, "Error in word_storage string mem alloc\n");
exit(1);
}
size = START_SIZE;

/*Initializing the array of structs for the output*/
output_storage = (single_words*) malloc (sizeof(single_words)*START_SIZE);
if(!output_storage){
fprintf(stderr, "Error in output_storage mem alloc\n");
exit(1);
}
output_size = START_SIZE;

/*Set the letter index 0(which is a) to the first character*/
letter_index[0] = 0;

words = fopen("words", "r");

/*Read the words(forward and reverse) in from stdin into the word_storage*/
while(fgets(buffer, BUFFERLEN, words) != NULL){
buffer = strtok(buffer, "\n");
strlen = length(buffer);
if (num_words < size){
/*Allocate memory for the forward and reverse strings*/
word_storage[num_words].forw = (char *) malloc (sizeof(char) * strlen);
if(!word_storage[num_words].forw){
free(word_storage[num_words].forw);
fprintf(stderr, "word_storage forward string malloc was unsuccessful");
exit(1);
}
word_storage[num_words].back = (char *) malloc (sizeof(char) * strlen);
if(!word_storage[num_words].back){
free(word_storage[num_words].back);
fprintf(stderr, "word_storage forward string malloc was unsuccessful");
exit(1);;
}

/*Store the forward and reverse in the strings*/
strncpy(word_storage[num_words].forw, buffer, strlen);
reverse(word_storage[num_words].forw, word_storage[num_words].back, strlen);
printf("%d: %s %s\n", num_words, word_storage[num_words].forw, word_storage[num_words].back);

/*Increment the letter if it changes*/
if(word_storage[num_words].forw[0] != alpha[letter]){
letter++;
letter_index[letter] = num_words + 1;
}
num_words++;
}
else{
/*Increase the size of word_storage*/
word_storage = (word*) realloc (word_storage, sizeof(word) * size * GROW);
if(!word_storage){
free(word_storage);
fprintf(stderr, "Error in word_storage realloc string mem realloc\n");
exit(1);
}
size = size * GROW;
}
}


return 0;
}

此处发生重新分配错误:

word_storage = (word*) realloc (word_storage, sizeof(word) * size * GROW);
if(!word_storage){
free(word_storage);
fprintf(stderr, "Error in word_storage realloc string mem realloc\n");
exit(1);
}
size = size * GROW;

最佳答案

因此,您最初将 size 设置为 START_SIZE,即 100,000。然后,当您用完它时,您尝试分配 sizeof(word) * size * GROW 字节。 sizeof(word) 大概是 16 个字节;我们知道 size 是 100000,GROW 是 1000。因此,这足以容纳 100,000,000 个条目,您说您将使用其中的 235,000 个条目。看来分配有点慷慨了。

100,000,000 个条目的总空间为 1,600,000,000 字节。这似乎很多,尽管现在许多台式机都可以处理这个问题。但 realloc 失败似乎并不太令人惊讶。

也许你应该让 GROW 更合理,比如 2。

顺便说一句,一旦确定 word_storage 为 NULL,调用 free(word_storage) 就没有意义了。它没有任何害处,因为 free(NULL) 是一个无操作,但出于同样的原因它也没有任何好处。

关于c - 结构体数组内存 realloc stderr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19206005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com