C++: XCode EXC_BAD_ACCESS issue
c++, error-handling, memory-management, xcode
Solution
If Jesse Good posts his comment "`int matrix[10000][10000];` looks like a stack overflow to me. " as an answer, I'll delete this.
You should create an allocation of this size on the heap, rather than on the stack.
Here's the breakdown:
400,000,000 bytes = 10000*10000*sizeof(int)
One common way to overcome this problem by creating allocations on the heap would be:
#include <iostream>
#include <vector>
int main(int argc, const char* argv[]) {
std::vector<std::vector<int> > matrix(10000, std::vector<int>(10000, 24));
cout<<"Done"<<endl;
return 0;
}
Stack size is determined by the operating system, hardware, and other things. You may have less than one MB to work with, but not usually more than a few. Therefore, large stack allocations should be avoided.
Problem
I am new to coding C++ in Xcode as my framework so I would like to ask a question that I believe is merely related to memory management What I'm trying to achieve is that I try to create a matrix size 10000 x 10000 however even I do the proper coding(at least that's what I believe) I just can't get it to work, just pointing out the EXC_BAD_ACCESS error. You can find my code in below. Is this an XCode-oriented error or I've done an obvious coding error? ``` #include <iostream> using namespace std; int main(int argc, const char * argv[]) { // insert code here... int matrix[10000][10000]; for(int i=0;i<10000;i++) for(int j=0;j<10000;j++) matrix[i][j]=24; cout<<"Done"<<endl; return 0; } ```