stack overflow
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: stack overflow

  1. #1
    Banned
    Join Date
    Aug 2004
    Posts
    534

    stack overflow

    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"

  2. #2
    Banned
    Join Date
    Aug 2004
    Posts
    534
    are global variables assigned dynamicaly

  3. #3
    Junior Member
    Join Date
    Jun 2003
    Posts
    6
    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.

  4. #4
    Senior Member
    Join Date
    Jun 2004
    Posts
    112
    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:

    http://computer.howstuffworks.com/c28.htm

    http://computer.howstuffworks.com/c29.htm

    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:

    http://www.computing.net/programming...rum/11004.html

    I hope this has helped.

  5. #5
    Banned
    Join Date
    Aug 2004
    Posts
    534
    hobbdebub thank you .. great links

  6. #6
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    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.


    Code:
    Virtual memory schema (without unnecessary details).
    +------------------+  0xFFF...F (~2gig private space by process on windows) 
    |       Stack      |      | Grows downwards    (local variables, function parameters...)
    +------------------+      V
    |                  |
    |                  |
    |       Heap       |      ^   Grows upowards     (dyanmic alloc. ie: malloc or new / free or delete) 
    +------------------+      |
    |        Data      |            Global/static variables, constants...
    +------------------+
    |        Text      |             Program code (machine language)
    +------------------+  0x000...0
    Each process has it's own virtual memory space assigned to it and loaded into.

    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")...


    Ammo
    Credit travels up, blame travels down -- The Boss

  7. #7
    Banned
    Join Date
    Aug 2004
    Posts
    534
    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???

  8. #8
    Junior Member
    Join Date
    Jun 2003
    Posts
    6
    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

  9. #9
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    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

  10. #10
    Banned
    Join Date
    Apr 2004
    Posts
    94
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •