Hello
I think this is easy for experienced C++ users.
I want to define and initialize a [512][512] matrix.
The problem is that when I do int matrix[512][512] I get an error and the program it shuts down.
I think this has to do with max reserve memory.
What can I do?
Thank you

Dani AI

Generated

Below are concise, practical notes that clarify what likely happened and how to fix it cleanly — tying back to points already raised by , and .

A large automatic (local) array lives on the stack. Large stack allocations can crash a program at runtime even if they compile. Using a heap allocation instead (for example with a single std::vector) moves the memory off the stack and avoids that failure mode. Making the array static or defining it at namespace scope also avoids using the stack. ’s switch to vectors is exactly why the crash stopped.

If you want a compact, cache-friendly 2D container, allocate one contiguous vector and index with row*cols+col. Example pattern (not shown earlier in the thread):

const int ROWS = 512, COLS = 512;
std::vector<int> mat(ROWS * COLS, -1);

auto at = [&](int r, int c) -> int& { return mat[r * COLS + c]; };
at(100,100) = 123;
std::cout << at(100,100) << '\n';

This uses heap memory, is contiguous for better performance, and is easy to pass around.

Troubleshooting tips: run under a debugger to see whether the crash is a stack overflow or an access violation; narrow the crash location by printing or breaking before/after the allocation/fill; enable runtime checks/sanitizers if available. Also verify loop indices and that i/j are declared and the output operator is used correctly — small typos can cause no output or immediate termination; ’s catch about those basics is still useful.

Recommendation: prefer a single std::vector for most uses. If you must use a true C-style 2D array and avoid heap, declare it static/global. If threads are involved, prefer heap allocation because thread stacks can be smaller.

Recommended Answers

All 5 Replies

Hello
I think this is easy for experienced C++ users.
I want to define and initialize a [512][512] matrix.

You probably don't. Tell us why you think you should.

The problem is that when I do int matrix[512][512] I get an error and the program it shuts down.
I think this has to do with max reserve memory.
What can I do?

Tell us what the error says.
If you declare an int array that's 512*512*4 bytes (asssuming 32 bit OS), that would result in exactly 1 MB memory use, so that shouldn't really be a problem. (assuming you have a PC from the past 10-15 years)

Yeah, I tried this, it was compiling ...
I stored a value in it but when displaying with cout it didn't display anything ...

You're probably better off with a vector:

// Create
vector< vector<int> > vec(512, vector<int>(512));
// Write
vec[2][3] = 10;

I initialize the matrix as following and when then I cout, the program crashes...

int matrix[512][512];
   for(i=0;i<512;i++){
	   for(j=0;j<512;j++){
		  
		   matrix[i][j]=-1;
	   }
	   }

cout<<matrix[100][100]<endl;

>>I initialize the matrix as following and when then I cout, the program crashes..
Look at the <endl it should be <<endl
Are i and j defined?
Try running this code on your compiler;

int matrix[512][512];
   for(int i=0;i<512;i++){
	   for(int j=0;j<512;j++){
		  
		   matrix[i][j]=-1;
	   }
	   }

cout<<matrix[100][100]<<endl;

Thanks you boys. The only way was to use vector as "tux4life" suggested. Thank everybody.Although I cant understand way the program crashes I will mark this thread as solved.Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.