重新分配作业帮助
对于分配,我必须做的部分工作涉及使用 malloc
和 realloc
。我首先创建一个二维字符数组,维度是行数和字符数。然后,我使用 malloc 分配足够的内存来存储某个文件的输入。使用 fgets
我一次读取一行,并将其存储在数组中。这部分工作得很好(或者我认为是这样)。当我尝试根据需要为更多行重新分配内存时,问题就出现了。程序流程应该是这样的:
创建一个 50 行的字符数组,每行 80 个字符(工作)
使用 fgets
一次读取一行并将其保存到数组中(工作) )
当读取了 50 行时,重新分配数组以允许 100 行(不起作用)
根据需要继续重新分配(不起作用)
这就是我到目前为止所拥有的(至少是它的核心,我省略了不相关的代码):
#define NUMBER_OF_LINES 50
#define CHARACTERS_PER_LINE 80
FILE *inputFile = fopen("some.text", "r");
char **lines;
lines = malloc(NUMBER_OF_LINES * sizeof(*lines));
int i;
for (i = 0; i < NUMBER_OF_LINES; i++)
*(lines+i) = malloc(CHARACTERS_PER_LINE * sizeof(char));
int linesRemaining = NUMBER_OF_LINES;
int reallocCount = 1;
i = 0;
while (!feof(inputFile)) {
if (!linesRemaining) {
reallocCount++;
lines = realloc(lines, (NUM_OF_LINES * reallocCount) * sizeof(*lines));
linesRemaining = NUM_OF_LINES;
}
fgets(*(lines+i), CHARS_PER_LINE, inputFile);
i++;
linesRemaining--;
}
我的直觉告诉我问题出在 realloc
上,所以我将解释我认为它正在做什么。
realloc(lines, (NUM_OF_LINES * reallocCount) * sizeof(*lines));
第一个参数,lines
,是我想要重新分配一定量内存的指针。 NUM_OF_LINES
是我想要增加大小的数量。我将其乘以 reallocLinesCount
,这是一个计数器,用于跟踪我应该拥有多少组 50 行。 sizeof(*lines)
部分是指向 char
的指针的大小。
感谢您的阅读,非常感谢您的帮助:)
编辑:谢谢大家的回复;我现在没有时间阅读所有答案,但是一旦这个迫在眉睫的截止日期过去,您的所有答案将被更彻底地阅读和理解:D
For an assignment, part of what I have to do involves the use of malloc
and realloc
. I first create a 2D array of chars, the dimensions being the number of lines and the number of characters. I then use malloc
to allocate enough memory to store input from some file. Using fgets
I read one line in at a time, and store it in the array. This part works fine (or so I think). The problem comes in when I try to reallocate memory for more lines if need be. The program flow is supposed to be like this:
Create a character array of 50 lines, with 80 characters per line (working)
Use fgets
to read one line at a time and save it to the array (working)
When 50 lines have been read, reallocate the array to allow for 100 lines (not working)
Keep reallocating as need be (not working)
This is what I have so far (the core of it at least, I omitted irrelevant code):
#define NUMBER_OF_LINES 50
#define CHARACTERS_PER_LINE 80
FILE *inputFile = fopen("some.text", "r");
char **lines;
lines = malloc(NUMBER_OF_LINES * sizeof(*lines));
int i;
for (i = 0; i < NUMBER_OF_LINES; i++)
*(lines+i) = malloc(CHARACTERS_PER_LINE * sizeof(char));
int linesRemaining = NUMBER_OF_LINES;
int reallocCount = 1;
i = 0;
while (!feof(inputFile)) {
if (!linesRemaining) {
reallocCount++;
lines = realloc(lines, (NUM_OF_LINES * reallocCount) * sizeof(*lines));
linesRemaining = NUM_OF_LINES;
}
fgets(*(lines+i), CHARS_PER_LINE, inputFile);
i++;
linesRemaining--;
}
My gut tells me the problem is with the realloc
, so I'll explain what I think it's doing.
realloc(lines, (NUM_OF_LINES * reallocCount) * sizeof(*lines));
The first argument, lines
, is the pointer I would like to reallocate a certain amount of memory. NUM_OF_LINES
is the amount I would like to increase the size by. I multiply this by reallocLinesCount
, which is a counter that keeps track of how many sets of 50 lines I ought to have. The sizeof(*lines)
part is the size of a pointer to a char
.
Thank you for reading and any help is greatly appreciated :)
EDIT: thank you all for the responses; I do not have time right now to read all of the answers right now, but all of your answers will be more thoroughly read and understood once this imminent deadline has passed :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的座右铭是:“说出你的意思”。就您而言,您的意思是在数组不足以容纳数据时扩大数组。
My motto is: "say what you mean". In your case, you MEAN to enlarge your array when it's not big enough to hold your data.
realloc() 经常会发现没有足够的可用空间来就地扩展现有数组;在这种情况下,它将创建一个指定大小的全新数组,将旧数组的内容复制到新数组,释放旧数组,并返回指向新数组的指针。所以你应该这样写
(
oldLines
的目的是保留原始指针,以防realloc()
耗尽内存并返回NULL
,如下根据@Brian L 的提示)。realloc()
will often find out that there is not enough available room to expand the existing array in-place; in that case, it will create an entirely new array of the specified size, copy the contents of the old array to the new one, deallocate the old array, and return a pointer to the new one. So you should write(the purpose of
oldLines
is to keep the original pointer in caserealloc()
runs out of memory and returnsNULL
, as per @Brian L's tip).这就是你应该如何重新分配:
阅读重新分配参考了解详细信息。
编辑
您需要为每条新线分配更多分配。
This is how you should realloc:
Read realloc reference for details.
EDIT
You need more allocation for each new lines.
您将更多的指针分配给行,而不是行本身。它位于代码的开头:
因此,在为每行分配行数后,您为该行本身分配空间。当您重新分配时,您忘记对新线路执行此操作。
You are allocating more pointers to lines but not the lines themselves. It is in your code at the beginning:
So after you allocated your number of lines for each line you allocate the space for the line itself. You forgot to do this for the new lines when you reallocate.
我们首先看看
realloc()
是如何工作的。它返回一个指向new的指针成功时内存,失败时
NULL
。失败时,它不会触摸旧内存,成功后,复制后就
free()
了您的数据到新的地方。
所以,安全使用
realloc()
的方法是:所以,首先是你错误地使用了
realloc()
。你不应该说
x = realloc(x, ...);
,因为如果realloc()
失败,您将
x
分配给NULL
,并且旧内存将丢失。这是内存泄漏。
现在,解决你的问题。假设您已成功阅读
NUMBER_OF_LINES
行。现在您想为额外的空间腾出空间NUMBER_OF_LINES
行。你会这样做:最后一点:使用
while 几乎总是错误的 (!feof(fp))
从文件中读取行。
Let's first see how
realloc()
works. It returns a pointer to newmemory on success, and
NULL
on failure. On failure, it doesn'ttouch the old memory, and on success, it
free()
's it, after copyingyour data to the new place.
So, the way to use
realloc()
safely is:So, the first thing is that you are using
realloc()
incorrectly.You should never say
x = realloc(x, ...);
, because ifrealloc()
fails, you assign
x
toNULL
, and the old memory is lost. This isa memory leak.
Now, on to your problem. Let's say you have successfully read
NUMBER_OF_LINES
lines. Now you want to make room for an additionalNUMBER_OF_LINES
lines. You would do:One final note: it's almost always wrong to use
while (!feof(fp))
to read lines from a file.