2 ms·
Generally speaking, you don't do this. You figure out the size you need and allocate it dynamically. If you don't know the size, you use a dynamically growing
by SamReidHughes 8y ago
Generally speaking, you don't do this. You figure out the size you need and allocate it dynamically. If you don't know the size, you use a dynamically growing array type (that does the same thing as C++'s std::vector). E.g. use asprintf (or implement your own using vsnprintf) as necessary. What you don't do is emulate the behavior of janky C programs that allocate a fixed size array. That's bad.
(Sometimes you have arrays where the size doesn't affect the correctness of your program. An example is if you're processing a file in chunks (e.g. to compute ROT-13), then chunk size will affect performance, but not correctness. You pick the size appropriate for your use case.)