I want to set up definitions of all the tetris:
#include <iostream>
#include <vector>
struct Tetris{
std::vector<int> data;
int getAt(int x, int y){return data.at(y*4+x);};
} tetris[6];
int main(){
//stick
tetris[0].data.resize(32);
int array0[32]={0,0,0,0,
0,0,0,0,
1,1,1,1,
0,0,0,0,
0,1,0,0,
0,1,0,0,
0,1,0,0,
0,1,0,0};
tetris[0].data.assign(array0,array0+32);
//brick
tetris[1].data.resize(16);
int array1[16]={0,0,0,0,
0,1,1,0,
0,1,1,0,
0,0,0,0};
tetris[1].data.assign(array1,array1+16);
...
}
In this way I'll need to define 6 arrays just for storing initialization data(array0, array1...), which is useless after the initialization. This seems quite inefficient and a waste of memory. I'm wondering if there's a way to delete those data after each usage?
Update:
If I want to reuse array0, say
tetris[0].data.resize(32);
int array0[32]={...};
tetris[0].data.assign(array0,array0+32);
//brick
tetris[1].data.resize(16);
delete array0;
int array0[16]={...};
tetris[1].data.assign(array0,array0+16);
...
The compiler will report error of "array0 redefinition". Is delete not working in this case?
If you would like to control the lifetime of statically allocated types, you can add in arbitrary scope resolution operators to do so.
int main()
{
{
//stick
tetris[0].data.resize(16);
int array0[16]={0,0,0,0,
0,0,0,0,
1,1,1,1,
0,0,0,0};
tetris[0].data.assign(array0,array0+16);
//brick
tetris[1].data.resize(16);
int array1[16]={0,0,0,0,
0,1,1,0,
0,1,1,0,
0,0,0,0};
tetris[1].data.assign(array1,array1+16);
}//THISONE
//Do the rest of the work of main, without the pesky arrays sticking around.
}
In this example main, your tetris variable is still alive, because it is global. But those arrays you declared will not stick around past "THISONE".
Note that I don't necessarily recommend this approach. Creating a class, or using some other more standard approach is preferable. But occasionally this is a nice little trick, to avoid using dynamic allocations that aren't necessary.
EDIT: This approach is probably better, though be sure you understand everything that is going on, otherwise you might be better off sticking with what you know. Particularly if you don't understand all of the bad things I did in here, in order to get you an example quickly.
#include <iostream>
using namespace std;
class stick;//Forward declaration so shape can make stick a "friend"
class shape {//This classe declaration should go in a .h file.
static const int WIDTH = 4;
static const int HEIGHT = 4;
int array[WIDTH][HEIGHT];
public:
shape(){
for(int i = 0; i < HEIGHT; i++) {
for(int j = 0; j < WIDTH; j++) {
array[i][j] = 0;
}
array[i][i] = 1;
}
}
void printShape() {
for(int i = 0; i < HEIGHT; i++) {
for(int j = 0; j < WIDTH; j++) {
cout << array[i][j] << " ";
}
cout << endl;
}
}
friend class stick;
};
class stick: public shape {//This declaration should also go in a header file
public:
stick() {
for(int i = 0; i < HEIGHT; i++) {
for(int j = 0; j < WIDTH; j++) {
if(i == 0) array[i][j] = 1;
else array[i][j] = 0;
}
}
}
};
int main(){
shape planeShape;
stick stickShape;
cout << "PLAIN SHAPE: " << endl;
planeShape.printShape();
cout << endl << "STICK SHAPE: " << endl;
stickShape.printShape();
cout << endl << "PLANE SHAPE POINTER:" << endl;
shape* shapePointer = &planeShape;
shapePointer->printShape();
cout << endl << "STICK SHAPE POINTER:" << endl;
shapePointer = &stickShape;
shapePointer->printShape();
}
The instructions that constitute a program live in memory. So the fact that you're setting the values of array0 explicitly with array0[n]={0,0,1,...} means that the values are already stored in memory via the code that assigns them and cannot be deleted.
Even if you switched to using pointers and dynamically allocated arrays, you would still have the contents of the array ever-present in memory due to the fact that they are hard-coded in the code that populates the arrays.
I would just go with your original setup, except don't re-use array0. Have a different array for each case where you were going to use array0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With