By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Difference between the stack and the heap?

P: n/a
Could someone please illustrate this with some ANSI C code? :-)

Zach

Feb 19 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Feb 19, 1:00 pm, "Zach" <net...@gmail.comwrote:
Could someone please illustrate this with some ANSI C code? :-)
Nope.

Primarily because the ANSI and ISO standards do not mention (let alone
distinguish between) stack and heap.

Sorry
--
Lew

Feb 19 '07 #2

P: n/a
Zach wrote:
Could someone please illustrate this with some ANSI C code? :-)
Do you mean implemented a stack and a heap, or how some runtime
environments may utilize things called "the stack" and "the heap".

The former can be addressed in any good book or Google search. The
latter does not have anything to do with ANSI C, I think.
Feb 19 '07 #3

P: n/a
On Feb 19, 1:18 pm, Clever Monkey
<clvrmnky.inva...@hotmail.com.invalidwrote:
>
The former can be addressed in any good book or Google search. The
latter does not have anything to do with ANSI C, I think.
Ah, been reading some posts in here and see heap and stack mentioned.
I have a vague understanding of what they are. Thought there was way
to illustrate this with some code in C.

Zach
Feb 19 '07 #4

P: n/a
On Feb 19, 1:27 pm, "Zach" <net...@gmail.comwrote:
>
Ah, been reading some posts in here and see heap and stack mentioned.
I have a vague understanding of what they are. Thought there was way
to illustrate this with some code in C.
To answer Clever Monkey:

I was thinking about the latter: "the stack" and "the heap".
Never saw or learned the former either: "stack" and "heap" in C
yet :-)

Zach

Feb 19 '07 #5

P: n/a
On Feb 19, 6:00 pm, "Zach" <net...@gmail.comwrote:
Could someone please illustrate this with some ANSI C code? :-)

Zach
If you just declare a variable or array like this:
int n;
char ac[5];
then it's on the stack. When it goes out of scope (i.e. when you exit
the function or block in which it was declared) the memory which it
took up is automatically given back.

If you allocate memory using malloc, like this:
char * pc = malloc(5);
then the memory is allocated on the heap, and will not automatically
be given back when the variable goes out of scope, so you have to
explicitly free the memory:
free(pc);

Rachael

Feb 19 '07 #6

P: n/a
Rachael wrote:
On Feb 19, 6:00 pm, "Zach" <net...@gmail.comwrote:
Could someone please illustrate this with some ANSI C code? :-)

Zach

If you just declare a variable or array like this:
int n;
char ac[5];
then it's on the stack.
This is not required by the standard, but is often not even true when
the system uses a stack. Automatic variables can be and sometimes are
held in registers.
When it goes out of scope (i.e. when you exit
the function or block in which it was declared) the memory which it
took up is automatically given back.
Again, no such behavior is mandated.
If you allocate memory using malloc, like this:
char * pc = malloc(5);
then the memory is allocated on the heap, and will not automatically
be given back when the variable goes out of scope, so you have to
explicitly free the memory:
free(pc);
What you say about the lifetime is correct, but no such entity as a
heap is required to do this.

Brian
Feb 19 '07 #7

P: n/a
Thanks for the code illustration Rachael.

Zach

Feb 19 '07 #8

P: n/a
Zach wrote:
On Feb 19, 1:27 pm, "Zach" <net...@gmail.comwrote:
>>Ah, been reading some posts in here and see heap and stack mentioned.
I have a vague understanding of what they are. Thought there was way
to illustrate this with some code in C.

To answer Clever Monkey:

I was thinking about the latter: "the stack" and "the heap".
Never saw or learned the former either: "stack" and "heap" in C
yet :-)
Provide enough context, usually be quoting, so that you meaning becomes
apparent. It is not clear from the message what the latter and former are.

--
Thad
Feb 20 '07 #9

P: n/a
Zach wrote:
>
Could someone please illustrate this with some ANSI C code? :-)
What stack? What heap? No such things are defined in ISO C.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 20 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.