Re: Stack is slow than heap?
On Nov 7, 4:28 pm, BGB <cr88...@hotmail.com> wrote:
On 11/7/2011 9:47 AM, Victor Bazarov wrote:
On 11/7/2011 11:27 AM, Nephi Immortal wrote:
[..]
How big can stack hold on 32 bits machine? Should it hold between
500 megabytes and 1 gigabytes of memory?
It is usually controlled by the settings of your linker.
and usually *much* smaller...
on Windows, the default size if 4MB.
I see. the size of stack 4 MB answers my question.
After you write a function, do you prefer to store one buffer to hold
1,024 bytes or 4,096 bytes?
I prefer to store what my algorithm tells me to store.
> Should 4,096 bytes be sufficient to hold
buffer or small array in stack?
Depends on the use of the buffer.
yep.
sometimes 64 or 256 is more than sufficient...
and sometimes 4096 will still overflow.
it all depends on what one is doing, what data they are working with, ...
Make sense.
> Every time, function is called and it
takes CPU time to push empty buffer or small array into stack, do
algorithm, and pop used buffer or small array out of stack prior
exiting function is always slow.
Huh?
nothing really has to be "added" or "removed" from the stack, as it is
usually just a sliding pointer.
What option do you have? Do you prefer to create global variables to
hold buffer or small array into heap through reference or pointers
between function calls can be faster?
Use anything available to you. If the size of the buffer is known at th=
e
compile-time, AND it's small enough, you can declare it locally in some
function. It's *usually* as fast as, or faster than, dynamic allocation
of arrays. If the size of the buffer is *not* known until run-time, you
have a choice, use 'new[]/delete[]' and do it yourself or use standard
containers.
yep, among other options.
unless one has a good reason though, global variables are generally bad
practice though, as:
depending on use, they may not mix well with the use of threads;
they impede modularity;
they reduce flexibility;
Of course, global variable is bad idea if you choose to use thread.
If two or more threads are running at the same time while accessing
one global variable directly, why not create backup variable?
// global scope
char* pBuffer = 0;
void foo()
{
char* pTemp = pBuffer;
// do algorithm on second thread
pBuffer = pThread_Buffer2;
// do algorithm on third thread
pBuffer = pThread_Buffer3;
// do algorithm on fourth thread
pBuffer = pThread_Buffer4;
// restore buffer pointer prior exiting function
pBuffer = pTemp;
}
If I understand correctly, you want to create four threads and you
will need to create one copy of buffer to each thread. All threads
cannot share the same one buffer directly. Buffer class is a good
idea.
...
For example:
[..snip..]
Please answer one question. How big can stack hold up to 500
megabytes on 32 bits machine?
Please re-phrase the question. I don't understand what it is you're
trying to learn about the stack (I presume you're talking about the
process' memory for objects with automatic storage duration).
I think the OP was asking how big the stack is...
but, it does not hold anywhere near that much.