Re: Why are variables stored on the stack?
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Sat, 15 Mar 2008 17:46:02 -0500
Malcolm McLean wrote:
"CJ" <cj@xxxxxxxxxx> wrote in message
2) I believe the argument about it being more efficient to use
the stack than the heap is spurious - if I recall, both are O(N)
data structures.
A stack is O(1) to allocate a chunk, and chunk sizes can be
variable. O(N) to fill the chunk with useful data, where N is
chunk size. A heap is O( f(N) ) to allocate a chunk, where f(N)
is some complex and platform dependent function of the number of
chunks allocated. Typically it will be about logarithmic,
certainly under N. You can look up broken stick distributions
and the like if you are interested in problems of fragmentation
and how they impact on block search.
A heap is O(N) to fill with useful data, where N is the chunk
size.
So you are not really correct, unless chunk size is very large
in comparision to the number of blocks allocated, in which case
that term will dominate.
You are in error about heaps. For an example where all calls to
malloc, realloc, free are O(1) see nmalloc.zip, available at:
<http://cbfalconer.home.att.net/download/>
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com
.
- References:
- Why are variables stored on the stack?
- From: CJ
- Re: Why are variables stored on the stack?
- From: CJ
- Re: Why are variables stored on the stack?
- From: Malcolm McLean
- Why are variables stored on the stack?
- Prev by Date: Re: Why are variables stored on the stack?
- Next by Date: Re: program for reading a file
- Previous by thread: Re: Why are variables stored on the stack?
- Next by thread: Re: Why are variables stored on the stack?
- Index(es):
Relevant Pages
|
|