I know that the amount of memory that compiler gives for creating an array has limits. How can I configure my compiler to increasing this memory. And if it is possible, what are the advantage and disadvantages? I use linux and g++ compiler.
-
9Be more specific. Do you mean an array on the stack? Are you asking how to increase the stack size? If so, on what platform? In any case it sounds like a poor approach for solving the underlying problem. – Amardeep AC9MF Apr 08 '12 at 17:03
-
3Well, we don't know which compiler are you using, so we may guess. You also didn't specify, which memory you mean - I guess you are talking about stack. So, if we're talking about Visual Studio, you may appreciate this: http://msdn.microsoft.com/en-us/library/tdkhxaks%28v=vs.100%29.aspx But if we're talking about Linux, than check this: http://linux.die.net/man/2/setrlimit, especially RLIMIT_STACK. – Miroslav Mares Apr 08 '12 at 17:07
-
The compiler doesn't 'give memory for creating an array'. In the case of static data, that is done by the loader. In the case of stack data it is done by the operating system. In the case of heap memory it is done by the implementation of new() in conjunction with the operating system. Your question remains unclear. – user207421 Apr 10 '12 at 04:30
2 Answers
I'm going to try to answer you, even if the question is very unspecific (please specify your compiler and if you mean stack).
Now I will assume that you mean stack and you are using Linux or Windows with Visual Studio.
Linux
On linux, you can use system call setrlimit. Then you will set the stack programatically in your code, like in this Stack Overflow thread.
Windows
On Windows with Visual Studio, you may use compiler option /F
or /STACK
, for more information visit this MSDN documentation page.
In some cases, you may also consider using heap instead of stack, there is some nice comparation in this thread. Then you would use dynamic memory allocation and you wouldn't care about stack size (however, you would care about a little different approach to the array).
If you have another question, please specify your compiler...

- 1
- 1

- 2,292
- 3
- 22
- 27
If you are talking about the stack size, it depends on the System you are using. Since you said to be on linux you can change the stack size from your program. However beware that this is not portable to other OS. To change the stack size you can use this function (More or less copyied from here)
#include <sys/resource.h>
using namespace std;
//Increases the Stacksize to at least minStackSize
bool setStack(rlim_t minStackSize)
{
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
//If we got an answer
if (result == 0)
{
//Check if Stack is smaller than needed
if (rl.rlim_cur < minStackSize)
{
//Increase Stacksize
rl.rlim_cur = minStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result == 0)
return true;
else
return false;
}
else
return true;
}
else
return false;
}
Note it is a minimalistic function and you probably want to add your own error messages, instead of just returnen TRUE/FALSE.
The advantage of incresing your stack size, is simply that your programm does not crash if you try to put more variables on the stack than your stack size allows. The disadvantage is that this space is always occupied by your program regardless if it is actually used or not. (Your OS however can trick a bit)