Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing an array with C

Tags:

I need to have an array of structs in a game I'm making - but I don't want to limit the array to a fixed size. I'm told there is a way to use realloc to make the array bigger when it needs to, but can't find any working examples of this.

Could someone please show me how to do this?

like image 493
Gary Avatar asked May 30 '10 03:05

Gary


People also ask

How do you resize an array?

The easiest way to resize an array is to create a new array with the desired size and copy the contents of the old array into it. We can delete the old array by setting it to null. For this, we use the arraycopy() method of the System class or copyOf() method of the java. util.

Can we change the size of an array at run time in C?

Size of Variable Length Arrays can be set at runtime, but we could not change their size once set. Unlike static arrays, Dynamic arrays in C are allocated on the heap and we could change their size in runtime.

Can we resize an array?

You cannot resize an array in C#, but using Array. Resize you can replace the array with a new array of different size.

Which method is used to resize an array?

Resize(T[], Int32) Method is used to resize the number of elements present in the array.


2 Answers

Start off by creating the array:

structName ** sarray = (structName **) malloc(0 * sizeof(structName *)); 

Always keep track of the size separately:

size_t sarray_len = 0; 

To increase or truncate:

sarray = (structName **) realloc(sarray, (sarray_len + offset) * sizeof(structName *)); 

Then set the size:

sarray_len += offset; 

Happy to help and hope that helps.

like image 98
Delan Azabani Avatar answered Sep 27 '22 16:09

Delan Azabani


The realloc function can be used to grow or shrink an array. When the array grows, existing entries retain their value and new entries are uninitialized. This may either grow in-place, or if that was not possible, it may allocate a new block elsewhere in memory (and behind the scenes, copy all the values over to the new block and free the old block).

The most basic form is:

// array initially empty T *ptr = NULL;  // change the size of the array ptr = realloc( ptr, new_element_count * sizeof *ptr );  if ( ptr == NULL ) {     exit(EXIT_FAILURE); } 

The multiplication is because realloc expects a number of bytes, but you always want your array to have the right number of elements. Note that this pattern for realloc means you do not have to repeat T anywhere in your code other than the original declaration of ptr.

If you want your program to be able to recover from an allocation failure instead of doing exit then you need to retain the old pointer instead of overwriting it with NULL:

T *new = realloc( ptr, new_element_count * sizeof *ptr );  if ( new == NULL ) {     // do some error handling; it is still safe to keep using     // ptr with the old element count } else {     ptr = new; } 

Note that shrinking an array via realloc may not actually return memory to the operating system; the memory may continue to be owned by your process and available for future calls to malloc or realloc.

like image 29
M.M Avatar answered Sep 27 '22 17:09

M.M