Runtime Stack allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam

    Runtime Stack allocation

    Is there a way in C++ to allocate a variable amount of stack space at
    run-time?

    void complex_maths_o peration(const int num_rows, double* vector)
    {
    double temp[num_rows];
    ....
    }

    The obvious answer is to use the heap... but if this isn't an option.



  • Jeff Schwab

    #2
    Re: Runtime Stack allocation

    Adam wrote:
    Is there a way in C++ to allocate a variable amount of stack space at
    run-time?
    >
    void complex_maths_o peration(const int num_rows, double* vector)
    {
    double temp[num_rows];
    ....
    }
    >
    The obvious answer is to use the heap... but if this isn't an option.
    Recurse. Your base case can depend on a run-time variable.

    Comment

    • Andrey Tarasevich

      #3
      Re: Runtime Stack allocation

      Adam wrote:
      Is there a way in C++ to allocate a variable amount of stack space at
      run-time?
      >
      void complex_maths_o peration(const int num_rows, double* vector)
      {
      double temp[num_rows];
      ....
      }
      >
      The obvious answer is to use the heap... but if this isn't an option.
      ...
      There's no C++-standard way to do that.

      If you don't mind going outside the limits of C++ standard, look up the 'alloca'
      function. It does exactly what you want but has some limitations, which might be
      (or might not be) critical in your case.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Adam

        #4
        Re: Runtime Stack allocation

        Thanks Andrey,

        Yes, 'alloca' style allocation is what I was looking for.
        This sort of behavor is supported by Ada and I've become fond of it.

        Adam

        "Andrey Tarasevich" <andreytarasevi ch@hotmail.comw rote in message
        news:OPqdnWZLEK axhFzanZ2dnUVZ_ ubinZ2d@comcast .com...
        Adam wrote:
        >Is there a way in C++ to allocate a variable amount of stack space at
        >run-time?
        >>
        >void complex_maths_o peration(const int num_rows, double* vector)
        >{
        > double temp[num_rows];
        > ....
        >}
        >>
        >The obvious answer is to use the heap... but if this isn't an option.
        >...
        >
        There's no C++-standard way to do that.
        >
        If you don't mind going outside the limits of C++ standard, look up the
        'alloca' function. It does exactly what you want but has some limitations,
        which might be (or might not be) critical in your case.
        >
        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Matthias Buelow

          #5
          Re: Runtime Stack allocation

          Adam wrote:
          Yes, 'alloca' style allocation is what I was looking for.
          This sort of behavor is supported by Ada and I've become fond of it.
          Don't allocate too much with alloca(), on most systems, there's a lot
          less stack space than heap space available with default settings.

          Comment

          Working...