-
Problem with a 2D array.
Hi I'm dynamically creating a 2D array and I'm trying to write somethin to it but I get a run-time error that memory could not be read. I use the exact same code a few lines before it and it works fine, but this one fails. Here's the code:
Code:
Vic **mtxVicValsB;
mtxVicValsB = new Vic*[vars.NumScan];
for(i=0; i<vars.NumScan; i++)
mtxVicValsB[i] = new Vic[VicSize3];
mtxVicValsB[0][0].Name = "name"; <-- This line gives an access violation error
// and Vic is
struct Vic
{
float Value;
CString Name;
};
Any ideas on what's going on and how to get rid of the propblem?
-
I'm not familiar with the CString class, but I assume that the proper operator= is defined.
Make sure your sizes for the array are positive.
And, what compiler/OS?
Cheers
-
I'm using MS 6.0. and I I passed it a wrong variable to allocate the size. That's why it was getting confused. It wrks llike a charm now.
thanks though.
-
in making my simple chess game, i used this to make a 1d array a 2d array
Code:
int fun(int x, int y)// najes 1d array 2d array
{
return (8*y+x);
}
-
Word of caution. MSVC 6 is a pre-standard compiler, and tends to do some things in "funny" ways. Might want to consider upgrading to a standards compliant compiler at some point: gcc (Dev-C++), or .NET, or something similar.
Glad you got it working.
Cheers