T
Tom
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.
Thanks,
Tom.
C++? If I want to allocate a variable amount I need to use the OS APITom said:What I mean is why can I only allocate const size stuff on the stack in
That is the whole point os having a stack and a heap! If you want toTom said: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.
That is the whole point os having a stack and a heap! If you want to
allocated space, of amounts unknown at compile time, but known at
runtime, then you use the heap.
Java is the same.
Not sure about other langs.
~S
I have never used the _alloca() funtion. My programming experience withBell said:C++? If I want to allocate a variable amount I need to use the OS API
(Win32 in my case).
That is the whole point os having a stack and a heap! If you want to
allocated space, of amounts unknown at compile time, but known at
runtime, then you use the heap.
Java is the same.
Not sure about other langs.
~S
I don't agree. There is a good reason to do dynamic allocation on the
stack. When I allocate I do:
(1) automatic (stack) allocation, with objects of fixed (compile time)
sizes.
(2) dynamic (heap) allocation, with the new operator.
(3) dynamic heap allocation using _alloca (win32 only). I only do this with
speed sensitive code. So, instead of writing something like this:
void MyFunc( size_t s )
{
char* p = new char[ s ];
....
delete p;
}
I write something like this:
void MyFunc( size_t s )
{
char* p = (char*) _alloca( nBufSize + 1 );
....
}
So, am I missing something? Is this not faster?
Tom.
Shea Martin said:That is the whole point os having a stack and a heap! If you want to
allocated space, of amounts unknown at compile time, but known at
runtime, then you use the heap.
Java is the same.
Not sure about other langs.
I have never used the _alloca() funtion. My programming experience withstack. When I allocate I do:
(1) automatic (stack) allocation, with objects of fixed (compile time)
sizes.
(2) dynamic (heap) allocation, with the new operator.
(3) dynamic heap allocation using _alloca (win32 only). I only do this with
speed sensitive code. So, instead of writing something like this:
void MyFunc( size_t s )
{
char* p = new char[ s ];
....
delete p;
}
I write something like this:
void MyFunc( size_t s )
{
char* p = (char*) _alloca( nBufSize + 1 );
....
}
So, am I missing something? Is this not faster?
Tom.
the win32 api has been minimal.
But I have a fairly good guess what it
does:
_alloca is allocating a chunk of memory at startup, and then just giving
it out peice by peice, until it runs out, then grabs another large
chunk.
Bell said:C++? If I want to allocate a variable amount I need to use the OS API
(Win32 in my case).That is the whole point os having a stack and a heap! If you want to
allocated space, of amounts unknown at compile time, but known at
runtime, then you use the heap.
Java is the same.
Not sure about other langs.
~S
I don't agree. There is a good reason to do dynamic allocation on the
stack. When I allocate I do:
(1) automatic (stack) allocation, with objects of fixed (compile time)
sizes.
(2) dynamic (heap) allocation, with the new operator.
(3) dynamic heap allocation using _alloca (win32 only). I only do this with
speed sensitive code. So, instead of writing something like this:
void MyFunc( size_t s )
{
char* p = new char[ s ];
....
delete p;
}
I write something like this:
void MyFunc( size_t s )
{
char* p = (char*) _alloca( nBufSize + 1 );
....
}
So, am I missing something? Is this not faster?
Tom.
Tom said: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).
#include<iostream>cat main.cc
main.cc: In function `int main(int, char**)':g++ -Wall -o main main.cc
g++ -Wall -ansi -pedantic -o main main.cc
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.