Dynamic Allocation of memory c++ performance improvements -


good night

i'm learning c++ scientific code development. differently of learning strategy used in python , matlab, i'm trying learn c++ in old , deep way (using book ref[1]). i'm struggling understand pointers , dynamic memory allocation.

i came across exercise , doubts appeared. exercise simple 2x2 matrix multiplication. code:

#include<iostream> //perform c[2][2] = a[2][2]*b[2][2] using dynamic memory allocation   int main(){      //declaring length of matrices     int row,col;     row = 2;     col = 2;      //allocating memory matrice [heap ?]     double** a, **b, **c;     = new double* [row];     b = new double* [row];     c = new double* [row];     (int i=0; i<row; ++i){         a[i] = new double [col];         b[i] = new double [col];         c[i] = new double [col];     }      //performing calculation     (int i=0; i<2; ++i){         (int j=0; j<2; ++j){             a[i][j] += 10;             b[i][j] += 20;             c[i][j] += a[i][j]*b[i][j];             std::cout << c[i][j] << " ";         }         std::cout << std::endl;     }      //deleting memory allocation [is there memory leak ? (no *=null)]     (int i=0; i<2; ++i){         delete[] a[i];         delete[] b[i];         delete[] c[i];     }     delete[] a;     delete[] b;     delete[] c;      return 0; } 

my doubts are:
1 - read memory division (global/stack/heap), located in hardware ? hd/ram/cpu cache ?

2 - when allocate array:

int arrays[2];       //is @ stack ?  int* arrayh;         //is @ heap ? arrayh = new int [2]; 

3 - in resolution of matrix problem. there memory leak or garbage creation ? (note didn't pointed arrays *=null rid of address)

4 - suggest way improve code performance , efficiency ?

thank !

1) global area, stack , heap located within application's cpu address space. mean they're in ram, subject virtual memory paging (in case may go , live on hd while) , cpu caching (in case may go , live on cpu while). both of things transparent far generated code concerned.

2) yes, arrays[2] on stack (unless it's global, of course), , returned new (or malloc, should include c code) on heap.

3) can't see leaks if you're going use row , col rather repeating 2 magic constant on place uniformly , mark them const can't accidentally modify them.

4) in terms of cache efficiency, may better 1 allocation new double[col * row] , either spread pointers out throughout block or index [i*col + j], assuming i indexes rows , rows col items long. new otherwise permitted spread rows out wherever likes across memory, more lead cache faults.

as style? well, didn't ask, think cdhowie's comment valid. deduplicator's point should read more deeply: stl has bunch of pieces make sure don't leak memory — read on unique_ptr , shared_ptr.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -