473,803 Members | 4,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Heap vs Stack allocations

MSG
Hello

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. Without learning assembly, is there any
way to see how a particular compiler handles each of these cases?

Best wishes,
MSG
Nov 14 '05
19 6281
MSG wrote:
<E.************ **@jpl.nasa.gov > 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 (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #11
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?

Nov 14 '05 #12
On Fri, 23 Jan 2004 09:31:43 GMT, in comp.lang.c , CBFalconer
<cb********@yah oo.com> 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.c om/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 =---
Nov 14 '05 #13
MSG
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote in message news:<bu******* ***@oravannahka .helsinki.fi>.. .
MSG <ms*****@yahoo. com> scribbled the following
on comp.lang.c:
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message news:<40******* *******@jpl.nas a.gov>...
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 "interestin g" 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.

Nov 14 '05 #14
On 23 Jan 2004 14:49:24 -0800, ms*****@yahoo.c om (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
re************* ***********@att .net
Nov 14 '05 #15
Martin Dickopp wrote:
ms*****@yahoo.c om (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?

Nov 14 '05 #16
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.

Nov 14 '05 #17
On 23 Jan 2004 14:49:24 -0800, in comp.lang.c , ms*****@yahoo.c om
(MSG) wrote:
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote in message news:<bu******* ***@oravannahka .helsinki.fi>.. .
MSG <ms*****@yahoo. com> 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.c om/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 =---
Nov 14 '05 #18
On 22 Jan 2004 14:22:53 -0800, in comp.lang.c , ms*****@yahoo.c om
(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 do you care? You don't need to know.....
However, AFAIK, only in case of f2 the
compiler is required to do so.
Um, since C doesnt require a compiler to use a heap or stack, then not
even f2 is required to use it.
Without learning assembly, is there any
way to see how a particular compiler handles each of these cases?


The existence of a heap or stack is utterly implementation dependent
and therefore depends on finding a means to monitor how your
implementation does something. A good debugger might help.

However the fundamental question is: why do you care? If you use the
knowledge of how your current compiler does it, then your code is
almost guaranteed to break when the maker upgrades it.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/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 =---
Nov 14 '05 #19
ms*****@yahoo.c om (MSG) wrote in message news:<54******* *************** ****@posting.go ogle.com>...
Hello

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. Without learning assembly, is there any
way to see how a particular compiler handles each of these cases?

Best wishes,
MSG


Although it is not in the C standard there is a function called
'alloca' that does this. Linux, all modern Unix implementaions and MS
Windows provide it.

y = alloca(x);

Allocates x bytes and gives a pointer to them. They are deallocated
at the end of the function.

The MS Windows version is called "_alloca"

It's not gauranteed to be faster than malloc, or in fact implemented
on the stack (I've come across it implemented on the heap) but it
normally is.

Personally I wish it was in the C standard, but it's not, because it
is hard to implement universally (on machines without stacks). So if
you want to know more post the question elsewhere, or email me
directly.

I wouldn't worry much about speed of memory allocation anyway, or
really about fragmentation, mallocs are good these days. But I do use
alloca because it demonstrates my intentions clearly i.e. that the
memory stops being used at the end of the function.
Nov 14 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
808
by: MSG | last post by:
Hello void f1(int n) { vector<int> x(n); /* C++ */ } void f2(int n) { int x; /* C99 only */ } void f3(int n) { int* x = new int; /* C++ */ delete x; }
17
5054
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
11
2317
by: Dan Elliott | last post by:
Hello all, I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting most/all of it on the stack? Does it matter? Any considerations I might take into account when deciding how to allocate the many data structures? By the way, the system will consist of a handful of very large data structures (primarily matrices...
4
16644
by: ivan | last post by:
Hi, How can we calculate the stack and heap size requried by a C program. Is there any specific formula used? Please suggest. regards, Ivan
2
2857
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is allocated on the stack since it's a value type variable - variable d is allocated on the stack since it's a value type variable Where does variable "c" get allocated? It's a value type, but not foundwithin a method.
9
2545
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default constructor and get me an Array<int32> with a size 0? The memory used is the stack, right? ia = Array<int32>(10);
11
2926
by: Nehil | last post by:
I would like to know which is dynamic in nature. if i refer the C memory model (Richard Steven), it is shown that both stack and heap grow towards each other. Now, can one go into other's area and hence effecting the size of other memory area. Does any limit exist upto which a stack or a heap can grow. and if it is there then who decides the limit? plz clarify.
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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,...
1
7604
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
5500
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
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.