| | |
Using Delete in class function
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
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.
c++ Syntax (Toggle Plain Text)
// Overloaded addition operator. CSimpleString operator+(const CSimpleString& aSimpleString) const { // Unsinged int variable that represents the length of both strings added together. size_t len = m_Length + aSimpleString.m_Length + 1; // Pointer to char. char* cstr = 0; // Allocate space for both strings + \0. cstr = new char [len+1]; // String to hold left operand. string s1 = (pmessage); // String to hold right operand. string s2 = (aSimpleString.pmessage); // String to add both of them together. string s3; // Append both strings to s3. s3.append(s1); s3.append(s2); // Safe copy std::string to const char* strcpy_s (cstr, len +1, s3.c_str()); return CSimpleString(cstr); }
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?
•
•
Join Date: Feb 2009
Posts: 12
Reputation:
Solved Threads: 3
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.
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.
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
If you are afraid of delete, using auto_ptr can be a good option.
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.
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?
c++ Syntax (Toggle Plain Text)
class CSimpleString { public: // Constructor definition. CSimpleString(const char* text = "Default Text") { pmessage = new char [strlen(text) + 1]; strcpy_s(pmessage, strlen(text) + 1, text); m_Length = strlen(pmessage); } // Overloaded addition operator. CSimpleString operator+(const CSimpleString& aSimpleString) const { // Unsinged int variable that represents the length of both strings added together. size_t len = m_Length + aSimpleString.m_Length + 1; // Pointer to char. char* cstr = 0; // Allocate space for both strings + \0. cstr = new char [len+1]; // String to hold left operand. string s1 = (pmessage); // String to hold right operand. string s2 = (aSimpleString.pmessage); // String to add both of them together. string s3; // Append both strings to s3. s3.append(s1); s3.append(s2); // Safe copy std::string to const char* strcpy_s (cstr, len +1, s3.c_str()); cout << &cstr << endl; return CSimpleString(cstr); // delete [] cstr; } // Destructor definition. ~CSimpleString() { cout << "Destructor Called." << endl; delete [] pmessage; } private: size_t m_Length; char* pmessage; }; };
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?
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
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.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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.
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.
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?
![]() |
Similar Threads
- About Class function , pls help (C++)
- Calling a member function inside a class (C++)
- stuck with abstract class, function overriding (C++)
- Linked List Delete (C++)
- return 2d array to caller from public class function (C++)
- How to "delete" this memory??? (C++)
- unresolved external symbol when using a hashtable class (C++)
- Destructor help (C++)
- Converting Struct to class lost with pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: slotmachine reel spin simulation
- Next Thread: Is there an easy way to ignore...
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





