Using Delete in class function

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 21
Reputation: Vallnerik25 is an unknown quantity at this point 
Solved Threads: 1
Vallnerik25's Avatar
Vallnerik25 Vallnerik25 is offline Offline
Newbie Poster

Using Delete in class function

 
0
  #1
Mar 5th, 2009
I have a question regarding when to use delete to free memory used by a pointer. I have a class function that uses dynamically allocated memory and I want to properly use delete to free up the space when I am done with it.

  1. // Overloaded addition operator.
  2. CSimpleString operator+(const CSimpleString& aSimpleString) const
  3. {
  4. // Unsinged int variable that represents the length of both strings added together.
  5. size_t len = m_Length + aSimpleString.m_Length + 1;
  6. // Pointer to char.
  7. char* cstr = 0;
  8. // Allocate space for both strings + \0.
  9. cstr = new char [len+1];
  10. // String to hold left operand.
  11. string s1 = (pmessage);
  12. // String to hold right operand.
  13. string s2 = (aSimpleString.pmessage);
  14. // String to add both of them together.
  15. string s3;
  16. // Append both strings to s3.
  17. s3.append(s1);
  18. s3.append(s2);
  19. // Safe copy std::string to const char*
  20. strcpy_s (cstr, len +1, s3.c_str());
  21. return CSimpleString(cstr);
  22.  
  23. }

I just need to know where to place the delete statement and why I need to place it there.

Should it be in the class function before the return statement? In the class function after the return statement? Or in the main function? And where-ever I place it, why is that place the correct place?

The book I"m reading from doesn't really explain the correct placement of the delete statement. It would seem that I would need to use the delete statement in the class function because the dynamically allocated variable is local to the function but if I place it after the return statement will it still be executed?

Thank You.
Is SpongeBob SquarePants supposed to be terrifying?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 12
Reputation: vivekc++ is an unknown quantity at this point 
Solved Threads: 3
vivekc++ vivekc++ is offline Offline
Newbie Poster

Re: Using Delete in class function

 
1
  #2
Mar 5th, 2009
You should delete only when you are done with this memory area.That means you can not delete in overloaded operator function.
In main you will recieve this memory address in another pointer.There if you are not passing it further to any other function and you are sure you won't use this memory area any more you can delete that pointer which will free up this memory allocated to variable cstr.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: Using Delete in class function

 
0
  #3
Mar 5th, 2009
If you are afraid of delete, using auto_ptr can be a good option.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 21
Reputation: Vallnerik25 is an unknown quantity at this point 
Solved Threads: 1
Vallnerik25's Avatar
Vallnerik25 Vallnerik25 is offline Offline
Newbie Poster

Re: Using Delete in class function

 
0
  #4
Mar 5th, 2009
Why wouldn't I want to delete it in the function? At the end of the function I am using the newly created string as a parameter for the class constructor.
  1.  
  2. class CSimpleString
  3. {
  4. public:
  5. // Constructor definition.
  6. CSimpleString(const char* text = "Default Text")
  7. {
  8. pmessage = new char [strlen(text) + 1];
  9. strcpy_s(pmessage, strlen(text) + 1, text);
  10. m_Length = strlen(pmessage);
  11.  
  12. }
  13.  
  14. // Overloaded addition operator.
  15. CSimpleString operator+(const CSimpleString& aSimpleString) const
  16. {
  17. // Unsinged int variable that represents the length of both strings added together.
  18. size_t len = m_Length + aSimpleString.m_Length + 1;
  19. // Pointer to char.
  20. char* cstr = 0;
  21. // Allocate space for both strings + \0.
  22. cstr = new char [len+1];
  23. // String to hold left operand.
  24. string s1 = (pmessage);
  25. // String to hold right operand.
  26. string s2 = (aSimpleString.pmessage);
  27. // String to add both of them together.
  28. string s3;
  29. // Append both strings to s3.
  30. s3.append(s1);
  31. s3.append(s2);
  32. // Safe copy std::string to const char*
  33. strcpy_s (cstr, len +1, s3.c_str());
  34. cout << &cstr << endl;
  35. return CSimpleString(cstr);
  36. // delete [] cstr;
  37. }
  38.  
  39. // Destructor definition.
  40. ~CSimpleString()
  41. {
  42. cout << "Destructor Called." << endl;
  43. delete [] pmessage;
  44. }
  45. private:
  46. size_t m_Length;
  47. char* pmessage;
  48.  
  49. };
  50. };

The class destructor frees up the memory allocated in the constructor and I can't place the delete statement in the class destructor because the compiler doesn't know about the variable, it's out of scope just like it would be if I tried to place the delete statement in the main function.

So this leads me back to my original thought that I need to delete it within the scope of where I declared and allocated it. However, my question still stands if I place the delete statement after the return statement is the delete statement actually getting executed?

The program compiles and executes and performs as expected I'm just wondering if the delete statement is actually being performed and what will happen if its not and is there anyway to test this?
Is SpongeBob SquarePants supposed to be terrifying?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: Using Delete in class function

 
1
  #5
Mar 5th, 2009
Of course, if you put the delete statement after the return statement, it won't be executed at all. Instead you can do like

CSimpleString mySimpleString(cstr);
delete[] cstr;
return mySimpleString;
Last edited by kbshibukumar; Mar 5th, 2009 at 8:24 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Using Delete in class function

 
1
  #6
Mar 5th, 2009
I don't understand why your are interchanging null terminated c style strings with the String class. The string class alone should suffice here and it handles its own memory (and also contains an overloaded + operator). If you are trying to learn about memory management, write the whole code using only char arrays, pull your hair out a bit, then never use them again.
Last edited by skatamatic; Mar 5th, 2009 at 1:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 21
Reputation: Vallnerik25 is an unknown quantity at this point 
Solved Threads: 1
Vallnerik25's Avatar
Vallnerik25 Vallnerik25 is offline Offline
Newbie Poster

Re: Using Delete in class function

 
0
  #7
Mar 7th, 2009
Thank you everyone. skatamatic I didn't write the problem it was just a simple exercise at the end of a chapter to design a class that took a const* char as a parameter and had to have certain requirements. I agree, I thought it was a pretty stupid and I did pull my hair out a bit.

I was really changing null terminated strings with std::strings because I read an article on codeproject.com that covered string types and conversions really well and I wanted to kind of throw that in here too. And kbshibukumar thank you, that should have been obvious to me, I feel rather dumb right now.
Is SpongeBob SquarePants supposed to be terrifying?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC