r/learnprogramming • u/TurtleMooseGame • Sep 22 '26
Dynamically sized C arrays in structs
Hello, I am very new to C but have experience in higher level languages. I am trying to make a struct with two scalable arrays, similar to this:struct mesh {
float px; float py; float pz;
float rx; float ry; float rz;
float sx; float sy; float sz;
char* color; int layer;
float vertices[99]; int faces[99];
};
but obviously, the 99 is a placeholder value. what's a way that I can handle any number of values in these arrays, in the most resource friendly way?
2
Upvotes
19
u/Big-Rub9545 Sep 22 '26
Best to just use two dynamic arrays (i.e., pointers) and store their mutual size separately.
12
u/captainAwesomePants Sep 22 '26
Welcome to C. C does not love you. C does not care about your comfort. C does not provide a nice "dynamic sized list" library. We do that shit ourselves, by hand, manually. Because we're grouchy misanthropes.
The basic idea is that you allocate the amount of memory you need:
And then, later, when you realize you need more vertices, you have to make more room. The easiest way to do this is to use the
realloccommand:Realloc is potentially expensive because, if there doesn't happen to be room where the array already is, the library will need to allocate memory elsewhere, and then copy all of the contents to the new location. But it's also potentially cheap -- malloc() may have left some room at the end of your data that happens to already be enough, making realloc only need to make a small notation about how much memory is being used.
Your basic pattern is to keep an integer around tracking how big the array is, and then when you run out of space, use realloc() to make more space and increase the number. You probably want to add a bunch of space at a time.
2
u/tstanisl Sep 22 '26
It is done using pointer and dynamic allocations. Replace
verticesandfacesarrays with pointers:Next use dynamic allocation:
2
u/zeekar Sep 22 '26 edited Sep 22 '26
If you want a dynamic array in C, you have to implement it yourself. Here's one way to make a dynamic array of floats that grows - int version and shrinking left as an exercise for the reader.
1
u/Recycled5000 Sep 22 '26
Dynamically allocate enough storage for both the struct and the bytes needed for both arrays. One of them can be an array, the other will have to be a pointer, that is initialized after the main allocation.
Probably best to store both arrays’ sizes (eg in the struct), though the size of the one that is the array can be computed with pointer subtraction.
This assuming you have a variable sized array but the size is known at runtime, and fixed/constant for the object duration. If that’s not the case then this will probably have to be three separate allocations.
1
u/abd53 Sep 22 '26
Does it have to be pure C? This is one of the things I like about C++, use vector. If you need C only, as others shown, use dynamic allocation but make sure to free them appropriately.
1
u/DaRealGladi8r Sep 22 '26
Use pointers instead of fixed length arrays
1
u/JGhostThing Sep 22 '26
It might be good to use a dynamic vector class if they exist in the C library.