Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with Pointers

Started by programmermattc May 29, 2006 at 11:24 PM 5 replies 650+ views
Original Post
programmermattc
programmermattc
Hey everyone, I've been working on pointers recently and learning stuff about them and I've hit a snag with the 'delete' statement. Below is the code I'm testing.

int main()
{
	using namespace std;
	unsigned short int myVar = 5, yourVar = 20, hello;
	unsigned short int * pVar = new unsigned short int;

	pVar = &myVar //pVar is now pointing to myVar
	*pVar = yourVar; //myVar equals yourVar
	cout << "myVar:\t" << myVar << endl;
	cout << "yourVar:\t" << yourVar << endl;

	delete pVar;

	cin >> hello;

	return 0;
}
Obviously just a basic console program that changes the value of the first variable by changing the pointer value. The problem occurs after the second 'cout' so obviously it's in my delete or cin. (The cin is only used to pause the program at the end). I've tried other delete's in my compiler (Visual Studio .NET) and they've worked fine. It says it is giving me an 'assertion' error. Can anyone see what's wrong?
=============================RhinoXNA - Easily start building 2D games in XNA!Projects
omgomghilol
omgomghilol
You change the value of pVar to the address of something allocated on the stack (in this case myVar). This leaks the memory of whatever it was originally pointing to since it can no longer be accessed or deleted. And your delete is where you crash because you try to free something allocated on the stack (you only delete heap allocated objects).
Falken42
Falken42
You have changed the location of pVar to point to myVar instead of the newly allocated memory you gave it. This is why when you call delete on pVar, it tries to delete memory that it does not own, and either asserts or gets ignored (because the pointer is not a valid location in the memory pool).

You do not need to allocate space for a new unsigned short int when declaring a pointer. Simply use:
unsigned short int *pVar;

Likewise, you do not need to delete it.

Good luck!

Edit: too slow... :)
jxkwl
jxkwl
The delete operator can only be applied to a pointer returned by new. (Or to zero). When you assigned pVar to the address of myVar, you assigned it to a pointer value that was not created by new, and the delete operator will now no longer work on it. You have also lost the pointer value that your new call returned, meaning that you can never delete this memory.
programmermattc
programmermattc
Ok I think I get it (be patient I'm a pointer n00b), so basically I would only use the delete if the pointer I was deleting wasn't pointing to a specific variable in memory? (If instead it was set to like 7 and not &myVar)
=============================RhinoXNA - Easily start building 2D games in XNA!Projects
jxkwl
jxkwl
I don't think you quite have it yet. When you use the new operator, this operator returns a pointer to memory it allocated from the heap. Unlike local variables such as your myVar and yourVar that automatically go out of scope and are destroyed when the function ends, the memory allocated by new is persistent and must be de-allocated using delete.
programmermattc
programmermattc
Yup I get it now. I just read this long part in this book about the heap and the other 4 memory areas and understand it now. The heap was so much more complex than the others I had to really read it before taking it in. Thanks everyone.
=============================RhinoXNA - Easily start building 2D games in XNA!Projects

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.