delete []array problems (program crashes)

A

ahmad

Hi,

I am having trouble finding the root of this problem. My program gives
me a debugg error - I am using VC++ 6.0 - on this line delete []array,
and I would appreciate if you could provide some help.

here is the code...


Note: N=4

for(_rows=0;_rows<N;_rows++)
{

double** manor=new double*[N-1];

for(i=0;i<N;i++)
*(i+manor)=new double[N-1];

// loops to copy manor

for(rows=1;rows<N;rows++)
{

int _columns=0;
for(int columns=0;columns<N;columns++)
{

if(columns==_rows)
continue;
manor[rows-1][_columns]=m[rows][columns];
_columns++;

}

}

// code works up to this point
// Now I want to free the allocated memory

for(i=0;i<N-1;i++)
delete manor;

delete []manor; // it crashes on this line. I don't understand why?
Thanks in advance,

Ahmad
 
G

Gernot Frisch

ahmad said:
Hi,

I am having trouble finding the root of this problem. My program
gives
me a debugg error - I am using VC++ 6.0 - on this line delete
[]array,
and I would appreciate if you could provide some help.

here is the code...


Note: N=4

for(_rows=0;_rows<N;_rows++)
{

double** manor=new double*[N-1];

manor can hold 3 double* now.
for(i=0;i<N;i++)
*(i+manor)=new double[N-1];

for i=0; 1, 2, 3 -> that's 4 double*'s now. You're accessing memory
beyond allocation.
And that will yield at delete[] command.
-Gernot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top