c++ - How do I properly delete an array of doubles through a pointer after the pointer has been iterated? -



c++ - How do I properly delete an array of doubles through a pointer after the pointer has been iterated? -

i have created array of doubles using next code:

double *points = new double[(ii+1)*(jj+1)*(kk+1)*3];

i realize in order delete array, have is:

delete[] points;

however; array created in function (called create_points) passes address of first element follows:

return &points[0];

the code calls function iterates through address:

double *address = create_points(x_dim,y_dim,z_dim); for(int k=0; k<x_dim+1; ++k) for(int j=0; j<y_dim+1; ++j) for(int i=0; i<z_dim+1; ++i) { v[p_count].x = *address; ++address; v[p_count].y = *address; ++address; v[p_count].z = *address; ++address; ++p_count; }

now question is, how delete array of doubles address used access? can go delete[] address, or have other way?

you cannot delete[] address, doing undefined behavior. have delete through pointer original base of operations address of array. so, if you're going increment pointer, need remember original pointer (or recompute it):

double *base_address = create_points(...); double *address = base_address; // stuff address, increment it, etc. delete [] base_address

c++ memory delete-operator

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -