How does the OS lay out arrays on the heap?

Hey I've got a VERY experimental function which takes data stored to a file and assigns it to a multidimensional array on the heap. It's designed for infinite dimensions by recalling itself with updated information but I don't think this is very safe.

The template function creates a heap array using a TYPE**, and recalls itself to create the new dimensions. I want to replace this with the much safer method of assigning just a single heap memory array and then only assign using the recalling method (unless I can find anything else).

To do this though I need to know how single dimensional arrays are stored on the heap, as well as multi-dimensional (for n dimensions). So any idea where I can find this information?

btw I only need this for the Windows operating system, 32bit, I'm not exactly sure what 'C++ style' this is but I'm using Microsoft's Visual Studio Express 2012 as my IDE, so whatever that uses.
Just incase any specifics are needed.

Thanks in advance for any help
You can generate assembly code using the /FA switch.
http://msdn.microsoft.com/en-us/library/367y26c6.aspx
closed account (zb0S216C)
SatsumaBenji wrote:
"To do this though I need to know how single dimensional arrays are stored on the heap, as well as multi-dimensional (for n dimensions)."

Generally speaking, there's no standard arrangement for arrays (1-D to n-D) and standard containers. Though, arrays are normally stored in contiguous memory to reduce memory usage and make more effective use of CPU caches. 2-dimensional arrays, on the other hand, are normally stored in the same way as arrays, like so:

1
2
3
4
5
6
7
8
9
10
11
12
// 1-D Array:
// |-----|-----|-----|
// | OBJ | OBJ | OBJ |
// |  1  |  2  |  N  |
// |-----|-----|-----|

// 2-D Array
// Row: 1                Row: 2
// |------|------|------|------|------|------|
// | OBJ  | OBJ  | OBJ  | OBJ  | OBJ  | OBJ  |
// |  11  |  12  | 1N   |  21  |  22  | 2N   |
// |------|------|------|------|------|------| 

[Note: the compiler may -- and most probably will -- store data in exactly the same way regardless of placement (e.g. stack, heap, ROM, etcetera) --end note]

You can confirm data-placement accurately by looking at the Assembly output or by consulting your compiler's/OS's API documentation.

Wazzak
Last edited on
If you're talking about the internal layout of a single array...

The layout of C-style arrays is nothing to do with the o/s; it's defined by the language. Array elements are stored contiguously, even for multi-dimensional arrays. Where the array is stored makes no odds to this.

(This is defined in the C++ standard.)

Or do you mean something else?

Andy

PS To be clear, this kind of thing

1
2
3
4
TYPE** pArray = new TYPE*[rows];
for(int r = 0; r < rows; ++r) {
    pArray[r] = new TYPE[cols];
}

does not create an actual multi-dimensional array; just something that behaves like one when it comes to accessing elements using chained operator[] calls.
Last edited on
Yeah I found out that there are a few "problems" with how I'm setting up the arrays which is why I want to completely change it, because the pointer to array (being a pointer/reference itself to the first value) gets a little confusing in a recursive function.

@htirwin, How would I use this then? What would FA show me and what do I look for?

And thanks for the help guys
Last edited on
Topic archived. No new replies allowed.