473,794 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Feb 23 '08 #1
4 2206
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.
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_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
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.comw rote 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_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
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
30101
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 part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
9
4965
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
8214
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 allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new DestinationAddress(); // code 1
25
2571
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 it and I'd rather not waste space or dynamically allocate on the heap (since it doesn't need to persist). Now I'm planning on utilizing inline assembly to modify the stack pointer directly to allocate the space I need. Here's an example:
6
1701
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!= "") dosomething();
16
4451
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 in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
5
37162
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
3839
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 this stored in stack. If yes whether it will be deleted on exiting from the function. is dynamic memory allocation needed for this purpose
87
5577
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 place? I can see two definite disadvantages with this: 1) deeply nested recursive calls to a function (especially if it defines
10
2311
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 that utilizes it and I'd rather not waste space or dynamically allocate on the heap (since it doesn't need to persist). Now I'm planning on utilizing inline assembly to modify the stack pointer directly to allocate the space I need.
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10161
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.