C语言 C: 用 malloc 扩展数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2748036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: Expanding an array with malloc
提问by Mal Ock
I'm a bit new to malloc and C in general. I wanted to know how I can, if needed, extend the size of an otherwise fixed-size array with malloc.
总的来说,我对 malloc 和 C 有点陌生。如果需要,我想知道如何使用 malloc 扩展其他固定大小的数组的大小。
Example:
例子:
#define SIZE 1000
struct mystruct
{
int a;
int b;
char c;
};
mystruct myarray[ SIZE ];
int myarrayMaxSize = SIZE;
....
if ( i > myarrayMaxSize )
{
// malloc another SIZE (1000) elements
myarrayMaxSize += SIZE;
}
- The above example should make clear what I want to accomplish.
- 上面的例子应该清楚我想要完成什么。
(By the way: I need this for an interpreter I write: Work with a fixed amount of variables and in case more are needed, just allocate them dynamically)
(顺便说一句:我写的解释器需要这个:使用固定数量的变量,如果需要更多变量,只需动态分配它们)
采纳答案by i_am_jorf
Use realloc, but you have to allocate the array with malloc first. You're allocating it on the stack in the above example.
使用realloc,但您必须先使用 malloc 分配数组。在上面的示例中,您在堆栈上分配它。
size_t myarray_size = 1000;
mystruct* myarray = malloc(myarray_size * sizeof(mystruct));
myarray_size += 1000;
mystruct* myrealloced_array = realloc(myarray, myarray_size * sizeof(mystruct));
if (myrealloced_array) {
myarray = myrealloced_array;
} else {
// deal with realloc failing because memory could not be allocated.
}
回答by R Samuel Klatchko
You want to use realloc (as other posters have already pointed out). But unfortunately, the other posters have not shown you how to correctly use it:
您想使用 realloc (正如其他海报已经指出的那样)。但不幸的是,其他海报并没有向您展示如何正确使用它:
POINTER *tmp_ptr = realloc(orig_ptr, new_size);
if (tmp_ptr == NULL)
{
// realloc failed, orig_ptr still valid so you can clean up
}
else
{
// Only overwrite orig_ptr once you know the call was successful
orig_ptr = tmp_ptr;
}
You need to use tmp_ptrso that if reallocfails, you don't lose the original pointer.
您需要使用,tmp_ptr以便在realloc失败时不会丢失原始指针。
回答by Steve Jessop
No, you can't. You can't change the size of an array on the stack once it's defined: that's kind of what fixed-size means. Or a global array, either: it's not clear from your code sample where myarrayis defined.
不,你不能。一旦定义了堆栈上的数组,您就无法更改它的大小:这就是固定大小的含义。或者全局数组,或者:从您的代码示例中不清楚在哪里myarray定义。
You could malloca 1000-element array, and later resize it with realloc. This can return you a new array, containing a copy of the data from the old one, but with extra space at the end.
您可以创建malloc一个包含 1000 个元素的数组,然后使用realloc. 这可以返回一个新数组,其中包含旧数组的数据副本,但末尾有额外的空间。
回答by pm100
a) you did not use malloc to create it so you cannot expand with malloc. Do:
a) 你没有使用 malloc 来创建它,所以你不能用 malloc 扩展。做:
mystruct *myarray = (mystruct*)malloc(sizeof( mystruct) *SIZE);
b) use realloc (RTM) to make it bigger
b) 使用 realloc (RTM) 使其更大

