You have two options to create stuff on the stack:
1) You can use a variable sized array. It may be simplest to move the main of the code into a function, and pass in the length as an argument:
Code:
void inner (size_t n)
{
uint32_t buffer[n];
// do stuff here with buffer
// when inner returns, the space is automatically returned to the stack
return;
}
void outer (void)
{
size_t n;
// calculate n here
// Use variable sized array
inner (n);
}
2) You can use the
__builtin_alloca function to allocate stuff on the stack:
Code:
void stuff (void)
{
size_t n;
// Calculate n
uint32_t *array = (uint32_t *) __builtin_alloca (sizeof (uint32_t) * n);
// Do stuff
// space for array will disappear when stuff exits.
return;
}