September 2nd, 2004, 02:35 AM
i'm reading "Practical C" ... chapter about the "variable namespace"
The author says that "temporary variables" aka "those that belong to certain namespaces" are created in stack ...
this in some ways suggests that you cannot overflow global variables since they don't belong to the stack. i'm guite sure that this is not true but if you CAN overflow global vars. too, why call it a stack overflows.
by the way ... what the hell is "heap"
September 2nd, 2004, 02:49 AM
are global variables assigned dynamicaly
September 2nd, 2004, 06:07 AM
Wait a minute; in C++ variables are dynamically allocated on the heap, not the stack. Unless there was some drastic change, it should be the same way in C.
As for global variables, I'm not 100% sure, but I think they go on the lower levels of the stack unless you explicitly dynamically allocate a global variable with malloc.
September 2nd, 2004, 09:23 PM
The heap allows you to allocate memory exactly when you need it in a program instead of pre-allocating it with a declaration. You are right ydobon, malloc is used to allocate and free is used to...well you know.
Here are some links to help you along a bit:
As for the stack...Local variables in functions get put in as long as they are not static or register. It is able to overflow this because the stack has a limit set on its size, so if your array is too big for the stack you will need to use the heap instead so you won't have any errors. Here is a link to another forum that explains this a bit:
I hope this has helped.
September 3rd, 2004, 04:24 AM
hobbdebub thank you .. great links
September 3rd, 2004, 09:00 PM
Err, I'm sorry but the two first links you provided (howstuffworks) misleading to say the least:
The explanation provided (including schema) mixes virtual memory and real memory allocation... The heap IS NOT the collection of unallocated real memory but rather the unallocated virtuall memory inside each process' memory space.
Each process has it's own virtual memory space assigned to it and loaded into.
Virtual memory schema (without unnecessary details).
+------------------+ 0xFFF...F (~2gig private space by process on windows)
| Stack | | Grows downwards (local variables, function parameters...)
| Heap | ^ Grows upowards (dyanmic alloc. ie: malloc or new / free or delete)
| Data | Global/static variables, constants...
| Text | Program code (machine language)
Now, that being said, the schema on howstuffworks shows more of a representation of real memory but bypasses any explanation of virtual memory paging and thus becomes very misleading. For example, as far as you and your program are concerned, all memory dynamically allocated to you will be in contiguous memory that belongs only to you. Virtually, each running program has 2 gigs (or more depending on os and other config) of contiguous heap space, but if you try to allocate that much in many programs, you will soon start swaping memory to disk, and eventually run out of swap space and "receive out of memory" errors...
Memory management is a deepsubject... Hope this helped a little...
Oh and BTW, it is also possible to overflow heap variables (google for "heap overflow")...
Credit travels up, blame travels down -- The Boss
September 8th, 2004, 11:40 AM
from what i understand you basically have to "tell" the compiler to allocate anything in the heap
or does anything allocate in there automatically
also ... is stack limited by the free memory aviable
i mean ...if you "eat up" the stack .. you won't be able to have heap... (no more malloc) right???
September 9th, 2004, 08:51 PM
As far as I know, stuff is only allocated on the heap if you explicitly ask it to be with dynamic memory (not 100% sure though).
Yes, your stack is limited by how much memory you have.
If you "eat up" the stack by say, allocating a massive array, you'll probably crash so you won't have to worry about malloc
September 9th, 2004, 10:25 PM
I'd strongly suggest picking up a real reference on c/c++. Some of the information in this thread is blantantly wrong.
"When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
"There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
"Mischief my ass, you are an unethical moron." - chsh
Blog of X
September 20th, 2004, 05:40 PM
hey i'll tell just a little thing
stack has some applications and one of them is stack frame
and it says when we call a particular function sort of a thing
first the arguements then the return datatype and then the loacal variables are pushed onto the stack i.e. stack frame is just a region of memory within which a function executes and the stack used in this is dynamic i.e. there can be no overflow as the memory is allocated at the time of runtime!