-
February 9th, 2011, 09:19 AM
#1
Problem with const int.
Sorry to ask simple question, but, even after googling I couldn't solve my problem. Hope you guys can help me.
Code:
const int i=100000;
const int j=32;
__int64 A[i][j];
...
It compiles but when I run the .exe file it gives an error. The error is like:
.exe has encountered a problem and needs to close. We are sorry for the inconvenience. ....... Send error report/Don't send.
But, when I decrease one zero in i's value (i.e. when const int i=10000 it works fine.
What is the problem here and how can I solve it?
Thank you.
Last edited by ritika_sharma82; February 9th, 2011 at 09:23 AM.
-
February 9th, 2011, 09:22 AM
#2
Re: Problem with const int.
You're probably running out of stack space.
-
February 9th, 2011, 09:26 AM
#3
Re: Problem with const int.
Originally Posted by GCDEF
You're probably running out of stack space.
Hi GCDEF,
How can I solve it.
Any quick and easy solution will be appreciated.
I am using MS Visual c++ 6.0
-
February 9th, 2011, 09:26 AM
#4
Re: Problem with const int.
Is this a local variable?
If so, you are trying to allocate 25,600,000 bytes on the stack.
(100000 * 32 * 8)
That is highly unlikely to work on any machine! The default on Windows machines is 1MB per thread (I think).
You will have to allocate from the heap, either with a 'new' or use a std::vector.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 9th, 2011, 10:19 AM
#5
Re: Problem with const int.
Originally Posted by JohnW@Wessex
Is this a local variable?
If so, you are trying to allocate 25,600,000 bytes on the stack.
(100000 * 32 * 8)
That is highly unlikely to work on any machine! The default on Windows machines is 1MB per thread (I think).
You will have to allocate from the heap, either with a 'new' or use a std::vector.
Hello JohnW,
These are the local variables. Could you please give me an example? How to use heap?
My array is exactly A[100000][3]. So, it comes (100000 * 3 * 8)/(1024*1024) = 2.28MB.
-
February 9th, 2011, 10:47 AM
#6
Re: Problem with const int.
Using std::vector
Code:
#include <vector>
using namespace std;
int main()
{
const int i = 100000;
const int j = 32;
vector<vector<__int64>> A(i, vector<__int64>(j));
}
Though if you are going to have large 2D arrays then it is often better to wrap a 1D vector in a class and let the interface emulate the 2D aspect. In that way there is only one allocation of memory and a lot less heap fragmentation.
You can write your own fairly simply or Use Boost's multi_array.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|