473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Runtime Stack allocation

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.

Feb 23 '08 #1
4 2187
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.
Feb 23 '08 #2
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
Feb 24 '08 #3
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" <an**************@hotmail.comwrote in message
news:OP******************************@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
Feb 24 '08 #4
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.

Feb 25 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
25
by: Brian Lindahl | last post by:
I'm using a temporary buffer to transfer some data and would rather not allocate it on the heap. The problem is that the size of the buffer is only known upon entry into the function that utilizes...
6
by: Chance Hopkins | last post by:
Can anyone tell me in a few words, is one of the following better and if so which one and why: if(varone != null) if(varone != "") dosomething(); if(varone != null && varone!= "")...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
5
by: sunny | last post by:
Hi All Is there any way to determine stack and heap size, during runtime. i.e can we predict stack overflow. etc
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
87
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
10
by: kid joe | last post by:
Hi all, I'm using a temporary buffer to transfer some data and would rather not allocate it on the heap. The problem is that the size of the buffer is only known upon entry into the function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.