Getting glibc detected error when trying to delete parts of a matrix in C++ -
when run following code, keep getting glibc detected error when try use delete[] i[i] command (always on last run through of loop). right before loop tries delete i[i], print out values in last row of i, , shows supposed to, don't think issue has loop being large. doing wrong? (i've included every line of code **i shows up).
edit 2: entirety of error message is:
*** glibc detected *** getparams: free(): invalid next size (fast): 0x086861f0 *** ======= backtrace: ========= /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x271591] /lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x272de8] /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x275ecd] /usr/lib/libstdc++.so.6(_zdlpv+0x21)[0x1cb741] /usr/lib/libstdc++.so.6(_zdapv+0x1d)[0x1cb79d] getparams[0x804ac78] getparams[0x8048943] /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x21cbd6] getparams[0x8048731] ======= memory map: ======== 00110000-001f9000 r-xp 00000000 08:01 396979 /usr/lib/libstdc++.so.6.0.13 001f9000-001fa000 ---p 000e9000 08:01 396979 /usr/lib/libstdc++.so.6.0.13 001fa000-001fe000 r--p 000e9000 08:01 396979 /usr/lib/libstdc++.so.6.0.13 001fe000-001ff000 rw-p 000ed000 08:01 396979 /usr/lib/libstdc++.so.6.0.13 001ff000-00206000 rw-p 00000000 00:00 0 00206000-00359000 r-xp 00000000 08:01 11272305 /lib/tls/i686/cmov/libc-2.11.1.so 00359000-0035a000 ---p 00153000 08:01 11272305 /lib/tls/i686/cmov/libc-2.11.1.so 0035a000-0035c000 r--p 00153000 08:01 11272305 /lib/tls/i686/cmov/libc-2.11.1.so 0035c000-0035d000 rw-p 00155000 08:01 11272305 /lib/tls/i686/cmov/libc-2.11.1.so 0035d000-00360000 rw-p 00000000 00:00 0 0074f000-00773000 r-xp 00000000 08:01 11272350 /lib/tls/i686/cmov/libm-2.11.1.so 00773000-00774000 r--p 00023000 08:01 11272350 /lib/tls/i686/cmov/libm-2.11.1.so 00774000-00775000 rw-p 00024000 08:01 11272350 /lib/tls/i686/cmov/libm-2.11.1.so 00a7b000-00a7c000 r-xp 00000000 00:00 0 [vdso] 00d1c000-00d37000 r-xp 00000000 08:01 11276409 /lib/ld-2.11.1.so 00d37000-00d38000 r--p 0001a000 08:01 11276409 /lib/ld-2.11.1.so 00d38000-00d39000 rw-p 0001b000 08:01 11276409 /lib/ld-2.11.1.so 00ec8000-00ee5000 r-xp 00000000 08:01 11272275 /lib/libgcc_s.so.1 00ee5000-00ee6000 r--p 0001c000 08:01 11272275 /lib/libgcc_s.so.1 00ee6000-00ee7000 rw-p 0001d000 08:01 11272275 /lib/libgcc_s.so.1 08048000-0804c000 r-xp 00000000 08:01 4720134 /home/rkappiyo/dropbox/xingresearch/finalmatlab/getparams 0804c000-0804d000 r--p 00003000 08:01 4720134 /home/rkappiyo/dropbox/xingresearch/finalmatlab/getparams 0804d000-0804e000 rw-p 00004000 08:01 4720134 /home/rkappiyo/dropbox/xingresearch/finalmatlab/getparams 08686000-086a7000 rw-p 00000000 00:00 0 [heap] b7600000-b7621000 rw-p 00000000 00:00 0 b7621000-b7700000 ---p 00000000 00:00 0 b774f000-b7751000 rw-p 00000000 00:00 0 b7762000-b7765000 rw-p 00000000 00:00 0 bfcd4000-bfce9000 rw-p 00000000 00:00 0 [stack] aborted
edit: i've changed include entirety of code.
void invertmatrix(double **mat, int size) { //index variables used looping int i, j, k; //l , u lu decomposition of mat double **l, **u; //invmat inverted matrix, stored in mat double **invmat; //i identity matrix double **i; l = new double *[size]; //allocation 1 for(i = 0; < size; i++) l[i] = new double[size]; //allocation 2 u = new double *[size]; //allocation 3 for(i = 0; < size; i++) u[i] = new double[size]; //allocation 4 //compute lu decomposition of mat , store in l , u ludecomp(mat, size, l, u); invmat = new double *[size]; //allocation 5 for(i = 0; < size; i++) invmat[i] = new double [size]; //allocation 6 = new double *[size]; //allocation 7 for(i = 0; < size; i++) { i[i] = new double [size]; //allocation 8 for(j = 0; j < size; j++) { if(i == j) i[i][j] = 1; else i[i][j] = 0; } } for(i = 0; < size; i++) { invmat[i][0] = i[i][0] / l[0][0]; for(j = 1; j < size; j++) { invmat[i][j] = i[i][j]; for(k = 0; k < j; k++) invmat[i][j] -= l[j][k] * invmat[i][k]; invmat[i][j] /= l[j][j]; } } for(i = 0; < size; i++) { mat[i][size - 1] = invmat[i][size - 1] / u[size - 1][size - 1]; for(j = size - 2; j > -1; j--) { mat[i][j] = invmat[i][j]; for(k = j + 1; k < size; k++) mat[i][j] -= u[j][k] * mat[i][k]; mat[i][j] /= u[j][j]; } } for(i = 0; < size; i++) { delete[] l[i]; //free allocation 2 delete[] u[i]; //free allocation 4 delete[] invmat[i]; //free allocation 6 delete[] i[i]; //free allocation 8 } delete[] l; //free allocation 1 delete[] u; //free allocation 3 delete[] invmat; //free allocation 5 delete[] i; //free allocation 7 }
i find way of allocating 2-d matrices annoying, wrote simple c procedure allocate , initialize 2-d array single malloc() call, can release memory single free() call. (note, extended/modified use new char[...])
/* set memory 2d matrix entries of size "size" */ void** matrix2d(long rows, long columns, long size) { long i; unsigned long long row_size = (unsigned long long)columns * (unsigned long long)size; unsigned long long data_size = ((unsigned long long)rows * (unsigned long long)columns + 1) * (unsigned long long)size; unsigned long long pointer_size = (unsigned long long)rows * sizeof(void*); void** result; if ( (result = (void**)malloc((size_t)(data_size + pointer_size))) == null ) { return null; } // take first bit vector pointing m_pdata each row char* pdata = (char*)result + pointer_size; if ((unsigned long)pdata % size) { pdata += size - (unsigned long)pdata % size; } // each row, set pointer m_pdata (i = 0; < rows; i++) { result[i] = (void*)pdata; pdata += row_size; } return result; }
then create each of matrices using single line, e.g.:
double** = (double**)matrix2d(size, size, sizeof(double));
and freed using, e.g.:
free(i);
much simpler. also, matrix data values contiguous, example if create array of integers , want initialize them zero, do:
int** array = (int**)matrix2d(height, width, sizeof(int)); memset(array, 0, height * width * sizeof(int));
or, initialize given (non-zero) value:
for (int = 0; < height * width; ++i) array[i] = value;
Comments
Post a Comment