Timeline for How do I declare a 2d array in C++ using new?
Current License: CC BY-SA 3.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 1, 2020 at 2:32 | comment | added | Peter Cordes | Just for the record, the C equivalent with malloc uses the same syntax to declare the pointer-to-2D-array that holds the result: Malloc a 2D array in C | |
Sep 30, 2015 at 5:24 | comment | added | cmaster - reinstate monica | @M.M Pity that C++ does not have syntactic sugar for a true, consecutive multidimensional array allocated on the heap with sizes that are only known at runtime. As long as you don't need this, C++ syntactic sugar is fine. But when you need all the above, even FORTRAN beats C++... | |
Sep 30, 2015 at 0:08 | comment | added | M.M | @cmaster dynamically allocated VLAs are syntactic sugar anyway... they're good in C because there's nothing else, but C++ has better syntactic sugar :) | |
Sep 11, 2015 at 22:49 | history | edited | Peter Cordes | CC BY-SA 3.0 |
This is a GNU extension
|
Dec 14, 2014 at 13:46 | comment | added | cmaster - reinstate monica |
@user3241228 gcc fooled me when I wrote this: supplying -std=c++11 is not enough to switch on strict standard conformance, -pedantic-errors is required as well. Without the later flag, gcc happily accepts the cast, even though it is indeed not according to the C++ standard. With what I know now, I can only advise to fall back to C when doing stuff that's heavily reliant on multidimensional arrays. C99 is just much more powerful in this regard than even C++17 will be.
|
|
Dec 14, 2014 at 11:30 | comment | added | user1234567 |
@cmaster double (*in)[m][n] = (double (*)[m][n])new double[k*m*n]; doesn't work too. I'm getting C2057, C2540 errors on n because it isn't known at compile time. I don't understand why I can't do it, because memory allocated properly and it's only pointers to handle this memory conveniently. (VS 2010)
|
|
Oct 1, 2013 at 4:59 | comment | added | jbo5112 | What compiler are you using? The array syntax compiles and runs fine with g++ 4.6.4 and 4.7.3. I'm just getting a warning on the last ] before the = that "value computed is not used" or "statement has no effect". However, if I use g++ 4.8.1 (supposedly fully c++11 compliant), it throws errors on n and o not being constant "array size in operator new must be constant", and points to the last ] in the line. | |
Jan 12, 2012 at 15:32 | history | answered | etham | CC BY-SA 3.0 |