Fixing pointer memory leaks...

I am learning about pointers and so far they really don't seem as complicated as everyone made them out to be. Actually I completely understand (I think)..
1
2
3
4
5
6
7
8
9
10
11
12
//Setup a variable
int myVar = 5;
//Setup a pointer to myVar
int *pVar = &myVar;
//Give address of pointer
cout << &pVar << endl;
//Give address of myVar through pointer
cout << pVar << endl;
//Give address of myVar (same address, different method of last address)
cout << &myVar << endl;
//Read value in memory of myVar through our pointer
cout << *pVar;


May have left out a few things, but ya I pretty much completely understand pointers for the most part. Just this small thing I'm confused about right now is memory leaks. I followed the example in the book I am reading by writing the code below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Allocating and deleting a pointer
#include <iostream>
using namespace std;

int main()
{
	int localVar = 5;
	int *pLocal = &localVar;
	int *pHeap = new int;
	*pHeap = 7;

	cout << "localVar: " << localVar << endl;
	cout << "*pLocal: " << *pLocal << endl;
	cout << "*pHeap: " << *pHeap << endl << endl;

	delete pHeap;
	pHeap = new int;
	*pHeap = 9;

	cout << "*pHeap: " << *pHeap << endl;
	delete pHeap;

	return 0;
}


The only thing I am confused about is why do we have to redeclare pHeap as a new int? I don't really understand the point.. wouldn't this accomplish the same thing..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Allocating and deleting a pointer
#include <iostream>
using namespace std;

int main()
{
	int localVar = 5;
	int *pLocal = &localVar;
	int *pHeap = new int;
	*pHeap = 7;

	cout << "localVar: " << localVar << endl;
	cout << "*pLocal: " << *pLocal << endl;
	cout << "*pHeap: " << *pHeap << endl << endl;

	delete pHeap;
	*pHeap = 9;

	cout << "*pHeap: " << *pHeap << endl;
	delete pHeap;

	return 0;
}
Last edited on
After you use delete, you no longer own the memory pointed to by pheap. Trying to modify is an error.
Alright, thanks a lot I understand now. I was confused because it compiled fine for me with no errors, however when you run it then it crashes.
Last edited on
Just in case anyone else was curious about this, I just coded a program (from my book) that demonstrates the trouble caused by a stray pointer.
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
//Demonstrates a stray pointer
#include <iostream>
using namespace std;
typedef unsigned short int USHORT;

int main()
{
	USHORT *pShort = new USHORT;
	*pShort = 10;

	cout << "*pShort: " << *pShort << endl;
	delete(pShort);

	long *pLong = new long;
	*pLong = 90000;
	
	cout << "*pLong: " << *pLong << endl;

	//This was deleted and was not set up again.. should have been...
	//pShort = new USHORT:
	//*pShort = 20;
	*pShort = 20;

	cout << "*pShort: " << *pShort << "\t\t*pLong: " << *pLong << endl ;

	delete(pLong);

	return 0;
}


This writes the following to console..
1
2
3
*pShort: 10
*pLong: 90000
*pShort: 20                              *pLong: 65556


By following the code you should see that pLong's value was set to 90000 (as you can see in the first output) and I never once changed the value before the second output (which is wrong.) Now even though pLong's value is completely wrong, it is because pShort is now a stray pointer. You can imagine how it would be almost impossible to find the cause of a randomly wrong value in a much larger program because of a stray pointer.

Here is how this happened as quoted from my book...

Here are the details of how 65,556 got into the memory address of pLong in Listing 8.9:
1. pInt was pointed at a particular memory location, and the value 10 was assigned.
2. delete was called on pInt, which told the compiler that it could put something else
at that location. Then, pLong was assigned the same memory location.
3. The value 90000 was assigned to *pLong. The particular computer used in this example
stored the four-byte value of 90,000 (00 01 5F 90) in byte-swapped order.
Therefore, it was stored as 5F 90 00 01.
4. pInt was assigned the value 20—or 00 14 in hexadecimal notation. Because pInt still
pointed to the same address, the first two bytes of pLong were overwritten, leaving
00 14 00 01.
5. The value at pLong was printed, reversing the bytes back to their correct order of 00
01 00 14, which was translated into the DOS value of 65556.

Last edited on
Just to make it clear for any other newbies out there who might read this thread:

The above program exhibits undefined behavior according to the C++ standard. Although OP was able to reproduce the output from the book, there are no guarantees. You can very well expect your compiler to output different results.
Topic archived. No new replies allowed.