Answer the question
In order to leave comments, you need to log in
What does a dynamic array look like in memory in C++?
I study pointers, there is a task "How does a three-dimensional dynamic array look in memory if my_3d_array_height=2, my_3d_array_width=3, my_3d_array_depth=4?". There is an answer to this problem:
int ***AllocateLargeMemoryBlockFor3DArray (int height, int width, int depth) {
int * large_memory_block = new int[ height * width * depth ];
int ***my_3d_array = new int**[ height ];
for (int row_index = 0; row_index < height; ++row_index) {
my_3d_array [ row_index ] = new int * [ width ];
for (int column_index = 0; column_index < width; ++column_index) {
my_3d_array [ row_index ][ column_index ] = &large_memory_block [ ( row_index *width + column_index ) *depth ];
}
}
return my_3d_array;
}
Answer the question
In order to leave comments, you need to log in
In the answer to this problem, one goal was apparently achieved - to make it possible to address array elements by three consecutive indexings, and not by one "complex" one - [ ( row_index *width + column_index ) *depth ]
.
Why this is done in this way is not clear, in C ++ you can offer more elegant options for solving this problem without additional. allocations for arrays-from-pointers (limited only to large_memory_block , where the array elements will be stored).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question