# C++ praciticing note ###### tags: `C++` ## Segmentation fault on large array sizes a problem aries: ``` // C_max is define by macro void f() { T arr1[C_max][H_max][W_max]; T arr2[C_max][C_max][K_max][K_max]; T_out arr3[C_max][H_max][W_max]; // process these three arrays } ``` This fails when C_max, H_max, W_max goes from 64 by 32 by 32 to 512 by 228 by 228 however we changes to ``` T*** arr1; arr1= new T**[C_max]; //.... for(int i = 0; i < C_max; i++) { arr1[i] = new T*[H_max]; // same for arr2,3 for(int j = 0; j < H_max; j++) { arr1[i][j] = new T[W_max]; } } // process these three arrays ``` it works. So what's different between two initiation? 1. `T local_fmap[C_max][H_max][W_max];` resides in **stack address space** 2. `T*** local_fmap` resizds in **heap**, and work if your machine has enough memory 3. from reference: ![](https://i.imgur.com/mBMXvJ9.png) * reference: * [Segmentation fault on large array sizes](https://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) * [What and where are the stack and heap?](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap)