Vectors and Deleting Allocated Memory

This is not for anything in particular, just for curiosity. If I had to allocate memory in a vector of pointers, how would i delete that allocated memory?

For demonstration purposes, i wrote a program that fills a vector of pointers to type int, and then I allocated memory arrays and filled them with zeros. I then printed the results.

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
#include <iostream>
#include <vector>
using namespace std;

int main()
{

	vector<int*> a_vector;
	const int SIZE = 10;
	int* input = new int[SIZE];

	for (int i = 0; i < SIZE; i++)
	{
		for (int j = 0; j < SIZE; j++)
		{
			input[i] = 0;
		}
		a_vector.push_back(input);
	}

	for (int i = 0; i < a_vector.size(); i++)
	{
		for (int j = 0; j < SIZE; j++)
		{
			cout << a_vector[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}


if i try to reclaim that memory i get a "triggered a breakpoint" exception throwned in Visual Studios.

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
#include <iostream>
#include <vector>
using namespace std;

int main()
{

	vector<int*> a_vector;
	const int SIZE = 10;
	int* input = new int[SIZE];

	for (int i = 0; i < SIZE; i++)
	{
		for (int j = 0; j < SIZE; j++)
		{
			input[i] = 0;
		}
		a_vector.push_back(input);
	}

	for (int i = 0; i < a_vector.size(); i++)
	{
		for (int j = 0; j < SIZE; j++)
		{
			cout << a_vector[i][j] << " ";
		}
		cout << endl;
	}

	for (int i = 0; i < a_vector.size(); i++)
	{
		for (int j = 0; j < SIZE; j++)
		{
			delete a_vector[i];
		}
		cout << endl;
	}
	return 0;
}










Last edited on
if i try to reclaim that memory i get a "triggered a breakpoint" exception throwned in Visual Studios.
The point is that you create input once on line 10. You add the buffer multiple times to a vector and delete it multiple times.

To make it work you need to create the buffer on line 18:

a_vector.push_back(new int[SIZE]);

The better approach would be to avoid new and either having a nested vector or using std::string.
and to be extra clear:
delete it multiple times
is the problem. you can only delete memory you own (from a new statement). You can only do this once -- a lot of programmers set the pointer to null so deleting it a second time is harmless. (when you have one copy, nulling it works).

Copying pointers (which you did here, each vector entry is a copy of the same pointer) leads to confusion around deletes; if you deleted one, you deleted them all, and confusion around the data as well: if you modified the contents (memory pointed to) in one, you modified them all. Pointers require precise coding or you can get into trouble fast.

as a side note: if you wrapped the raw pointer in a class with a delete destructor, it would self clean when vector went out of scope. And there are advanced pointer types as well, but you said it was just playing around code...
Last edited on
also, if you create with new[] (array), then destroy with delete[] (note the brackets)
if you create with new (element) then destroy with delete (note the lack of brackets)

so in your code it should be delete[] a_vector[7]; (¿do you understand why I wrote 7 there?)
thank you everyone!
Topic archived. No new replies allowed.