Thread: How "big" is the run-time stack?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    21

    How "big" is the run-time stack?

    I have some general programming questions:

    A stack overflow occurs when the stack gets "too big", so, how much is too big?
    Does the run-time stack have a specific size like an array? if so, how is the size determined? by the OS? the compiler?
    Is it a dynamic data structure like queues and lists? if so, when does it "overflow"? when the entire physical memory is filled?
    Also, if it is a dynamic data structure, does that mean that a running program is physically "fragmented"? does it occupy different seperated spots in the RAM?
    Are the answers to these questions OS-dependent? language dependent? compiler dependent?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    For me, it is 8 Mb.
    It depends on the OS, I think.
    On Linux, you can find it by "ulimit -s".
    You can find and change it from a running program with the system calls getrlimit and setrlimit.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by butteflymentor View Post
    A stack overflow occurs when the stack gets "too big", so, how much is too big?
    OS and compiler dependent. By default, on Windows with VC++, it's 1 MB.

    Does the run-time stack have a specific size like an array? if so, how is the size determined? by the OS? the compiler?
    Yes, it does. It's decided by the compiler and the OS.

    Is it a dynamic data structure like queues and lists? if so, when does it "overflow"? when the entire physical memory is filled?
    It's not dynamic. Its size is static and it's layout is that of a stack.

    Also, if it is a dynamic data structure, does that mean that a running program is physically "fragmented"? does it occupy different seperated spots in the RAM?
    The stack is a stack, therefore it does not get fragmented.

    Quote Originally Posted by manasij7479 View Post
    You can find and change it from a running program with the system calls getrlimit and setrlimit.
    Just to be explicit, that works only on Linux.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    21
    If all Windows programs allocate 1MB of stack memory, how come the Windows Task Manager shows a different RAM usage for each program? What do these numbers represent?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The stack is limited to around 1 MB, but that doesn't mean all programs are limited to 1 MB.
    There is also the free store, the heap, which is limited only to the virtual or physical amount of memory available in the system.
    Also, the stack does not overflow. What happens when you write after the end of the stack is undefined. You may get an error from the OS, or you may inadvertently corrupt some other data.
    Last edited by Elysia; 07-06-2012 at 11:27 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    21
    So, A pretty huge video game, such as Assassin's Creed would use 1MB of stack memory and the rest is handled by the heap? That doesn't make sense at all.
    Another example would be svchost.exe, It is currently using 123MB on my computer, are all the 122MBytes living in the heap?
    Also, many programs use LESS than 1MB of memory according to the task manager, does the task manager count only the used portion of the program's stack?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by butteflymentor
    A pretty huge video game, such as Assassin's Creed would use 1MB of stack memory and the rest is handled by the heap? That doesn't make sense at all.
    Why does that not make sense to you?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by butteflymentor View Post
    So, A pretty huge video game, such as Assassin's Creed would use 1MB of stack memory and the rest is handled by the heap? That doesn't make sense at all.
    Makes perfect sense to me.

    Another example would be svchost.exe, It is currently using 123MB on my computer, are all the 122MBytes living in the heap?
    Pretty much.

    Also, many programs use LESS than 1MB of memory according to the task manager, does the task manager count only the used portion of the program's stack?
    Memory management is a complex world. For one, you must divide memory into committed, reserved, free, shared and other.
    Though your program may have allocated some memory (eg 1 MB for the stack), you may not be using it all, and therefore the memory manager won't show it.
    A lot of this is low-level details that form the basis on how modern operating systems are built.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    21
    Why does that not make sense to you?
    Makes perfect sense to me.
    I admit that there is not a single logical argument against this, guess it does make sense :P

    Thanks for the help.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I seriously doubt AC is using the full 1mb of stack. More likely they are using dynamic memory pools for their objects. The chunk of memory is pre-allocated and then a class is responsible for dividing this up into equal chunks and doling them out to users. There are also stack-based memory allocators that use dynamic memory like a stack. They are extremely fast but have some interesting behavior when you go to clean up since they are stack based. Most games use a combination of pools and stacks for their memory management systems and they will vary from platform to platform. I invite you to read Game Code Complete, 2nd ed. for more information about the real world use of these types of constructs and how they are used in the industry.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    21
    I have a 20KB program which I programmed myself, I never use malloc or any other heap memory allocation function, yet the Task Manager shows a usage of 2.7MB, how do you explain this? The program is based on Win32, do the win32 functions use heap memory?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Based on what you use, there may be a lot of "book keeping" data in memory. Also remember that globals, static variables, and other things such as resources, the actual code, etc, also gets loaded into memory and counts towards your total usage.
    Then you must understand that there are different ways of showing "how much memory" a program is using. There is working set, private bytes, shared bytes, commited memory, shared memory, virtual size, etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    21
    Is code loaded into the heap or the stack? or both? can the programmer control that?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code isn't loaded to the stack, because it's meant for local variables which you can push and pop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code is loaded "somewhere else" by the OS. The allocation/de-allocation/loading of code is the OS's job. In many cases, the OS won't load the entire program into memory, either. Parts of the program are loaded when needed.

    It's neither stack nor heap.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-05-2012, 08:32 AM
  2. Replies: 1
    Last Post: 12-08-2007, 10:49 PM
  3. char* ptr="HELLO"; String Literal:Stack/Heap/Data Segment
    By forumuser in forum C Programming
    Replies: 9
    Last Post: 09-20-2007, 04:53 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM