6 Replies - 1666 Views - Last Post: 18 March 2013 - 05:24 AM Rate Topic: -----

#1 richardm1985  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 31
  • Joined: 28-November 12

Understanding "delete" in an assignment operator

Posted 16 March 2013 - 12:14 AM

Here is the code:

class HasPtr
{
	public:
		HasPtr(const string &s = string()) : ps(new string(s)), i (0) {}
		HasPtr(const HasPtr &hp) : ps(new string(*hp.ps)), i(hp.i) {}
		HasPtr& operator=(const HasPtr &hp);
		~HasPtr(){delete ps;};

	private:
		string *ps;
		int i;
};

HasPtr& HasPtr::operator=(const HasPtr &hp)
{
	auto newp = (new string(*hp.ps));
	delete ps;
	cout << *hp.ps << endl;;
	ps = newp;
	i = hp.i;
	return *this;
}


I added the cout below "delete ps;" so I could see if hp.ps data still existed or deleted...but it still exists...so my question is...When I try:

HasPtr one("richard"), two;
two = one;

I'm rather confused about the whole concept so my question might seem confusing, but what the hell is happening with the delete??? What am I deleting? Am I deleting what ps points to? So shouldn't "cout << *hp.ps;" fail because *ps is pointing to that value??

Ran debugger a few times to try to understand what is happening. It seems that ps is apart of "*this" and if I dont use delete, than whatever string I place in "burger" will just be sitting in memory with no way to access it. I think I just answered my question thanks guys!

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding "delete" in an assignment operator

#2 richardm1985  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 31
  • Joined: 28-November 12

Re: Understanding "delete" in an assignment operator

Posted 16 March 2013 - 12:38 AM

Ran debugger a few times to try to understand what is happening. It seems that ps is apart of "*this" and if I dont use delete, than whatever string I place in "burger" will just be sitting in memory with no way to access it. I think I just answered my question thanks guys!
Was This Post Helpful? 3
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Understanding "delete" in an assignment operator

Posted 16 March 2013 - 01:47 AM

As you've discovered delete just removes the reference to that region of memory, it doesn't wipe the location's value.
Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Understanding "delete" in an assignment operator

Posted 16 March 2013 - 02:24 AM

Quote

Ran debugger a few times to try to understand what is happening


You, sir, are well on your way to becoming an actual software developer! Huzzah!
Was This Post Helpful? 1
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Understanding "delete" in an assignment operator

Posted 16 March 2013 - 06:21 AM

Agreed! So refreshing! :)
Was This Post Helpful? 1
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Understanding "delete" in an assignment operator

Posted 17 March 2013 - 07:39 PM

Deallocating memory with delete does not wipe the memory, rather it just tell's the OS that memory location is fair game for other uses.
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: Understanding "delete" in an assignment operator

Posted 18 March 2013 - 05:24 AM

If you use the VS2010 and VS2012 compilers and use the debug version of the C++ runtime library, it can be configured to write a specific bit pattern into the memory that has been freed to help diagnose errors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1