Stackoverflow-Com
Stackoverflow-Com
Stackoverflow-Com
Learn
Products Search… Log in Sign up
more
Questions
multidimensional array and
Tags
allocating the array
Asked 13 years, 7 months ago Modified 4 years, 1 month ago Viewed 94k times
Users
Companies
I've tried looking but I haven't found anything with a definitive
answer. I know my problem can't be that hard. Maybe it's just that
LABS 28 I'm tired..
COLLECTIVES
int* array;
Communities for your favorite array = new int[somelength];
technologies. Explore all
Collectives
int* array;
array = new int[someheight][somewidth];
Ask questions, find answers
and collaborate at work with
Stack Overflow for Teams. The compiler gives me an error stating that ‘somewidth’ cannot
appear in a constant-expression. I've tried all sorts of combinations
Explore Teams
of ** and [][] but none of them seem to work. I know this isn't that
Create a free Team complicated...Any help is appreciated.
Add a comment
Customize settings
Report this ad
12 Unlike the other answers here, this one actually answers the question,
which was how to declare a pointer to a multidimensional array - not a
jagged array. This is an important differentiation for performance-
sensitive applications. It works if "somewidth" is a constant, and only the
"someheight" changes. If you look at how this 2D array is laid out in
memory - it's all together and contiguous, with no more than the single
"new" memory allocation done. The answer by Alexander Rafferty is also
good if the width is also dynamic. The other answers are slow because
they do a ton of mallocs... – James Johnston Jan 14, 2014 at 16:03
1 How do you properly destroy this array? delete[] array; I assume so since
it's really a contiguous piece of memory. – Dicer Mar 1, 2019 at 21:53
Just to clarify James Johnston's important note. Arrays and pointers are
not the same: an array can decay into a pointer, but a pointer doesn't
carry state about the size/configuration of the data to which it points.
This prevents using pointers of pointers as a contiguous memory. Still
the decay of an N dimensional array to a pointer to N-1 dimensional
array is allowed by c++ since you can lose the leftmost dimension and
still being able to correctly access array elements with N-1 dimension
information. Details in stackoverflow.com/a/3925968/1201614 – luca Apr
4, 2020 at 17:37
Add a comment
I just found this ancient answer still gets read, which is a shame
since it's wrong. Look at the answer below with all the votes instead.
17
Read up on pointer syntax, you need an array of arrays. Which is the
same thing as a pointer to a pointer.
int width = 5;
int height = 5;
int** arr = new int*[width];
for(int i = 0; i < width; ++i)
arr[i] = new int[height];
Share Improve this answer Follow edited May 23, 2017 at 12:25
Community Bot
1 1
2 Did you try this? It doesn't compile (if height and width are both
variables) – The Archetypal Paul Oct 11, 2010 at 7:32
Yes I have tried this and still get the same error with the compiler. it will
say that "‘width’ cannot appear in a constant-expression" – vince88 Oct
11, 2010 at 7:32
It is very well specified that when declaring a 2D-Array the row argument
can be a variable , but the column argument has to be constant only.
– krishna Nov 26, 2014 at 4:37
3 No, array of array and array of pointers are not the same thing. Why is
this post accepted ? The answer of Tony The Lion is the correct one, this
one is completely wrong. – Bregalad Mar 23, 2015 at 15:10
Add a comment
Share Improve this answer Follow answered Oct 11, 2010 at 7:32
Arun
20.1k 10 52 60
2 This is an array of arrays, not really a 2D one. – Alexander Rafferty Oct 11,
2010 at 7:38
6 @ArunSaha, this is old, but to answer your question and for anyone else
that stumbles across this, a 2-D array is contiguous in memory; a
dynamic array of dynamic arrays is contiguous in the first dimension, but
each array in the second dimension is stored separately – Stephen Lin
Mar 14, 2013 at 3:08
8 #define WIDTH 3
#define HEIGHT 4
Share Improve this answer Follow answered Oct 11, 2010 at 7:41
Alexander Rafferty
6,193 4 34 57
Add a comment
void bar (int *p, int nz, int ny, int nx) {
int (*A)[ny][nx] = (int(*)[ny][nx]) p;
#include <iostream>
using namespace std;
void bar (int *p, int nz, int ny, int nx)
{
int (*A)[ny][nx] = (int(*)[ny][nx]) p;
void out (int *p, int nz, int ny, int nx)
{
int (*A)[ny][nx] = (int(*)[ny][nx]) p;
cout << A[11][22][33] << endl;
}
int main (void)
{
int NX = 97;
int NY = 92;
int NZ = 20;
int *space = new int [NZ * NY * NX];
The declaration of the "A" alias is a little weird looking, but it allows
you to directly and simply use the desired multi-dimensional array
syntax
Add a comment
1 int r, c ;
std::cin>>r>>c ;
int *array = new int[r*c] ;
Add a comment
Your Answer
Sign up or log in
Sign up using Google
Post as a guest
Name
Email
Required, but never shown
Post Your By clicking “Post Your Answer”, you agree to our terms of service and
Answer acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged
c++ pointers multidimensional-array or ask your own question.
Featured on Meta
Linked
Related
Order of numerical solver when calculating difference between forwards and backwards
solution
I lost the keys for a gate's lock. Where can I find a replacement cylinder or how can I get
new keys?
Would it count as a story if nothing bad happens to the character anywhere in the story?
Proverb for someone who mistakenly assumes he has found the right answer and is
unwilling to accept his error?
How to provide opportunities for role playing in a single player campaign focused on
travelling and wilderness survival?
I cleaned the grease off an old floppy drive’s motor; do I need to lubricate it?
Why are the Lion Air and Ethiopian Airlines crashes being litigated in the US?
In the US, does the Social Security benefit calculator assumes you work until the retire
age?
Question feed
STACK OVERFLOW
Questions Help
PRODUCTS
Teams Advertising Collectives Talent
COMPANY
About Press Work Here Legal Privacy Policy Terms of Service Contact Us Cookie Settings Cookie Policy
Site design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2024.5.15.9214