# [C++] array of arrays

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know i sound like a bit of a n00b for asking this, but i am a bit confused with using an array of arrays. What i am trying to do is make an array that holds pointers to other arrays of different sizes. But i am getting confused with the syntax. I want an array where each element is a pointer to another array, each element of that is a pointer to an object.
// Map.cpp
void Map::create() {
const int size = ...
MapTile** tiles = new MapTile*[size];

for (int i = 0; i < size; i++) {
tiles = MapTile::makeTiles(...);
}
// Create my map from tiles

for (int i = 0; i < size; i++) {
delete[] tiles;
}
delete[] tiles;
}

// MapTile.cpp
MapTile* MapTile::makeTiles(...) {
const int size = ...
MapTile* tiles = new MapTile[size];

for (int i = 0; i < size; i++) {
tiles = new MapTile();
}

return tiles;
}


Now what is wrong with that? From what i read, using the new operator with arrays calls the constructor of each element. But i was thinking of using pointers. Should i just do this:
MapTile* MapTile::makeTiles(...) {
const int size = ...
MapTile* tiles = new MapTile[size];

for (int i = 0; i < size; i++) {
tiles.someVar = blah;
tiles.someOtherVar = blah;
}

return tiles;
}


And initialise the object that way? Also, is that the correct way to delete an array of arrays?

##### Share on other sites
There was an almost identical thread a day or two ago: 2d Dynamic Array. I suggest you read it for all of your answers.

##### Share on other sites
Do yourself a favour and just index into a single array:

some2DArray[x][y] becomes some1DArray[x + y * width]

Requires much less thought, IMHO. Or use boost::multiarray.

##### Share on other sites
Quote:
 Original post by Mike.PopoloskiThere was an almost identical thread a day or two ago: 2d Dynamic Array.

lol, that's almost the exact same thing i'm doing. I must have missed that thread.
I was thinking about using a vector of vectors like someone mentioned, but i already know the sizes of my arrays so i though a vector wouldn't really be any easier. And using a 1D array would probably be more confusing in the long run, i will look back at the code later and wonder "why am i using a 1D array when i need an array of arrays?". I think a 2D array improves readability (and maybe writeability). In my opinion, what's the use of 2D arrays if you are going to use a 1D array and access it in the same way the compiler accesses 2D arrays? 2D arrays are meant to make it easier so you don't have to do [x + y * width]. Just my opinion.

But none of that actually answered my questions.
1) the arrays hold objects, right, and not pointers? i.e. the second bit of code i have is the correct one, yes?
2) How do i delete an array of arrays? Is my code correct?
If i have done it right then there is no need to do it any different.

##### Share on other sites
-- the arrays hold objects, right, and not pointers?
arrays ARE pointers to blocks of memory and that memory can contain anything

-- the second bit of code i have is the correct one, yes?
This is more of a stylistic question. If you have a lot of data in your tile class then option 2 may not be ideal. IMHO your better bet is to create a class hierarchy for your tile variations and use the pointers. With this said, in option 1, this bit of code is a bit strange:

MapTile* tiles = new MapTile[size];

for (int i = 0; i < size; i++) {
tiles = new MapTile();
}

For multidimensional dynamic arrays, you should probably use a single dimensional array as TheUnbeliever suggested. This requires less time to allocate, and will be guaranteed a single block of memory. Among other things, you are less likely to get array out of bounds errors. Iterating through your tile list is easier and it's faster because you can use pointer arithmetic to get to the data you need. Indexing in is a little foreign at first, but you will get used to it quickly, and it is not as confusing as you might initially believe.

--How do i delete an array of arrays? Is my code correct?
with the exception of the issue I pointed out above, your code looks okay. Every new should have a matching delete.

##### Share on other sites
Quote:
 Original post by luckycpparrays ARE pointers to blocks of memory and that memory can contain anything

Um, yeah. I just got confused with trying to create a new object arrays of arrays. I tried to do tiles = new MapTile(); but the compiler said "could not assign MapTile* to MapTile" or something. Then i though that since tiles was a pointer/array, then tiles accessed an object. I don't know, sometimes i just get a bit confused with the syntax.

Quote:
 Original post by luckycppFor multidimensional dynamic arrays, you should probably use a single dimensional array as TheUnbeliever suggested. This requires less time to allocate, and will be guaranteed a single block of memory. Among other things, you are less likely to get array out of bounds errors. Iterating through your tile list is easier and it's faster because you can use pointer arithmetic to get to the data you need. Indexing in is a little foreign at first, but you will get used to it quickly, and it is not as confusing as you might initially believe.

Ok, you've convinced me. If it will be faster then that's always a plus. And i don't really have a problem using pointer arithmetic, it's ok if it's more efficient.

EDIT: oh, but there's a problem. I am returning the arrays from a function, makeTiles. I never really explained the purpose of the code since it wasn't (at the time) relevant to my problem. See, i have a number of images, which is what the first array dimension is for. I call these "composite images" since they are made up of a number of tiles. These tiles are stored in the 2nd array dimension. So my makeTiles function gets an array of tiles from an image and returns the array. If i use a 1D array then i will have to pass that array to my function as well as the starting index.

##### Share on other sites
Quote:
 Original post by luckycpparrays ARE pointers to blocks of memory

No, they're not. Arrays decay to pointers when passed as parameters, but are very definitely not pointers.

##### Share on other sites
Quote:
 Arrays decay to pointers when passed as parameters, but are very definitely not pointers.

From http://www.lysator.liu.se/c/c-faq/c-2.html
	   +---+---+---+---+---+---+	a: | h | e | l | l | o |\0 |	   +---+---+---+---+---+---+	   +-----+     +---+---+---+---+---+---+	p: |  *======> | w | o | r | l | d |\0 |	   +-----+     +---+---+---+---+---+---+

??? Now i'm even more confused! I always thought arrays and pointers were the same. That is, i though that an array is simply a pointer to the first element.

##### Share on other sites
An array is a contiguous sequence (i.e. element 1 is located right after element 0 in memory) of homogeneous (i.e. all of the same type) elements, supporting random access. Try not think of arrays in terms of memory addresses, and rather like this. It makes more sense, if you think about it a bit (you use an array when you want an ordered sequence of values, not when you want a pointer to a value that is followed by a sequence of other values).

To use an awful analogy: it's like asking for a cup of coffee, rather than asking that somebody take coffee beans, roast them, grind them, brew them, filter the result and place a quantity of the result in a cup.

However, arrays will convert to a pointer to the first element in two ways that might confuse: firstly, they convert implicitly (i.e. without an explicit cast) when used where a pointer to the type of the elements they hold; secondly, they decay to pointers when passed as parameters to functions.

##### Share on other sites
Quote:
Original post by XTAL256
Quote:
 Arrays decay to pointers when passed as parameters, but are very definitely not pointers.

From http://www.lysator.liu.se/c/c-faq/c-2.html
*** Source Snippet Removed ***

??? Now i'm even more confused! I always thought arrays and pointers were the same. That is, i though that an array is simply a pointer to the first element.

The array name can be used as (i.e. "decays to") a pointer to the first element. The array can't "be" a pointer to the first element, because an array is the actual element storage space while a pointer only points at it.

Imagine that you give someone a real-world address: i.e., to your house. That consists of a "pointer to beginning of array" (the name of the street you live on) and an offset (your house number). (In C++, you are given a linear model of memory, so those two values get folded into a single number.) The name of the street you live on isn't a street, though. You can live on Maple Rd., but you can't live within the words "Maple Rd." as you speak them to your friend.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631424
• Total Posts
3000008
×