Thanks.
Is it considered bad form to just brute force allocate enough memory in the first place (eg, 80 chars) ?
Thanks.
Is it considered bad form to just brute force allocate enough memory in the first place (eg, 80 chars) ?
No, not really (although too much is usually a bad thing(TM)). Memory is plenty and brute force allocation is faster. If you think it's enough, then most of the times, it's fine.
But if you don't know that it may be enough, dynamic may be better.
But in C, it's better to brute force to get rid of complexity of having to call free. In a big application, it can be difficult to know where to free the memory.
This reminds of a function I saw once, which had to assemble a string from some input parameters and then write it out to a file or something. Because the code had to be fast, the programmer created a 100 char array to assemble the string in. The code then checked whether the final string would be longer than 100 chars and if so, would allocate memory on the heap instead. The final string didn't have to be passed anywhere though, so there were no free issues.
Not that I condone that kind of thing
QuantumPete
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd