Thread stack sizing (why powers of 2?)

This is kind of just a general question out of curiosity.

Is there a functional reason to ensure that all stack sizes are powers of 2? I've seen it throughout many examples and also it was mentioned in DevAcademy, but no reasoning was given. It seems like if a large thread needs just a little more room, doubling its size every time is a waste of memory. I've seen some people suggest it's only necessary when using an MPU to detect writes outside of the intended memory region.

  • MPU only supports power-of-2 region sizes and very few regions, so you need a power-of-2 size when using it for stack protections.

    Otherwise feel free to explore stack size - but keep in mind that stack overflows can be difficult to detect correctly.

    I've used a 24KB stack for an MP3 decoder thread once - there are no other limits to stack size other than alignment.

Related