memory management - Modifying a large dynamically sized 3D array mex/C++ -
short story: trying modify large 3d array gets allocated @ run-time on heap. believe function modifies array, vcross shown below, creating memory doesn't destroyed.
longer story: have large 3d double array (~126000x3x3 or 8.6mb) need run operations on. don't know how large first dimension of array @ compile time, allocate memory heap using new , delete operations.
when try store values array, segmentation violation. makes me think while storing values array, i'm creating memory somewhere goes waste, , fills heap.
the code compiles fine, hits seg violation when run it.
static void inpolyh( double (*f)[3],//pointer array[3], treated 2d array don't know first dimension until run-time. double (*v)[3], double (*p)[3], size_t numf, size_t nump) { /*calculate basenormals*/ //allocate memory on heap double (*basenormals)[3] = null;//pointer array[3] if ( !(basenormals = new double[numf][3]) ) { out_of_memory(); } //store vector cross products in each array[3] of basenormals (int i=0; i<numf; i++) { vcross(basenormals[i], v[(int)f[i][0]], v[(int)f[i][1]], v[(int)f[i][2]]); //this works } /*calculate face normals of tetrahedron*/ //allocate memory on heap (this works) double (*facenormals)[3][3] = null; //pointer array[3] of arrays[3] if ( !(facenormals = new double[nump][3][3]) ) { out_of_memory(); } //store vector cross products each array[3] of facenormals (int i=0; i<nump; i++ ) { (int j=0; j<3; j++ ) { vcross(facenormals[i][j], p[i], v[ (int) f[i][j] ], v[ (int) f[i][ (j + 1) % 3 ] ] ); //seg violation @ i=37560 } } delete [] basenormals; delete [] facenormals; } this believe culprit be. think function creates memory somewhere never gets destroyed. vector cross product functions accepts 4 array[3] parameters , assigns values first input parameter, passed reference.
static void vcross( double (&n)[3], double a[3], double b[3], double c[3]) { n[0] = b[1] * c[2] - a[1] * c[2] + a[1] * b[2] - b[2] * c[1] + a[2] * c[1] - a[2] * b[1]; n[1] = b[2] * c[0] - a[2] * c[0] + a[2] * b[0] - b[0] * c[2] + a[0] * c[2] - a[0] * b[2]; n[2] = b[0] * c[1] - a[0] * c[1] + a[0] * b[1] - b[1] * c[0] + a[1] * c[0] - a[1] * b[0]; return; } other details may matter:
- this intended mex function run in matlab.
- default encoding: windows-1252
- matlab root : c:\program files\matlab\r2012b
- matlab version : 8.0.0.783 (r2012b)
- operating system: microsoft windows 7
- processor id : x86 family 6 model 58 stepping 9, genuineintel
- virtual machine : java 1.6.0_17-b04 sun microsystems inc. java hotspot(tm) 64-bit server vm mixed mode
- window system : version 6.1 (build 7601: service pack 1)
are sure (int)f[i][j] in range [0, <dimension of v>)? first thing i'd print out values of (int)f[i][j] when loop runs. or fire debugger see values @ moment of crash.
Comments
Post a Comment