Difference between the stack and the heap?

L

Lew Pitcher

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
 
C

Clever Monkey

Zach said:
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.
 
Z

Zach

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
 
Z

Zach

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
 
R

Rachael

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
 
D

Default User

Rachael said:
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
 
T

Thad Smith

Zach said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top