If you don't have VLAs or alloca(), here is an extremely kludgy, but portable, stack-based technique:
int foo(int size)
{
if (size <= 1*1024*102464*1024)
{
unsigned char arr[1*1024*1024];arr[64*1024];
return bar(arr, size);
}
else (if (size <= ...other smaller block size...1*1024*1024)
{
unsigned char arr[...smaller size...];arr[1*1024*1024];
return bar(arr, size);
}
else
if (size <= {64*1024*1024)
// Assume 64 KB{
unsigned char arr[64*1024];arr[64*1024*1024];
return bar(arr, size);
}
else
return -1; // Assume it's too big
}
int bar(unsigned char arr[], int size)
{
...your code goes here...
}
int maincode(int size)
{
// Invoke bar() indirectly, allocating an array
// on the stack of at least 'size' bytes
return foo(size);
}
I don't particularly recommend this technique, but it will give you differently-sized blocks of memory allocated on the stack instead of the heap.