Search code examples
c++c++11stack

Maximum size for stack object


I'd like to provide an API with the following C++ struct:

struct fixed_string64 {
  char array[64];
};
typedef fixed_string64 st64_t;

Most people tell me that it is generally not a good idea to do anything that eats up lot of stack space, but then how much is "lot of" ?

In C++11, do we have something like is_stack_hungry<st64_t>::value ?


Solution

  • Size of the stack is implementation defined, and I am not aware of the standard way to get that information from the system/or compiler. However, most of the compilers should allow you to set the stack size (like --stack in gcc).

    However, "making the stack as large as you want" is definitely not my advice - if you need that, then you're doing something wrong.

    For example, in your example, you could just provide constructor and destructor in order to allocate and free memory.

    struct fixed_string64 {
      char* array;
    
      fixed_string64(){
        array = new char[64];
      }
    
    
      ~fixed_string64(){
        delete [] array;
      }
    };