I'm working on a school project where I'm building a very basic shell in C with a friend. I've dubbed it a Bash clone, but honestly, it's only got about 5% of the features of Bash. So far, while handling token lexing and parsing, we're using malloc at least once for each token and once for each AST code node, which results in around 300 allocations for processing 10-20 commands. We haven't even started on the execution parts, variable expansion, or anything else yet. Is this allocation frequency excessive, or does it seem okay? If you need more details, just let me know!
1 Answer
Honestly, just focus on getting it to work first, then you can optimize later. You could try calculating the memory you'll need ahead of time and group your data to reduce the number of allocations if you're interested in that. But for now, get that basic functionality down!

That's the plan! But since I'm still new to C, I’m curious about what others think about the number of allocations I've used. 300 seems high to me, but I want to know if it's something I really need to worry about or not. For reference, it currently uses about 7-8 kilobytes per average of 20-30 commands, with no memory leaks because everything's freed after execution.