Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Difference between the stack and the heap?

Reply
Thread Tools

Difference between the stack and the heap?

 
 
Zach
Guest
Posts: n/a
 
      02-19-2007
Could someone please illustrate this with some ANSI C code?

Zach

 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      02-19-2007
On Feb 19, 1:00 pm, "Zach" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
Clever Monkey
Guest
Posts: n/a
 
      02-19-2007
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.
 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      02-19-2007
On Feb 19, 1:18 pm, Clever Monkey
<(E-Mail Removed)> wrote:
>
> 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


 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      02-19-2007
On Feb 19, 1:27 pm, "Zach" <(E-Mail Removed)> wrote:
>
> 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

 
Reply With Quote
 
Rachael
Guest
Posts: n/a
 
      02-19-2007
On Feb 19, 6:00 pm, "Zach" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-19-2007
Rachael wrote:

> On Feb 19, 6:00 pm, "Zach" <(E-Mail Removed)> wrote:
> > 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
 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      02-19-2007
Thanks for the code illustration Rachael.

Zach

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-19-2007
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


 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      02-20-2007
Zach wrote:
> On Feb 19, 1:27 pm, "Zach" <(E-Mail Removed)> wrote:
>
>>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
 
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
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
difference between a pointer array and a stack array? Ahmad Humayun C Programming 10 05-28-2008 06:04 PM
What is the difference between heap and stack Veeru C++ 12 09-14-2006 04:13 PM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments