increasing array size with 'new'

hello I'm using new / delete to have a dynamic array size as the 'backpack'. Basically I'm creating my own vector(which you can have dynamic sizes, etc)

here is the code example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
using namespace std;

struct Item
{
	string descript;
	int effect;
};

class Player
{
private:
	Item *bp;
	int bpsize;
public:
	Player(Item item)
		:bpsize(0)
	{
		bpsize++;
		bp = new Item[bpsize];
		bp[0] = item;
	}
	~Player()
	{
		delete [] bp;
	}
	void disbp()
	{
		for(int i = 0;i<bpsize;i++)
		{
			cout << i << ". ";
			cout << bp[i].descript << endl;
		}
	}
	void pickup(Item item)	//add to backpack size
	{
		bp = new Item[bpsize++]; //this resets everything inside the array, i dont want this
		bp[bpsize] = item;
	}
};

int main()
{
	Item Potion = {"Potion",30};
	Player User(Potion);
	User.disbp();
	User.pickup(Potion);
	User.disbp();
	cin.get();cin.get();
	return 0;
}


it compiles, but

problem is in body of 'void pickup()'.
I want to increase the size of *bp[] and assign that to the 'item' (Potion), without reseting anything in the array, etc

thanks for any help
Last edited on
You need to allocate a new array, copy the elements over from the old one, delete the old one.
here is what I got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	void pickup(Item item)	//add to backpack size
	{
		Item *bp2 = new Item[bpsize];
		for(int i = 0;i<bpsize;i++)
		{
			bp2[i] = bp[i];
		}
		bpsize++;
		bp = new Item[bpsize];
		for(int i = 0;i<bpsize;i++)
		{
			bp[i] = bp2[i];
		}
		bp[bpsize] = item;

		delete [] bp2;
	}


It compiles, no errors/warnings. but crashes at pickup().
Could you provide an example, or possibly fix?

thanks
Last edited on
bump sorry, I deleted previous msg. any more help appreciated.
Your code seems confused. You seem to be making two new backpacks, and copying the contents around from the old one to a new one and then to another new one, and you've got a memory leak because you never delete the old one.. If you add comments to your code, you'll be able to see what you're trying to do. Read this sample code and hopefully witht he comments it's clear what's happening.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void pickup(Item item)	//add to backpack size
{
  // How big does the new backpack need to be?
  int newBackPackSize = bpsize+1;

  // Make the new backpack, of that size
  Item* newBackpack = new Item[newBackPackSize];

  // copy old backpack across
  for (int i=0; i< bpsize; ++i)
  {
    newBackpack[i] = bp[i];
  }
 
  // add new item to the new backpack
  newBackpack[newBackPackSize-1] = item;
  
  // delete old backpack
  delete[] bp;

  // point bp at the new backpack we just made
  bp = newBackpack;

 // update the backpack size now that bp is the new backpack
  bpsize = newBackPackSize;
}
Last edited on
thankyou. i see what i did wrong now. =)

this is what i got

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	void pickup(Item & item)	//add item to backpack
	{
		bpsize++;
		Item * newbackpack = new Item[bpsize]; //create new bp with increased size

		for(int i = 0;i<bpsize-1;i++)	//old backpack size
		{
			newbackpack[i] = bp[i];   //copy all contents to newbackpack
		}

		newbackpack[bpsize-1] = item;

		delete [] bp;				//destroy everything in here
		bp = new Item[bpsize];			//reset with new size
		for(int i = 0;i<bpsize;i++)
		{
			bp[i] = newbackpack[i];
		}
		delete [] newbackpack;			//dont need this anymore
	}


dont need memory newbackpack. if u see anything wrong lemme no.
Last edited on
one last question. Do I have to apply the same affect If I were to decrease the size by 1(by using a item in the backpack)? Every where I look delete is being used to delete it as a whole, instead of just one element of the 'array' (backpack)

thanks for your help
Last edited on
You cannot delete one element. I would suggest that if you "use" an item, you save yourself the bother of having to create a new array and deleting the old one by simply marking that space as empty. Then, when you need to add another item, first look for empty slots, and only if there are none, go through the bother of expanding the array.

As you're aware, what you're doing here is slowly replicating the functionality of the existing vector<T> class :) Nothing wrong with that. It's good practice.
Last edited on
thanks for the help. I was able to get it working 'perfectly', if anyone cares here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <string>
using namespace std;   //ez read

struct Item
{
	int id;
	string descript;
	int effect;
};const Item EMPTY = {NULL,"EMPTY",NULL};

class Player
{
private:
	Item *bp;
	int bpsize;
	int health;
	int damage;
public:
	Player(Item & item)
		:bpsize(1), health(100), damage(20)
	{
		bp = new Item[bpsize];
		bp[0] = item;
	}
	Player()
		:bpsize(1), health(100), damage(20)
	{
		bp = new Item[bpsize];
		bp[0] = EMPTY;
	}
	~Player()
	{
		delete [] bp;
	}
	void disbp()
	{
		if(bp[0].id==NULL)
		{
			cout << "Empty";
		}
		else
		{
			for(int i = 0;i<bpsize;i++)
			{
				cout << i+1 << ".";
				cout << bp[i].descript << " ";
			}
		}
	}
	void use(int num)
	{
		num--;	//for the cout << i+1 at line 44
		if(num>=bpsize || num < 0)	//error check
		{
			cout << "No such item exists.\n";
		}
		else
		{
			if(bp[num].id==1)	//potion effect adds to health
			{
				this->health += bp[num].effect;
				cout << "Health increased to: " << health << endl;
			}
			else if(bp[num].id==2)
			{
				this->damage += bp[num].effect;
				cout << "Damage increased to: " << damage << endl;
			}
			else {
				cout << "No such item\n";
			}

			if(bpsize<=1)	//dont resize, just empty
			{
				bp[0] = EMPTY;
			}
			else
			{
				bpsize--;		//resize
				Item * newbackpack = new Item[bpsize];	//new backpack with new size

				for(int i=num;i<bpsize;i++) {
					newbackpack[i] = bp[i+1];
				}

				for(int i=0;i<num;i++) {
					newbackpack[i] = bp[i];
				}

				delete [] bp;
				bp = new Item[bpsize];
				for(int i = 0;i<bpsize;i++)
				{
					bp[i] = newbackpack[i];
				}
				delete [] newbackpack;
			}

		}
	}
	void pickup(Item & item)
	{
		bpsize++;
		Item * newbackpack = new Item[bpsize];

		for(int i = 0;i<bpsize-1;i++)	//old backpack size
		{
			newbackpack[i] = bp[i];
		}

		newbackpack[bpsize-1] = item;

		delete [] bp;					//destroy everything in here
		bp = new Item[bpsize];			//reset with new size
		for(int i = 0;i<bpsize;i++)
		{
			bp[i] = newbackpack[i];
		}
		delete [] newbackpack;
	}
};

int main()
{
	Item MPotion = {1,"Minor-Potion",15};
	Item GPotion = {1,"Great-Potion",25};
	Item Axe = {2,"Axe",10};
	Item SStone = {2,"Sharp-Stone",5};
	Item MaPotion = {1,"Major-Potion",35};

	Player User(MPotion);
	User.pickup(GPotion);
	User.pickup(Axe);
	User.pickup(SStone);
	User.pickup(MaPotion);

	User.disbp();

	short int input = 0;
	while(true)
	{
		cin >> input;

		User.use(input);
		cout << endl << endl;
		User.disbp();
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.