On Tuesday, August 29, 2017 03:16:13 Johnson Jones via Digitalmars-d-learn wrote: > I need to get this straight: > > A normal single dimensional array in D is defined as > > T[] arr > > and is a linear sequential memory array of T's with an unbound > length and is effectively the same as T*(although D treats them > differently?)?
A dynamic array is essentially a struct defined like so struct DynamicArray(T) { size_t length; T* ptr; } In actuality, for historical reasons, I think that it's unfortunately defined with void* at the moment instead of being templated, but effectively, that struct is what you're dealing with when you have a dynamic array. If you want the C/C++ equivalent, you use the ptr property to get at the underlying pointer. > We can fix the length by adding a upper bound: > > T[N] arr; > > and this is equivalent to > > auto arr = cast(T[])malloc(T.sizeof*N)[0..N]; > possibly > > auto arr = cast(T[N])malloc(T.sizeof*N); They are not at all equivalent. Fixed size arrays live on the stack, just like you'd get with T arr[N]; in C/C++. Static arrays have a fixed length and live on the stack (or directly inside of an object on the heap if they're a member variable of an object on the heap), and they have no indirections. With dynamic arrays, the struct itself that is the dynamic array lives directly on the stack or wherever it's put, but it refers to any slice of memory. Usually, that memory is on the GC-allocated heap, but it could be malloced memory or even a slice of a static array on the static. They're resizable simply because the GC will either reallocate memory to resize them (e.g. if the memory is not GC-allocated or because there isn't enough room to expand it in place), or if the memory is GC-allocated, and there's room beyond the end of that slice of memory, then the dynamic array will just be made to refer to a larger slice of that block of memory. > But then arr is a fixed type and we can't use to resize the array > later if needed. > > these don't actually work, for some odd reason, so > > auto arr = cast(T*)malloc(T.sizeof*N)[0..N]; > > seems to be the way to go. > > So, what this "shows" is that in memory, we have > relative address type > 0 T > 1*sizeof(T) T > 2*sizeof(T) T > ... > N-1*sizeof(T) T > > > > This is pretty basic and just standard arrays, all that is fine > and dandy! > > > Now, when it comes to multidimensional arrays: > > T[][] arr; > > There are two ways that the array can be laid out depending on > how we interpret the order of the row/col or col/row. > > > The most natural way to do this is to extend single dimensional > arrays: > > T[][] is defined to be (T[])[] > > or, lets used a fixed array so we can be clear; > > T[N][M] > > which means we have M sequential chunks of memory where each > chunk is a T[N] array. > > This is the natural way because it coincides with single arrays. > > Similary, to access the element at the nth element in the mth > chunk, we do > > t[n][m] because, again, this conforms with out we think of single > arrays. > > > Now, in fact, it doesn't matter too much if we call a row a > column and a column a row(possibly performance, but as far as > dealing with them, as long as we are consistent, everything will > work). > > > BUT! D seems to do something very unique, > > If one defines an array like > > T[N][M] > > > one must access the element as > > t[m][n]! > > The accessors are backwards! > > This is a huge problem! > > int[3][5] a; > > Lets access the last element: > > auto x = a[4][2]; > auto y = a[2][4]; <- the logical way, which is invalid in D > > This method creates confusion and can be buggy. If our array is > not fixed, and we use the *correct* way, then our bugs are at > runtime and maybe subtle. > > Why? Because the correct way only has one thing to get right, > which is being consistent, which is easy. > > In D, we not only have to be consistent, we also have to make > sure to reverse our array accessors from how we defined it. > > While it is a unique approach and may have something to do with > quantum entanglement, I'm curious who the heck came up with the > logic and if there is actually any valid reason? > > Or are we stuck in one of those "Can't change it because it will > break the universe" black holes? It's doing exactly what C/C++ would do if they declared static arrays as T[N][M] arr; instead of T arr[N][M]; The issue is that types are normally read outwards from the variable name. You mostly don't notice it, but it becomes critical to understand when trying to read function pointer types in C/C++. Regardless, a side effect of that is that putting the array lengths on the right like C/C++ does puts them in the order that people expect, whereas putting them on the left like D does makes them seem backwards. It's consistent with how the compiler reads types though. I'm inclined to think that we'd have been better off to introduce an inconsistency and have the lengths be read from left-to-right rather than outward from the type and thus right-to-left, but we're stuck at this point, and it _is_ consistent with how C/C++ reads types. Ultimately though, all it means is that you need to be aware that when you have a multi-dimensional static array, the lengths go from right-to-left instead of left-to-right. Indexing them at that point is perfectly normal and functions like it would for dynamic arrays. You just have to be aware of the seemingly backwards direction of the lengths in the declaration for a static array. - Jonathan M Davis