Domanda

reading this question: What is the difference between int* x[n][m] and int (*x) [n][m]?

the correct reply stated this:

int arr[2][3];
int (*p1)[2][3] = &arr;  // arr decays to int(*)[3], so we need to take the address
int (*p2)[2][3] = new int[42][2][3]; // allocate 42 arrays dynamically

the comment in that last line says it allocates 42 new arrays. how does that work? can someone explain that to me?

thanks!

i would reply on that question, but i can't, so new question it is (:

È stato utile?

Soluzione

new int[3] allocates 3 integers. Now new int[2][3] allocates 2 arrays, each of length 3 integers. Extending this further, new int[42][2][2] allocates 42 arrays, each of length 2 arrays, each of which in turn is, of length 2 integers. Declarations are really a recursive idea.

Altri suggerimenti

Allright, class time.

C++ Multidementional arrays vs. Jagged array

A multidemenional array (in C++) is a block of contiguous memory. It allocates the full number of elements, all in a line, and then access them by combining the index accessors. In essense. If I have an array defined by int x[2][3], it essentially turns x[i][j] into (&x[0][0]+i*3)[j].

sizoef(int[2][3])==24 (4bytes/int*2*3)

This type of array is often called a "static" array, because the size of the array must be allocated at compile time.

Note that the first size is irrelevent to this lookup. This makes it so that, when REFERENCING this type of array, we can exclude the smallest size from the type. This makes is so that both the functions below are valid and can work on the array x declared above:

int foo(int y[2][3]){ return y[1][1];}
int bar(int y[][3]){ return y[1][1];}

Note in this context, sizeof(y)==sizeof(void*), but that is a different problem all together.

The typeing convention for static arrays behaves differently than your used to. Part of the type information comes AFTER the variable declaration. This actually persists in typedefs as well:

typedef int a[4][20];
typedef int b[][20];

If you wanted to take the address of such a value type, then what you need to declare the pointer to this array type. That can be done with:

int (*xptr)[2][3] = &x;

The int (*var)[...] says that var is a pointer to a int[2][3].

When people say C++ arrays are pointers, they are NOT refering to this type of array: the array is a little more compilicated than a pointer here, though we could flatten it into a 1D array.

A jagged array (or dynamic array) is a single block of contiguose memory that is allocated in a linear fashion. This type of array is often called dynamic because the size does NOT need to be known at compile time.

Dynamic arrays are allocated with new or malloc (though you should only use new in C++). These types of array are strictly pointers. When I say int* a=new int[4], I allocate 4 integers: thats it.

We achieve mutlidementionality here by crating jagged arrays, which are arrays of pointers. So for example:

int** a = new int*[2];
for (int i = 0; i < 2; i++) { a[i]=new int[3];}

What your code does

int arr[2][3];//
int (*p1)[2][3] = &arr;  // arr decays to int(*)[3], so we need to take the address
int (*p2)[2][3] = new int[42][2][3]; // allocate a dynamic array (42 elements long) of int[2][3] (sizeof(int[2][3])==24, so this allocates 42*24 bytes!)

It essentially allocates 43 int[2][3] in a row. So it actually comes up with all contiguous memory (though why it needs to be dynamic and not static is beyond me).

Personally, my rule is multidementional arrays "confusing as hell" and only use them in a local context now, but to each their own.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top