Array of pointers VS pointer to an array (memesweeper game)

The Partridge Family were neither partridges nor a family. Discuss.
Trumps_Nipple
Posts: 27
Joined: August 2nd, 2016, 12:34 am
Location: Russia, SPb
Contact:

Array of pointers VS pointer to an array (memesweeper game)

Post by Trumps_Nipple » August 3rd, 2017, 4:19 pm

Hello there!
How can I create an array of pointers in context of memesweeper game? Talk about Tile array in MemeField object.

Code: Select all

Tile * pTiles[];
?
If it's correct how I could initialize in cstruct? (here I have some troubles while trying to solve it on my own).

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Array of pointers VS pointer to an array (memesweeper ga

Post by albinopapa » August 3rd, 2017, 11:44 pm

Well, an array needs to be statically initialized with a const literal count/size

Code: Select all

static constexpr int numTiles = 1024;
Tile *pTiles[numTiles];
for(int i = 0; i < numTiles; ++i)
{
     // First allocate each pointer in the array
     pTiles[i] = new Tile;
}
// To access the elements, since each element in the array is now a pointer.
pTiles[i]->Draw(gfx);
// To deallocate
for(int i = 0; i < numTiles; ++i)
{
     delete pTiles[i];
     pTiles[i] = nullptr;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Trumps_Nipple
Posts: 27
Joined: August 2nd, 2016, 12:34 am
Location: Russia, SPb
Contact:

Re: Array of pointers VS pointer to an array (memesweeper ga

Post by Trumps_Nipple » August 4th, 2017, 6:55 am

Ok man, It's clear. But what if MemeField allocated dynamicly too? I mean the only way to allocate array with not fixed size it's: ?

Code: Select all

Tile* pTile = new Tile[some not constant value which calculated in runtime];
Because I don't have the field in stack, it's exist in heap so I don't see restrictions to make it with fixed size as object's member.
And additional question: :geek:
if we have a pointer to an array like previous _Tile* pTile;_
...why we call Tile's methods directly? Like with reference or with object himself? Directly
(we use pTiles.SomeMethod();, and not like this pTiles->SomeMethod();)
Seems to that operator [] make offset and automatically use "*" operator for get the object. Am I right?
:?: :?: :?:

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Array of pointers VS pointer to an array (memesweeper ga

Post by albinopapa » August 4th, 2017, 8:46 am

You could do this:

Code: Select all

// Setup the variables so that there are three fixed sizes
static constexpr int smallArraySize = 10 * 10;
static constexpr int mediumArraySize = 20 * 20;
static constexpr int largeArraySize = 30 * 30;

// Setup the three sized arrays
Tile smallTileArray[ smallArraySize ];
Tile mediumTileArray[ mediumArraySize ];
Tile largeTileArray[ largeArraySize ];

// Setup a default pointer to point to one of the arrays
Tile *pTiles = smallTileArray;

// Once you figure out which array the user clicks on, Small, Medium or Large
if( sizeOption == Small )
{
     pTiles = smallTileArray;
}
else if( sizeOption == Medium )
{
     pTiles = mediumTileArray;
}
else 
{
     pTiles = largeTileArray;
}
Nothing is dynamically allocated, it's all on stack.

Another way would be:

Code: Select all

int dynamicAllocatedCount ; // Assigned in constructor
Tile **ppTiles = new Tile*;
// dereference the pointer to create the underlying array of Tile objects
*ppTiles = new Tile[dynamicAllocatedCount];

// Accessing the elements
for(int i = 0; i < dynamicAllocatedCount; ++i)
{
     (*ppTiles)[ i ].someMember;
}
// The reason for this is, the first pointer is to the array of pointers.  
// If you indexed off the first pointer, it no longer points to the array.

// Deallocating
delete [] (*ppTiles);
delete ppTiles;

Another way is to make everything a pointer:

Code: Select all

int numTiles // Asigned in constructor
Tile **ppTiles = new Tile *[numTiles];
for(int i = 0; i < numTiles; ++i)
{
     // This time we have an array of pointers instead, so we can use the index operator [] 
     // on the first pointer
     ppTiles[ i ] = new Tile;
}

// Accessing
for(int i = 0; i < numTiles; ++i)
{
     ppTiles[i]->someMember;  // or if  you want to use the dot operator ( . )
     // you have to index into the array of pointers, then dereference the pointer at that
     // element, I'm not 100% about order of operations here, so I use the parentheses.
     (*( ppTiles[i] ) ).someMember;
}

// Deallocate
for(int i = 0; i < numTiles; ++i)
{
     // First, deallocate all the pointer elements
     delete ppTiles[i];
     ppTiles[i] = nullptr;
}

// Then, you delete the pointer array
delete [] ppTiles;
ppTiles = nullptr;
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Trumps_Nipple
Posts: 27
Joined: August 2nd, 2016, 12:34 am
Location: Russia, SPb
Contact:

Re: Array of pointers VS pointer to an array (memesweeper ga

Post by Trumps_Nipple » August 4th, 2017, 8:23 pm

Exhaustive answer! :o :o :o

Code: Select all

class MyClass
{
public:
	int GetTrue() { return 42; }
private:
};

void main() {
	MyClass* pointer = new MyClass();
	MyClass* ponterToAnArray = new MyClass[5];
	MyClass* arrayOfPointers[5];

	for (int i = 0; i < 5; i++)
	{
		arrayOfPointers[i] = new MyClass();
	}

	pointer->GetTrue();
	ponterToAnArray[4].GetTrue();
	arrayOfPointers[4]->GetTrue();
}
It's correct? (I wanted to send it here before your answer so.... )))) )

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Array of pointers VS pointer to an array (memesweeper ga

Post by albinopapa » August 4th, 2017, 10:01 pm

Trumps_Nipple wrote:Exhaustive answer! :o :o :o

Code: Select all

class MyClass
{
public:
	int GetTrue() { return 42; }
private:
};

void main() {
	MyClass* pointer = new MyClass();
	MyClass* ponterToAnArray = new MyClass[5];
	MyClass* arrayOfPointers[5];

	for (int i = 0; i < 5; i++)
	{
		arrayOfPointers[i] = new MyClass();
	}

	pointer->GetTrue();
	ponterToAnArray[4].GetTrue();
	arrayOfPointers[4]->GetTrue();
}
It's correct? (I wanted to send it here before your answer so.... )))) )

Almost correct.

Code: Select all

	MyClass* ponterToAnArray = new MyClass[5];
This is still just basically an array, the same as MyClass array[5];

Notice how you acess: pointerToAnArray[4].GetTrue()

You're using the dot operator, same as:
MyClass arrMyClass[5];
arrMyClass[4].GetTrue();
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Trumps_Nipple
Posts: 27
Joined: August 2nd, 2016, 12:34 am
Location: Russia, SPb
Contact:

Re: Array of pointers VS pointer to an array (memesweeper ga

Post by Trumps_Nipple » August 4th, 2017, 11:38 pm

Almost correct.

Code: Select all

 MyClass* ponterToAnArray = new MyClass[5];
But now It's exist in heap, not in stack? Or...
I mean

Code: Select all

MyClass* ponterToAnArray = new MyClass[5]; // heap
MyClass ponterToAnArray = new MyClass[5];   // stack
but in both situations we use dot operator for useing the members?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Array of pointers VS pointer to an array (memesweeper ga

Post by albinopapa » August 5th, 2017, 2:12 am

Trumps_Nipple wrote:Almost correct.

Code: Select all

 MyClass* ponterToAnArray = new MyClass[5];
But now It's exist in heap, not in stack? Or...
Nope, it doesn't exist on the stack. You are allocating 5 MyClass objects on the heap, and new returns the address of that array to pointerToAnArray.
Trumps_Nipple wrote: I mean

Code: Select all

MyClass* ponterToAnArray = new MyClass[5]; // heap
MyClass ponterToAnArray = new MyClass[5];   // stack
but in both situations we use dot operator for useing the members?

Code: Select all

MyClass ponterToAnArray = new MyClass[5];   // stack
This won't even work, because now pointerToAnArray is an object on the stack, not a pointer at all.
The only time you allocate an array on the heap is:

Code: Select all

MyClass arr[5];
Any time you use operator new or operator new[] in C++, it's allocated on the heap.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Array of pointers VS pointer to an array (memesweeper ga

Post by albinopapa » August 5th, 2017, 2:22 am

For the homework and for your own sanity, just create a dynamic array using:

Code: Select all

Tiles *pTiles = new Tile[numTiles];
This will allow you to allocated and runtime as many tiles as you need until you run out of memory or draw off screen, which ever comes first.

There are instance, where you might want an array of pointers, and to do that I would suggest:

Code: Select all

MyClass **ppMyClass = new MyClass*[ numPointers ];
for(int i = 0; i < numPointers; ++i)
{
     ppMyClass[i] = new MyClass;
}

ppMyClass[i]->GetTrue();
// or, double pointer needs double dereference.
(*ppMyClass[i]).GetTrue();

// Just make sure to deallocate in the order you allocated
for(int i = 0; i < numPointers; ++i)
{
     delete ppMyClass[i];
     ppMyClass[i] = nullptr;
}
// Then delete the array that was holding the pointers
delete [] ppMyClass;
ppMyClass = nullptr;
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Array of pointers VS pointer to an array (memesweeper ga

Post by albinopapa » August 5th, 2017, 7:36 am

Hey, do you play Rocket League? I'm pretty sure I've played against Trumps_Nipple before, never with :)
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply