I have a rather strange question that probably has no practical use, but the answers are very worrying. I tried a little chat with arrays and how to allocate them in memory using this code: (Xcode compiler 4 bits, 4 bytes integer)
int ***c; int size_x = 0; int size_y = 0; int size_z = 0; cout << "Enter x: " << endl; cin >> size_x; cout << "Enter y: " << endl; cin >> size_y; cout << "Enter z: " << endl; cin >> size_z; c = new int**[size_x]; for (int i = 0; i < size_x; ++i) { *(c+i) = new int*[size_y]; for (int j = 0; j < size_y; ++j) { *(*(c+i)+j) = new int[size_z]; } } for (int i = 0; i < size_x; ++i) { for (int j = 0; j < size_y; ++j) { for (int k = 0; k < size_z; ++k) { cout << (*(*(c+i)+j)+k) << endl;
When I log in now: 3, 2, and 4, I get the following output in the console:
0x100100a60 0x100100a64 0x100100a68 0x100100a6c 0x100100a70 0x100100a74 0x100100a78 0x100100a7c 0x100100a90 0x100100a94 0x100100a98 0x100100a9c 0x100100aa0 0x100100aa4 0x100100aa8 0x100100aac 0x100100ac0 0x100100ac4 0x100100ac8 0x100100acc 0x100100ad0 0x100100ad4 0x100100ad8 0x100100adc
What is my question now, if we look at the result, than we see that basically, the memory is aligned every 4 bytes, but sometimes we see a larger step, for example, from 0x100100a7c to 0x100100a90.
This is normal and how can I prevent it? Why is this? Is there any way to make c align my memory as a constant line? (I am not native English, so I'm sorry about that, but I don't know how to say it better)
Its just for a general understanding :-)
Thanks u!
PS is it enough to use delete [] after btw or do I need to go through each of the 3 memory blocks and delete [] the whole array there? EDIT:
Now I delete the memory and this works very well:
cout << "Free Memory" << endl; for (int i = 0; i < m_sx; ++i) { for (int j = 0; j < m_sy; ++j) { delete [] m_array[i][j];