Jump to content
  • Advertisement
Sign in to follow this  
programmermattc

Problems with Pointers

This topic is 4495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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).

Share this post


Link to post
Share on other sites
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... :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!