new and delete query
I have a function that creates an array of CStrings:
CString* Files;
Files=new CString[NumItems];
After the function does what it does it returns Files.
The query I have is how can I delete Files (delete[]Files) and the return it? I assume I have to delete it but is it the case that when the function returns it automatically deletes it?
My code is working fine as it is (i dont delete[]Files as then i cant return it) but im just wondering if this will lead to memory leaks or something and if so what can i do about it?
Thanks for your time
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
You can delete memory allocated by new[] with a call to delete[]. This will also call the destructor of the objects in the array, so that each element of the array will do its destructor tasks.
If you do not delete the memory allocated by new or new[], the memory won''t be deallocated until the program ends. Windows will then reclaim the memory (at least that''s the case in Win9x and Win2K). If you ''lose'' the pointer to the memory, there''s no way to deallocate it.
So, you should always delete memory you allocate. Also, it''s better to let the compiler do the memory management by allocating objects on the stack instead of the heap. When the object goes out of scope, the memory is freed automatically by the compiler. For a good introduction on this, see this site.
In your case, you''re better of using the STL (=standard template library) class ''vector'' for use as an array. Instead of using CString (which is MFC specific) you can use the string class. An array of strings can then be instantiated as follows:
This will create an empty vector (=array) of string objects, and insert 2 strings in it.
STL may look pretty daunting in the beginning, but once you get the hang of it, it''s very worthwhile to use. STL documentation can be found here.
HTH
If you do not delete the memory allocated by new or new[], the memory won''t be deallocated until the program ends. Windows will then reclaim the memory (at least that''s the case in Win9x and Win2K). If you ''lose'' the pointer to the memory, there''s no way to deallocate it.
So, you should always delete memory you allocate. Also, it''s better to let the compiler do the memory management by allocating objects on the stack instead of the heap. When the object goes out of scope, the memory is freed automatically by the compiler. For a good introduction on this, see this site.
In your case, you''re better of using the STL (=standard template library) class ''vector'' for use as an array. Instead of using CString (which is MFC specific) you can use the string class. An array of strings can then be instantiated as follows:
#include #include using namespace std;int main(){ vector files; // empty vector of strings string s("test.txt"); // string "test.txt" files.push_back(s); // insert s(="test.txt") at the end of the vector s = "test1.txt"; files.push_back(s); // insert s(="test1.txt") at the end of the vector string s1 = files[0]; // s1 = "test.txt" string s2 = files[1]; // s2 = "test1.txt" // vector is automatically ''destroyed'' here, along with its contents return 0;}
This will create an empty vector (=array) of string objects, and insert 2 strings in it.
STL may look pretty daunting in the beginning, but once you get the hang of it, it''s very worthwhile to use. STL documentation can be found here.
HTH
Some useful C++ links:Free multiplatform ANSI C++ Standard Library implementationVisual C++ 6.0 STL fixesVisual C++ 6.0 noncompliance issuesC++ FAQ Lite
Thanks for the reply. I probably should have mentioned that the app im making is an MFC app so I think its better(and easier) to stick with CStrings. I''ll have a look at the site you linked about allocating memory.
Thanks again
Thanks again
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
I personally find CStrings a lot easier to use than std::string, but I avoid MFC''s containers like the plague.
I''m a little worried that you''re talking about deleting somethig that you''re returning. If you return this array, it shouldn''t be deleted; that would mean a function that operated on the returned pointer would be operating on deallocated memory, probably causing an access violation.
Also, in general, it''s good practice to delete things in the context in which they''re created. That is, if you have a function that allocates memory and returns it to the client, you should also have a function that deletes that memory. If you don''t want to do that, have the client allocate the memory, pass the array into the function, and then the client is responsible for deleting it.
In other words, you don''t want to split up "new" and "delete" into two different levels in your architecture--it makes the program more confusing. Rather, strive for symmetry in the calls: if memory''s allocated in the constructor, it should be deleted in the destructor; if it''s allocated by an "AllocateFoo ()" function, there should be a "DeallocateFoo" function; if the client allocates, the client should delete.
I''m a little worried that you''re talking about deleting somethig that you''re returning. If you return this array, it shouldn''t be deleted; that would mean a function that operated on the returned pointer would be operating on deallocated memory, probably causing an access violation.
Also, in general, it''s good practice to delete things in the context in which they''re created. That is, if you have a function that allocates memory and returns it to the client, you should also have a function that deletes that memory. If you don''t want to do that, have the client allocate the memory, pass the array into the function, and then the client is responsible for deleting it.
In other words, you don''t want to split up "new" and "delete" into two different levels in your architecture--it makes the program more confusing. Rather, strive for symmetry in the calls: if memory''s allocated in the constructor, it should be deleted in the destructor; if it''s allocated by an "AllocateFoo ()" function, there should be a "DeallocateFoo" function; if the client allocates, the client should delete.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement