Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 12 posts ]  Go to page 1, 2  Next
Author Message
 Post Posted: August 3rd, 2017, 4:19 pm 
 

Joined: August 2nd, 2016, 12:34 am
Posts: 21
Location: Russia, SPb
Hello there!
How can I create an array of pointers in context of memesweeper game? Talk about Tile array in MemeField object.
Code:
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).


Top 
 Profile  
Reply with quote  
 Post Posted: August 3rd, 2017, 11:44 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
Well, an array needs to be statically initialized with a const literal count/size

Code:
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


Top 
 Profile  
Reply with quote  
 Post Posted: August 4th, 2017, 6:55 am 
 

Joined: August 2nd, 2016, 12:34 am
Posts: 21
Location: Russia, SPb
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:
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[i].SomeMethod();, and not like this pTiles[i]->SomeMethod();)
Seems to that operator [] make offset and automatically use "*" operator for get the object. Am I right?
:?: :?: :?:


Top 
 Profile  
Reply with quote  
 Post Posted: August 4th, 2017, 8:46 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
You could do this:
Code:
// 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:
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:
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


Top 
 Profile  
Reply with quote  
 Post Posted: August 4th, 2017, 8:23 pm 
 

Joined: August 2nd, 2016, 12:34 am
Posts: 21
Location: Russia, SPb
Exhaustive answer! :o :o :o
Code:
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.... )))) )


Top 
 Profile  
Reply with quote  
 Post Posted: August 4th, 2017, 10:01 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
Trumps_Nipple wrote:
Exhaustive answer! :o :o :o
Code:
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:
   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


Top 
 Profile  
Reply with quote  
 Post Posted: August 4th, 2017, 11:38 pm 
 

Joined: August 2nd, 2016, 12:34 am
Posts: 21
Location: Russia, SPb
Almost correct.
Code:
 MyClass* ponterToAnArray = new MyClass[5];

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

but in both situations we use dot operator for useing the members?


Top 
 Profile  
Reply with quote  
 Post Posted: August 5th, 2017, 2:12 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
Trumps_Nipple wrote:
Almost correct.
Code:
 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:
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:
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:
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


Top 
 Profile  
Reply with quote  
 Post Posted: August 5th, 2017, 2:22 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
For the homework and for your own sanity, just create a dynamic array using:
Code:
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:
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


Top 
 Profile  
Reply with quote  
 Post Posted: August 5th, 2017, 7:36 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
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


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 12 posts ]  Go to page 1, 2  Next

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: