Unhandled exception
Nov 21, 2012 at 9:02pm UTC
HI all,
when i run this code i get error message said
"Unhandled exception at 0x012D9116 in prog.exe: 0xC0000005: Access violation reading location 0x0126E710." and the prog stop running.
Note: in main(), and when i remove the loop, prog executed properly.
can you help me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
char x1[50][50][50];
unsigned x2[3][50][50];
void fn1(){
unsigned * x3= new unsigned [50];
//...
//...
delete [] x3;
}
void fn2(){
int i,j;
double *x4= new double [50];
char *** x5;
x5=new char **[50];
for (i = 0; i < 50; ++i) {
x5[i] = new char *[50];
for (j = 0; j < 50 ; ++j)
x5[i][j] = new char [50];}
// ...
//...
delete [] x4;
for (i = 0; i < 50; ++i) {
for ( j = 0; j < 50; ++j)
delete [] x5[i][j];
delete [] x5[i];
}
delete [] x5;
}
int main(){
int i=100;
while (i--)
{
fn1();
fn2();
}
retrn 0;
}
Last edited on Nov 21, 2012 at 9:05pm UTC
Nov 21, 2012 at 9:09pm UTC
The problem is probably somewhere in the code you haven't posted.
Last edited on Nov 21, 2012 at 9:10pm UTC
Nov 21, 2012 at 9:31pm UTC
yes, yes, i found that i tried to access x3[-99] in fun1() :D :D in the sec loop iteration of main.
thanks a lot,
i'll trying to fix the problem and ask again.
greetings.
Nov 23, 2012 at 11:00pm UTC
hi again,
if i have this code in fn2()
1 2 3 4
for (i=0; i<50; i++)
for (x=0 ; x<lengthM;x++)
for (y=0; y<widthM ;y++)
x1[i][x][y]=x5[i][x][y];
is there any way to directly change x1 addresse and make it points to x5?
Nov 23, 2012 at 11:34pm UTC
No.
1_ Use std::swap() with std::vector
A_ Maintain both matrix, select with a pointer
\alpha_ Construct directly in `x1'
¿why so much abuse with dynamic allocation?
Nov 23, 2012 at 11:43pm UTC
okay, thanks.
it's in real genetic algorithm implementation(multi-objective land use allocation problem),and i always have run out of memory, because of the large size of the chromosome.
*actually now i use:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
char x1[50][50][50];
unsigned x2[3][50][50];
void fn1(){
unsigned * x3= new unsigned [50];
//...
//...
delete [] x3;
}
void fn2(){
int i,j;
double *x4= new double [50];
char x5[50][50][50];
// ...
//...
delete [] x4;
}
int main(){
int i=100;
while (i--)
{
fn1();
fn2();
}
retrn 0;
}
Last edited on Nov 23, 2012 at 11:46pm UTC
Topic archived. No new replies allowed.