Timeline for How do I declare a 2d array in C++ using new?
Current License: CC BY-SA 4.0
40 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 29 at 16:00 | review | Suggested edits | |||
May 30 at 9:48 | |||||
Feb 13 at 0:30 | comment | added | xealits |
is it possible to use a placement pointer with that new? i.e. new (buf) int [nrows][CONSTANT]; My g++ says "no matching function call..." I'm trying to declare an array for the compiler in a chunk of hugepages-backed memory.
|
|
Feb 10 at 12:03 | comment | added | Kphysics | What I usually do is allocate contiguous memory in one go: int** a = new int*[rowCount]; int* m= new int[rowCount * colCount]; for(int i = 0; i < rowCount; ++i) a[i] = m+i * colCount; | |
Dec 1, 2020 at 2:44 | history | edited | Peter Cordes | CC BY-SA 4.0 |
C++ VLA support *does* allow new, just not new[]. Link another answer here, instead of the C answer.
|
Dec 1, 2020 at 2:23 | comment | added | Peter Cordes | @MehrdadAfshari: I made an edit to mention the other better (more efficient) ways of handling 2D arrays. It's fairly intrusive so you might want to adjust or roll it back. IMO the top answer on this FAQ question shouldn't only mention the worst way to do 2D arrays. It seems like more than half the C++ SIMD optimization questions about matrices include code like that, making arrays of pointers masquerading as 2D arrays, despite that being well known as a bad choice for high-performance code. I know there are other answers on this same question, but this should at least mention/link them. | |
Dec 1, 2020 at 2:19 | history | edited | Peter Cordes | CC BY-SA 4.0 |
Mentioning the other options seems appropriate for this highly-voted FAQ answer. Also don't claim that this is truly a 2D array. It only allows similar syntax.
|
Nov 15, 2020 at 4:52 | comment | added | DarkTrick | This should definitely not be the accepted answer! It leads novice programmers in a totally different direction and it contains false statements. Probably the questioner was also mislead and therefore accepted it. I strongly suggest mods to clean this up! | |
Oct 4, 2020 at 17:17 | comment | added | Karan Shah |
If I want to pass arrays declared as T (*ptr)[M] = new T[N][M]; to functions (M and N can be different) by reference, how do I do so?
|
|
Apr 28, 2020 at 14:31 | comment | added | Rahul | better to use vector of vector | |
Jan 6, 2020 at 15:48 | comment | added | Caleth |
@theferrit32 int(*)[rowSize] requires rowSize be constexpr. What does work is int** a = new int*[rowCount]; int* b = new int[rowCount * colCount]; for(int i = 0; i < rowCount; ++i, b += colCount) { a[i] = b; } To cleanup you delete[] *a; delete[]a;
|
|
Apr 17, 2019 at 4:20 | review | Suggested edits | |||
Apr 17, 2019 at 7:16 | |||||
Mar 14, 2019 at 22:28 | comment | added | theferrit32 |
What about int(*a)[rowSize] = (int(*)[rowSize]) new int[rowSize*colSize]; , which accepts both dimensions being variable. Memory addresses are contiguous, only 1 thing to free, and you just cast it to a 2d array pointer with the proper rowSize. With that cast the compiler apparently knows how to set up the correct indexing arithmetic.
|
|
Sep 16, 2018 at 13:17 | comment | added | too honest for this site | @MehrdadAfshari "It can be indexed like a 2D array" - No. It uses the same syntax, but the semantics are very different. You will see the difference if you ever have to use large matrices (aka 2D arrays) on memory-constraint systems or can't use dynamic allocation at all. Or if you have performance issues, i.e. have to optimise datastructures for cache and DRAM access. In addition this is much more complicated and clumsy than using a 2D array. | |
S Feb 15, 2018 at 22:33 | history | suggested | AAEM | CC BY-SA 3.0 |
change the array name to be compatable with the image. And deleting the should word, because the stated methodis not a one to advise other with
|
Feb 15, 2018 at 20:34 | review | Suggested edits | |||
S Feb 15, 2018 at 22:33 | |||||
Jul 14, 2017 at 9:42 | comment | added | Shirkam |
@Kekoa Also, keep in mind delete[] function, that deletes arrays.
|
|
Oct 27, 2016 at 19:07 | comment | added | Mohsin | @MehrdadAfshari how will this work if we want to declare a global 2D array? | |
Sep 17, 2016 at 6:24 | comment | added | nirvanaswap | So... the array WITHIN the array has to be of a constant size?? Well that's not very dynamic is it? Inside that for loop, can't I have colCount to be different values at each iteration?? | |
Jun 9, 2016 at 23:51 | comment | added | Mehrdad Afshari | @TheParamagneticCroissant You can argue it is not a 2D array. It's true. It can be indexed like a 2D array, but it is not a 2D array. The memory layout is in fact portrayed in the picture. The problem with that statement is it fails to work if M is not constant. | |
Jun 9, 2016 at 20:34 | comment | added | The Paramagnetic Croissant |
Oh my God, this is complete garbage, this is utterly wrong. This is no 2D array. "A dynamic 2D array is basically an array of pointers to arrays." ��� NOOOO, FFS! T (*ptr)[M] = new T[N][M]; is the correct solution… No amount of arrays-of-pointers will ever be the same as an array-of-arrays…
|
|
Apr 7, 2016 at 1:23 | comment | added | Praveen Vinny | Plus 1 if you can deallocate the memory allotted by new using the delete operator. | |
Mar 31, 2016 at 4:11 | comment | added | vitaut | This is super wrong. First it's not a two-dimensional array, second the arrays can't be deallocated in case of exception. | |
Oct 19, 2015 at 12:51 | comment | added | Hassan | Can someone explain why we are doing a pre-increment instead of a post-increment? | |
Oct 14, 2015 at 12:21 | history | edited | gsamaras | CC BY-SA 3.0 |
added 11 characters in body
|
Sep 30, 2015 at 22:51 | comment | added | Dagrooms | I like to think of this in a pivoted sense, with a[0] and etc. from left to right on the columns. This way, you can access a[x][y] and have it correctly get the (x, y) coordinate from your array if (0,0) is the top left element of the 2d array. This is the train of thought in graphics programming but there are other ways to think about it depending on the application. | |
Jul 19, 2015 at 20:28 | comment | added | ABcDexter | It is throwing bad_alloc. Why ? | |
Jun 27, 2015 at 22:29 | history | edited | user366312 | CC BY-SA 3.0 |
added 14 characters in body
|
Mar 22, 2015 at 3:41 | history | edited | Mehrdad Afshari | CC BY-SA 3.0 |
swap sizeX and sizeY
|
S May 22, 2014 at 23:09 | history | suggested | gsamaras | CC BY-SA 3.0 |
a pic I made for visualizing what you said. If you like it, accept it, you know better than me!
|
May 22, 2014 at 23:02 | review | Suggested edits | |||
S May 22, 2014 at 23:09 | |||||
Aug 20, 2013 at 11:19 | review | Suggested edits | |||
Aug 20, 2013 at 11:24 | |||||
Sep 3, 2009 at 22:11 | comment | added | Johannes Schaub - litb |
I've changed my mind on the type-system question though. I think "allocated type" doesn't mean to denote a completely different type category. I think it just says it's the type used for allocation. So, an array type can have a size not known at compile time, but you cannot declare an array with that type - instead you have to use new .
|
|
Jun 3, 2009 at 4:14 | vote | accept | user20844 | ||
Jun 1, 2009 at 21:00 | comment | added | Johannes Schaub - litb | More to the point, "array of arrays" and "multidimensional array" is one and the same thing. | |
Jun 1, 2009 at 21:00 | comment | added | Johannes Schaub - litb | Yes, a T[][N] would be called "array of array [N] of T" and be an incomplete type, while T[][] would be an invalid type (all except the last dimensions must have a known size). T[N][M] is "array [N] of array[M] of T", while yours, T[sizeX] is "array [sizeX] of T" where T is a pointer to an int. Creating a dynamically 2d array works like this: new int[X][Y]. It will create an array of an allocated-type int[X][Y]. This is a "hole" in C++'s type system, since the ordinary type system of C++ doesn't have array dimensions with sizes not known at compile time, thus these are called "allocated types" | |
Jun 1, 2009 at 20:56 | history | edited | Mehrdad Afshari | CC BY-SA 2.5 |
corrected the terminology...
|
Jun 1, 2009 at 20:55 | comment | added | Mehrdad Afshari | @litb: Is a[][] considered an "array of arrays" in spec or a multidimensional array? | |
Jun 1, 2009 at 20:53 | comment | added | Johannes Schaub - litb | Also note that this one is an array of pointers. not of arrays. The pointer in turn point to arrays. Important to really correct on the terms, since many tutorials get it wrong too. An array of arrays would be contiguous, which this one is not | |
Jun 1, 2009 at 20:51 | comment | added | Kekoa |
Remember that anything allocated with new is created on the heap and must be de-allocated with delete , just keep this in mind and be sure to delete this memory from the heap when you're done with it to prevent leaks.
|
|
Jun 1, 2009 at 20:45 | history | answered | Mehrdad Afshari | CC BY-SA 2.5 |