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.
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
The same way you specify the size of any array I suppose.
Sent from my iPadŽ
what compiler ? use std::vector and you don't have to worry about it.Originally Posted by siavoshkc
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
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.And a little test program.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; } };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Ž
Ah, you're talking about the system's stack? Yeah, basically what Ancient is saying.
Sent from my iPadŽ
see /Stack linker option
Good find, Ancient. It's a nice little read.
Sent from my iPadŽ
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
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.
well, Bubba, this crashes on runtime with VC++ 6.0
Code:int main() { int array[2000][2000][2000] = {0}; ... }
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
> 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.
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
> 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
Two instructions a piece, no matter what sort of array or pointer you're using.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
I'd be worried about getting the code right before worrying about such microscopic optimisations.