Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Heap vs Stack allocations

Reply
Thread Tools

Heap vs Stack allocations

 
 
CBFalconer
Guest
Posts: n/a
 
      01-23-2004
MSG wrote:
> <(E-Mail Removed)> wrote in message
> > MSG wrote:
> >
> > > void f1(int n) { vector<int> x(n); /* C++ */ }
> > > void f2(int n) { int x[n]; /* C99 only */ }
> > > void f3(int n) { int* x = new int[n]; /* C++ */ delete [] x; }
> > > void f4(int n) { int* x = (int*)malloc(n*sizeof(int));
> > > /*...*/ free(x); }
> > >
> > > In all of these cases, it makes sense for the compiler to
> > > allocate x on the stack instead of the heap.

> >
> > Why?

>
> LOL! A fella from JPL should know this, so listen good

.... snip ...

You are obviously not acquainted with our resident Trollsdale.
His advice and queries are normally nonsense, and even his quotes
are not to be trusted. He edits them, and totally changes their
meaning, without any annotation. I seriously doubt that he has
any connection with jpl or nasa, his alleged knowledge is just too
erroneous. However he may be a janitor there (which is perfectly
respectable, just not normally computer knowledgeable).

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-23-2004
Jack Klein wrote:

> The term "free store" has meaning in C++.
> It has no such meaning in C,


Nonsense!
It has exactly the same meaning that it has in C++.

> not being defined by the [ANSI/ISO C] language standard at all.


Nonsense!

void free(void *ptr);

is defined by ANSI/ISO C[89]9.

> The C standard does not specify where allocated memory comes from


Really?
Can a C program "steal" the memory that is allocated?

 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      01-23-2004
On Fri, 23 Jan 2004 09:31:43 GMT, in comp.lang.c , CBFalconer
<(E-Mail Removed)> wrote:

>meaning, without any annotation. I seriously doubt that he has
>any connection with jpl or nasa, his alleged knowledge is just too
>erroneous.


There apparently /is/ an ERTisdale there (his name has appeared on
some research papers). But that doesn't mean that /this/ ERT works
there, or even that this guy /is/ ERT. Its easy enough to fake up the
headers. After all, for all I know, you're a lesbian trucker from
Vermont called Shirley, and not a mongolian cat-skinner living in a
squat in Eastbourne at all.

Myself however, I believe he is the real thing. I met some VERY
self-opinionated research people when I was a postgrad, some of whom
thought that because they were (say) experts in the properties of
plasma in 30T torsional magnetic fields, they were naturally experts
in any other field of human endeavour. Given that many of them had
failed to master the art of washing their beards, wore shoes with
velcro and (aged 25) took all their laundry home to their mum every 8
weeks....

>However he may be a janitor there (which is perfectly
>respectable, just not normally computer knowledgeable).


You obviously don't read Dilbert.....

Never *ever* sass the garbage man....
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
MSG
Guest
Posts: n/a
 
      01-23-2004
Joona I Palaste <(E-Mail Removed)> wrote in message news:<buqlb3$1fl$(E-Mail Removed)>...
> MSG <(E-Mail Removed)> scribbled the following
> on comp.lang.c:
> > "E. Robert Tisdale" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> >> MSG wrote:
> >>
> >> > void f1(int n) { vector<int> x(n); /* C++ */ }
> >> >
> >> > void f2(int n) { int x[n]; /* C99 only */ }
> >> >
> >> > void f3(int n) { int* x = new int[n]; /* C++ */ delete [] x; }
> >> >
> >> > void f4(int n) { int* x = (int*)malloc(n*sizeof(int));
> >> > /*...*/ free(x); }
> >> >
> >> > In all of these cases, it makes sense for the compiler to allocate x
> >> > on the stack instead of the heap.
> >>
> >> Why?
> >>

>
> > LOL! A fella from JPL should know this, so listen good

>
> > 1. Stack allocation/deallocation is MUCH faster than heap -
> > approximately O(1) vs O(log(n)), where n is the number of allocated
> > pieces, IIRC.

>
> > 2. Heap can become fragmented (unless you move memory blocks around,
> > which C/C++ does not usually do AFAIK), so allocating can fail long
> > before your memory is exhausted, OTOH stack allocation will always
> > succeed as long as you have enough stack left.

>
> > BTW, #2 can be a source of "interesting" behavior when your program
> > works fine for a while, but then starts acting funny. This however
> > happens only to software of a certain degree of quality, most C/C++
> > programs (*) will crash long before heap fragmentation becomes an
> > issue. LOL.

>
> Did you *read* the other replies? Neither C or C++ specifies that a
> "stack" or a "heap" even exists. Trollsdale is only trying to

^^^^^^^^^^

It's not very nice of you. This is an international forum, so you need
to learn to be more tolerant of others' opinions, including opinions
as to what is on-topic.

"Love thy neighbor" - Mr. Fix It

> intentionally ignore this and discuss implementation details on
> comp.lang.c. If you want to discuss a particular implementation, find
> a newsgroup for that implementation.

 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      01-23-2004
On 23 Jan 2004 14:49:24 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (MSG) wrote:

>It's not very nice of you. This is an international forum, so you need
>to learn to be more tolerant of others' opinions, including opinions
>as to what is on-topic.


No. Topicality does not depend on nationality.

--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-24-2004
Martin Dickopp wrote:

> (E-Mail Removed) (MSG) writes:
>
>> void f1(int n) { vector<int> x(n); /* C++ */ }
>>
>> void f2(int n) { int x[n]; /* C99 only */ }
>>
>> void f3(int n) { int* x = new int[n]; /* C++ */ delete [] x; }
>>
>> void f4(int n) { int* x = (int*)malloc(n*sizeof(int));
>> /*...*/ free(x); }
>>
>>
>> In all of these cases it makes sense for the compiler to allocate x
>> on the stack instead of the heap. However, AFAIK, only in case of f2
>> the compiler is required to do so.

>
> No, neither in C nor in C++ a heap and/or stack is even required to
> exist. How/where the implementation allocates memory is unspecified.


I find it quite strange that C++ doesn't require a stack to exist, but
OTOH dictates that when an exception occurs, "stack unwinding" is done.
This term doesn't have a meaning if there is no stack, does it?

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-24-2004
E. Robert Tisdale wrote:

> Jack Klein wrote:
>
>> The term "free store" has meaning in C++.
>> It has no such meaning in C,

>
> Nonsense!
> It has exactly the same meaning that it has in C++.


Could you quote the part that defines it? I didn't find it.

>> not being defined by the [ANSI/ISO C] language standard at all.

>
> Nonsense!
>
> void free(void *ptr);
>
> is defined by ANSI/ISO C[89]9.


Yes it is. But what does the free() function have to do with the term
"free store"? I searched though the C99 standard, and no single
occurance of "free store" was found.
The C++ standard does use it, but it doesn't actually define the term
"free store". It just indicates that it ("free store") is the memory
where dynamic objects are allocated.

>> The C standard does not specify where allocated memory comes from

>
> Really?


Well, if it does, can you quote the part that dictates the location of
that memory?

> Can a C program "steal" the memory that is allocated?


The C standard neither requires nor forbids an implementation to provide
a way to "steal" memory, whatever that means.

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      01-24-2004
Rolf Magnus writes:

> I find it quite strange that C++ doesn't require a stack to exist, but
> OTOH dictates that when an exception occurs, "stack unwinding" is done.
> This term doesn't have a meaning if there is no stack, does it?


Sounds like a bit of poetic justice for pedants to me.


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      01-24-2004
On 23 Jan 2004 14:49:24 -0800, in comp.lang.c , (E-Mail Removed)
(MSG) wrote:

>Joona I Palaste <(E-Mail Removed)> wrote in message news:<buqlb3$1fl$(E-Mail Removed)>...
>> MSG <(E-Mail Removed)> scribbled the following
>> on comp.lang.c:


>> > 1. Stack allocation/deallocation is MUCH faster than heap -
>> > approximately O(1) vs O(log(n)), where n is the number of allocated
>> > pieces, IIRC.

>>
>> Did you *read* the other replies? Neither C or C++ specifies that a
>> "stack" or a "heap" even exists. Trollsdale is only trying to

> ^^^^^^^^^^
>It's not very nice of you. This is an international forum, so you need
>to learn to be more tolerant of others' opinions, including opinions
>as to what is on-topic.


Whats on topic here in CLC is not a matter of opinion (or in CLC++
for that matter). And Joona is exceptionally tolerant, even of idiots.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      01-24-2004
On Sat, 24 Jan 2004 11:27:01 +0100, in comp.lang.c , Rolf Magnus
<(E-Mail Removed)> wrote:

>Martin Dickopp wrote:
>
>> No, neither in C nor in C++ a heap and/or stack is even required to
>> exist. How/where the implementation allocates memory is unspecified.

>
>I find it quite strange that C++ doesn't require a stack to exist, but
>OTOH dictates that when an exception occurs, "stack unwinding" is done.
>This term doesn't have a meaning if there is no stack, does it?


There's a difference between the two meanings of "stack".


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How much heap can I reserve for JVM? Error when allocation very much heap Raymond Schanks Java 0 04-11-2010 04:25 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stl heap question: restoring heap validinty after changing 1 element viki C++ 6 06-28-2008 10:12 AM
Heap dump file size vs heap size Michal Slocinski Java 1 03-25-2008 12:54 PM
Heap vs Stack allocations MSG C Programming 19 01-29-2004 07:56 AM



Advertisments