Skip to main content
fixed code, if-then-else ordering
Source Link
David R Tribble
  • 12.2k
  • 5
  • 46
  • 55

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.

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*1024)
    {
        unsigned char   arr[1*1024*1024];
        return bar(arr, size);
    }
    else (if size <= ...other smaller block size...)
    {
        unsigned char   arr[...smaller size...];
        return bar(arr, size);
    }
    else
     {
        // Assume 64 KB
        unsigned char   arr[64*1024];
        return bar(arr, size);
    }
}

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.

If you don't have VLAs or alloca(), here is an extremely kludgy, but portable, stack-based technique:

int foo(int size)
{
    if (size <= 64*1024)
    {
        unsigned char   arr[64*1024];
        return bar(arr, size);
    }
    else if (size <= 1*1024*1024)
    {
        unsigned char   arr[1*1024*1024];
        return bar(arr, size);
    }
    else if (size <= 64*1024*1024)
    {
        unsigned char   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.

Source Link
David R Tribble
  • 12.2k
  • 5
  • 46
  • 55

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*1024)
    {
        unsigned char   arr[1*1024*1024];
        return bar(arr, size);
    }
    else (if size <= ...other smaller block size...)
    {
        unsigned char   arr[...smaller size...];
        return bar(arr, size);
    }
    else
    {
        // Assume 64 KB
        unsigned char   arr[64*1024];
        return bar(arr, size);
    }
}

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.