C:使用malloc扩展数组

时间:2022-09-17 03:15:29

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)

(顺便说一句:我需要这个用于我编写的解释器:使用固定数量的变量,如果需要更多变量,只需动态分配它们)

4 个解决方案

#1


16  

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.
   }

#2


13  

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_ptr so that if realloc fails, you don't lose the original pointer.

您需要使用tmp_ptr,以便在realloc失败时,您不会丢失原始指针。

#3


6  

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 myarray is defined.

不,你不能。一旦定义了数组,就无法更改数组的大小:这就是固定大小的含义。或者是全局数组:从代码示例中不清楚myarray的定义位置。

You could malloc a 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调整它的大小。这可以返回一个新数组,其中包含旧数据的副本,但最后会有额外的空间。

#4


1  

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)使其更大

#1


16  

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.
   }

#2


13  

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_ptr so that if realloc fails, you don't lose the original pointer.

您需要使用tmp_ptr,以便在realloc失败时,您不会丢失原始指针。

#3


6  

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 myarray is defined.

不,你不能。一旦定义了数组,就无法更改数组的大小:这就是固定大小的含义。或者是全局数组:从代码示例中不清楚myarray的定义位置。

You could malloc a 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调整它的大小。这可以返回一个新数组,其中包含旧数据的副本,但最后会有额外的空间。

#4


1  

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)使其更大