By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

deleting a multi-dimensional array

P: 1
Hi!

I am having problem deleting a multi-dimensional array. I get the following error message: glibc detected ***double free or corruption (out). The program is below. I am following the steps in FAQ-Lite at: http://www.parashift.com/c++-faq-lit...tore-mgmt.html

Expand|Select|Wrap|Line Numbers
  1. void myarray (int sta1) {
  2.   double **sta_dist = new double*[sta1];
  3.   for (int = 0; i < sta1; i++) {
  4.     sta_dist[i] = new double [sta1];
  5.     for (int j=0; j < sta1; j++) {
  6.       sta_dist[i][j] = 0.0;
  7.     }
  8.   }
  9.  
  10.   if (sta_dist) {
  11.     for (int i = sta1; i > 0; --i) {
  12.       delete [] sta_dist[i-1];
  13.     }
  14.    delete [] sta_dist;
  15.   }
  16. }
Dec 4 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
if (sta_dist) {
for (int i = sta1; i > 0; --i) {
delete [] sta_dist[i-1];
}
delete [] sta_dist;
}
}
This doesn't look right. You need to delete from 0 through sta1:
Expand|Select|Wrap|Line Numbers
  1. if (sta_dist) {
  2.     for (int i = 0; i < sta1; ++i) {
  3.       delete [] sta_dist[i];
  4.     }
  5.    delete [] sta_dist;
  6.   }
  7. }
  8.  
Dec 4 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.