The argument for Java 2D arrays being neither row major nor column major is that "two-dimensional array in Java is actually an array of references to arrays". C++ also follows the "array of arrays" abstraction. Then why does C++ require row major order?
Asked
Active
Viewed 352 times
1
-
8С/C++ doesn't have any inherent array order. Row-major vs column-major is determined by the meaning you assign to the first and second index. – HolyBlackCat Sep 19 '21 at 19:40
-
2Whoever told you C++ has row major order, ignore them. Permanently. From now on. At best, they're massively oversimplifying. At worst, they don't actually know C++ (C++ is infamous for this; there are a *ton* of people, even university professors, who teach C++ as "Java without a garbage collector" and think they can get away with just knowing Java and faking it from there). Learn C++ from someone who actually knows C++. A good tip is to ask about things like references, ownership, and move semantics. These are features that have no direct equivalent in Java. – Silvio Mayolo Sep 19 '21 at 20:40
-
I dont agree with the answer you link. The question asks "how are they stored" and the answer seems to interpret this in a very narrow way as requiring a very specific layout in memory. If you consider row/column major as "along which dimensions are elements in consecutive memory" this distinctions exists also in java – 463035818_is_not_an_ai Sep 19 '21 at 20:42
-
1To point out the oversimplification, in C++ the C-style arrays you're referring to are weird and annoying to work with. A variable of type `int[N][M]` only works if you know the dimensions at *compile-time*. Otherwise, you're stuck with `int**` and doing a *ton* of manual memory management to get it right. I would never do arrays this way. I would always use `std::vector` (probably a flat one, wrapped in a class to manage indexing), or an `std::array` if I really did know the size at compile-time. C-style arrays have no place in C++ in anything more than the most trivial uses. – Silvio Mayolo Sep 19 '21 at 20:43
-
@SilvioMayolo the C standard mentions that "_arrays are stored in row-major order (last subscript varies fastest_)" [e.g. http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1570.pdf, **6.5.2.1 Array subscripting**]. If it applies to C, surely it applies to C++ as well. – Sandeep Sep 19 '21 at 23:55
-
@Sandeepc- *"If it applies to C, surely it applies to C++ as well."* In this specific case yes, there is an equivalence. But in general, that can lead to fallacies. Be careful when drawing conclusions from one language about the other. – StoryTeller - Unslander Monica Sep 20 '21 at 00:36
1 Answers
3
Quoting the link in the question
What we may sometimes think of as two-dimensional array in Java is actually an array of references to arrays. It's not stored linearly in memory.
The difference is in C++ arrays are stored linearly in memory.
If you represent an m by n 2D array in linear memory, it has to either be layed out as n groups of m items or m groups of n items. In C and C++ an array like ary[M][N]
will be stored as M
groups of N
items. Whether you want to call those groups rows or columns is arbitrary but the point is the language does specify an order whereas Java does not because a 2D array in Java is not stored "flat".

jwezorek
- 8,592
- 1
- 29
- 46