Stack Size

This is a discussion on Stack Size within the C++ Programming forums, part of the General Programming Boards category; How can I specify the size of stack to prevent Stack Overflow? I don't want to use new. I want ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236

    Stack Size

    How can I specify the size of stack to prevent Stack Overflow? I don't want to use new. I want ot use real arrays.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    The same way you specify the size of any array I suppose.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by siavoshkc
    How can I specify the size of stack to prevent Stack Overflow? I don't want to use new. I want ot use real arrays.
    what compiler ? use std::vector and you don't have to worry about it.

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    I want to know what thing determines the size of stack. I want to use some huge arrays in my program (I have enough memory) but it generates error in run-time.
    I use VC++6.
    Last edited by siavoshkc; 02-28-2006 at 10:32 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    You keep a running variable or your current element variable and the max size of the array. Then you make sure your current is never equal to or greater than the max.
    Code:
    // A fixed size stack without dynamic memory allocation
    const int SIZE = 10;
    
    class FixedStack {
       private:
          int Num[SIZE];
          int currElement;
       public:
          FixedStack() {
             for(int i = 0; i < SIZE; i++)
                Num[i] = 0;
             currElement = 0;
          }
          
          void Push(int num) {
            if(currElement < SIZE - 1)
               Num[currElement++] = num;
            else
               std::cerr << "Stack is Full." << std::endl;
          }     
          
          int Pop() { return Num[--currElement]; }
          
          void Peek(int index) {
            if(index <= currElement)
               std::cout << Num[index] << std::endl;
            else
               std::cerr << "Index is larger than the stack's size." << std::endl;
          }     
    };
    And a little test program.
    Code:
    int main() {
       FixedStack A;
       int B;
       
       for (int i = 0; i < 4; i++)
           A.Push(i * i);
           
       std::cout << A.Pop() << std::endl;
       A.Peek(2);
       A.Peek(7);
       
       for (int i = 0; i < 7; i++)
          A.Push(i * i * i);
    
       std::cin.get();
       return 0;
    }
    Last edited by SlyMaelstrom; 02-28-2006 at 10:34 AM.
    Sent from my iPadŽ

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Ah, you're talking about the system's stack? Yeah, basically what Ancient is saying.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    see /Stack linker option

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Good find, Ancient. It's a nice little read.
    Sent from my iPadŽ

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Yes. Thanks.
    It was a good question, wasn't it?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Changing stack size to accomodate large arrays on the stack is a bad idea. Besides stack size is no longer a problem with modern 32-bit systems.

    The only time you may run into stack overflow problems is during heavy heavy recursion such a recursive algo to do a flood fill on a large area. Other than that you shouldn't have any problems.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    well, Bubba, this crashes on runtime with VC++ 6.0
    Code:
    int main()
    {
    	int array[2000][2000][2000] = {0};
    ...
    }

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Using stack will speed up the program execution. No allocation overhead will be exist. Only slowers startup, because whole memory should be reserved or allocated at start up.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > Using stack will speed up the program execution
    Do you imagine that asking the OS for more stack space will be more efficient than asking for more pool space for dynamic allocation?

    Just because you don't do it explicitly doesn't make it instantly a freebie request.

    Nor will it stop the OS from paging your data out to swap file, the stack can be swapped out just as easily as any other bit of data in your program which hasn't been touched in a while.

    Besides, how long is your code going to take to loop through all 100M+ entries in the array - a hell of a lot longer than it would take to allocate, no matter which way you go.
    For such large blocks of memory, the allocation cost is minimal.

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Because using arrays has more speed than pointers and using huge arrays is only possible with a huge stack it will speed up the work not because it allocate (or reserve) memory once, but because it makes us able to use arrays instead of pointers.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > Because using arrays has more speed than pointers
    Gotta love this old heresay evidence.
    Modern compilers don't have such problems anymore, and even if there is a difference, that difference isn't worth much.
    Besides, if you pass your array to a function, then all the optimisation which could have happened with an array is gone, since now everything is a pointer.

    Example
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int globalArray[10];
    int main ( int argc, char *argv[] ) {
      int localArray[10] = { 0 };
      int *ptrArray = malloc ( 10 * sizeof *ptrArray );
      int i;
      for ( i = 0 ; i < 10 ; i++ ) {
        printf( "%d %d %d\n", globalArray[i], localArray[i], ptrArray[i] );
      }
      return 0;
    }
    
    
    With gcc -O2
    movl	$LC0, (%esp)                  ; push the printf control string
    movl	(%ebx,%edi,4), %eax           ; load ptr[i]
    movl	%eax, 12(%esp)                ; push it
    movl	-56(%ebp,%edi,4), %eax        ; load local[i]
    movl	%eax, 8(%esp)                 ; push it
    movl	_globalArray(,%edi,4), %eax   ; load global[i]
    movl	%eax, 4(%esp)                 ; push it
    Two instructions a piece, no matter what sort of array or pointer you're using.

    I'd be worried about getting the code right before worrying about such microscopic optimisations.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 10:32 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 05:14 PM
  4. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  5. Replies: 11
    Last Post: 03-25-2003, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21