|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Is there a way in C++ to allocate a variable amount of stack space at
run-time? void complex_maths_operation(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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Adam wrote:
> Is there a way in C++ to allocate a variable amount of stack space at > run-time? Well, there is the class std::stack, but I doubt that you are referring to that. However, other than that, C++ doesn't define anything called "stack". |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Adam wrote:
> Is there a way in C++ to allocate a variable amount of stack space at > run-time? > > void complex_maths_operation(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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Adam wrote:
> Is there a way in C++ to allocate a variable amount of stack space at > run-time? > > void complex_maths_operation(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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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" <andreytarasevich@hotmail.com> wrote in message news:OPqdnWZLEKaxhFzanZ2dnUVZ_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_operation(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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|