943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1902
  • C++ RSS
Mar 5th, 2009
0

Using Delete in class function

Expand Post »
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.

c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 11
Solved Threads: 1
Newbie Poster
Vallnerik25 is offline Offline
21 posts
since Jun 2008
Mar 5th, 2009
1

Re: Using Delete in class function

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.
Reputation Points: 11
Solved Threads: 3
Newbie Poster
vivekc++ is offline Offline
12 posts
since Feb 2009
Mar 5th, 2009
0

Re: Using Delete in class function

If you are afraid of delete, using auto_ptr can be a good option.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 5th, 2009
0

Re: Using Delete in class function

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.
c++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 11
Solved Threads: 1
Newbie Poster
Vallnerik25 is offline Offline
21 posts
since Jun 2008
Mar 5th, 2009
1

Re: Using Delete in class function

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

Quote ...
CSimpleString mySimpleString(cstr);
delete[] cstr;
return mySimpleString;
Last edited by kbshibukumar; Mar 5th, 2009 at 8:24 am.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 5th, 2009
1

Re: Using Delete in class function

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.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Mar 7th, 2009
0

Re: Using Delete in class function

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.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
Vallnerik25 is offline Offline
21 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: slotmachine reel spin simulation
Next Thread in C++ Forum Timeline: Is there an easy way to ignore...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC