Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase stack size to use alloca()?

This is two questions overlapping- I wish to try out alloca() for large arrays instead of assigning dynamically-sized arrays on the heap. This is so that I can increase performance without having to make heap allocations. However, I get the impression stack sizes are usually quite small? Are there any disadvantages to increasing the size of my stack so that I can take full advantage of alloca()? Is it The more RAM I have, the larger I can proportionally increase my stack size?

EDIT1: Preferably Linux

EDIT2: I don't have a specified size in mind- I would rather know how to judge what determines the limit/boundaries.

like image 771
mezamorphic Avatar asked Mar 25 '13 23:03

mezamorphic


People also ask

Can you increase the stack size?

You may need to increase the stack size if your program gets stack-overflow messages at runtime. You can also set the stack size by: Using the /STACK linker option. For more information, see /STACK (Stack allocations).

How do you increase stack?

To increase the stack size, define the new value in the -Xss setting in the JAVA_OPTS entry in your Tomcat start script file (/opt/netiq/idm/apps/tomcat/bin/setenv.sh or C:\NetIQ\idm\apps\tomcat\bin\setenv. bat). For example, to set the stack size to 4M, change the setting to -Xss4M.

How do you increase stack size in code blocks?

For codeblocks follow: go to settings -> compiler. go to linker settings tab and add “-Wl,–stack,SIZE” in the other linker options section (i have set 512mb)


1 Answers

Stack sizes are (by default) 8MB on most unix-y platforms, and 1MB on Windows (namely, because Windows has a deterministic way for recovering from out of stack problems, while unix-y platforms usually throw a generic SIGSEGV signal).

If your allocations are large, you won't see much of a performance difference between allocating on the heap versus allocating on the stack. Sure, the stack is slightly more efficient per allocation, but if your allocations are large the number of allocations is likely to be small.

If you want a larger stack-like structure you can always write your own allocator which obtains a large block from malloc and then handles allocation/deallocation in a stack-like fashion.

#include <stdexcept>
#include <cstddef>

class StackLikeAllocator
{
    std::size_t usedSize;
    std::size_t maximumSize;
    void *memory;
public:
    StackLikeAllocator(std::size_t backingSize)
    {
        memory = new char[backingSize];
        usedSize = 0;
        maximumSize = backingSize;
    }
    ~StackLikeAllocator()
    {
        delete[] memory;
    }
    void * Allocate(std::size_t desiredSize)
    {
        // You would have to make sure alignment was correct for your
        // platform (Exercise to the reader)
        std::size_t newUsedSize = usedSize + desiredSize;
        if (newUsedSize > maximumSize)
        {
            throw std::bad_alloc("Exceeded maximum size for this allocator.");
        }

        void* result = static_cast<void*>(static_cast<char*>(memory) + usedSize);
        usedSize = newUsedSize;
        return result;
    }

    // If you need to support deallocation then modifying this shouldn't be
    // too difficult
}
like image 187
Billy ONeal Avatar answered Nov 10 '22 00:11

Billy ONeal