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.
On Feb 19, 1:27 pm, "Zach" <net...@gmail.c omwrote:
>
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 :-)
On Feb 19, 6:00 pm, "Zach" <net...@gmail.c omwrote:
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);
On Feb 19, 6:00 pm, "Zach" <net...@gmail.c omwrote:
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.
On Feb 19, 1:27 pm, "Zach" <net...@gmail.c omwrote:
>
>>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.
"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
Comment