[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Q: Unused local variables
[Thread Prev] | [Thread Next]
- Subject: Re: Q: Unused local variables
- From: Benoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>
- Date: Wed, 12 Aug 2026 09:37:55 +0200
- To: user@xxxxxxxxxxxxxxxxxxxxxx
Le 12/08/2026 à 09:10, BB a écrit :
Does having unused local variables in a method actually have an effect?I've never worried about it until today. My theory is that the only effect would be the execution of the command to allocate some space on the local heap when the method is activated, which would then be freed when the method exits. That is, approximately 1 malloc and 1 free.But is this what actually happens? Or since the variable is unused is the compiler smart enough to ignore it altogether?regards b
If your unused variables are not initialized at all, then they just take a stack slot.
And there is no malloc() / free() involved in stack allocation.In Gambas, the entire stack is allocated by a memory mapping of its maximum size ("man mmap"). That means that the memory is only actually allocated at first use, i.e. as the stack is growing (at least on Linux).
I admit that the compiler could ignore unused local variables so that nothing is allocated at all. But he is dumb. :-)
Regards, -- Benoît Minisini.
| Q: Unused local variables | BB <adamnt42@xxxxxxxxx> |