Jump to content
  • Advertisement
Sign in to follow this  
directNoob

[C++]Delete Strings from the heap

This topic is 4417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Suppose I have pointer Array like this:
TCHAR *pStringArray[500];
So, now I have an Array of TCHAR pointers, 500. First question: Is now
pStringArray;
a double pointer to every single TCHAR obj? Second question, according to the topic: If I had a String on the heap, and one pointer of the pointers in that Array is refering to it like so,
TCHAR *pString = TEXT("This initialisation may not be correct...");
pStringArray[0] = new TCHAR[nSize];
// copy procedure...
// copy pString in pStringArray[0]
(nSize is the size of pString) how could I delete/clean this allocated memory correctly? I mean, I want to clean pStringArray[0] completely. Ok, I could call
delete pStringArray[0];
// oh wait, pStringArray[0] is an Array of TCHARs, so I have to call
delete [] pStringArray[0];
But what can I do else? Is this possible:
pStringArray[0] = "\0";
Thanks Alex

Share this post


Link to post
Share on other sites
Advertisement
edit: Enigma has a better explanation than I do - and he's corrected me on arrays vs pointers. That said my std::string recommendation still stands

pStringArray would be a pointer to an array of pointers to TCHARs - so it's a TCHAR**. As to your second question you would do

for(int i=0; i<500; i++)
delete[] pStringArray[];

delete[] pStringArray

However, since you are using C++ I recommend using

std::vector<std::string> StringArray;

instead of raw arrays

Share this post


Link to post
Share on other sites
Quote:
Original post by directNoob (paraphrased)
Is pStringArray; a double pointer to every single TCHAR object?

pStringArray is an array of five-hundred contiguous pointers to TCHARs, something like:
+------+------+------+------+-   -+------+------+------+
|TCHAR*|TCHAR*|TCHAR*|TCHAR*| ... |TCHAR*|TCHAR*|TCHAR*|
+------+------+------+------+- -+------+------+------+

Each of those TCHAR *'s can point to the first of a contiguous set of zero or more TCHARs, i.e.:
+-----+-----+-----+-----+
|TCHAR|TCHAR|TCHAR|TCHAR|
+-----+-----+-----+-----+
^
| +-----+-----+-----+- -+-----+-----+
| |TCHAR|TCHAR|TCHAR| ... |TCHAR|TCHAR|
| +-----+-----+-----+- -+-----+-----+
| ^
| | +-----+
| | |TCHAR|
| | +-----+
| | ^
+------+------+------+------+- -+------+------+------+
|TCHAR*|TCHAR*|TCHAR*|TCHAR*| ... |TCHAR*|TCHAR*|TCHAR*|
+------+------+------+------+- -+------+------+------+

pStringArray also has an implicit decay to type TCHAR * *. This means that anytime you use pStringArray in a capacity where the compiler is expecting a TCHAR * * is will perform an automatic conversion. A TCHAR * * is different to a TCHAR *[500] because the former is a pointer to the first of an unspecified number of TCHAR *s and the latter is not a pointer, it actually is 500 TCHAR *s, although they can be indexed identically.

Quote:
Original post by directNoob (paraphrased)
If I had a String on the heap, and one pointer of the pointers in that Array is refering to it like so,
TCHAR *pString = TEXT("This initialisation may not be correct...");
pStringArray[0] = new TCHAR[nSize];
// copy procedure...
// copy pString in pStringArray[0]

First things first. The line TCHAR *pString = TEXT("This initialisation may not be correct..."); is bad form. The type of TEXT("This initialisation may not be correct...") is TCHAR const * and you are assigning it to a non-const TCHAR *. C++ allows this conversion for historical reasons, but it is deprecated.

What you should really do is use an array (or std::vector, boost::array or other container) of std::string or std::wstrings (which can be linked to a typedef in a similar manner to the way in which the TCHAR typedef resolves to char or wchar_t. This will use modern C++ RAII techniques to handle all memory management for you and you will not have to worry about what type of delete to use and whether or not you are leaking memory.

Σnigma

Share this post


Link to post
Share on other sites
I third the recommendation that you use std::string (and optionally std::vector).

If you want a character encoding dependent string, you can use a typedef like the following:

typedef std::basic_string< TCHAR > tstring;


jfl.

Share this post


Link to post
Share on other sites
Hi, and thanks for your immediate response to all.

@Enigma
You wrote:
TCHAR const *

Doesn´t have it to be more like
const TCHAR *
?
Because the type isn´t changing...

Oh, wait, the meaning of it is another...
500 constant pointers on TCHARs.

Does it mean, that every pointer refers to a constant TCHAR obj?
Sorry asking about this basic stuff.

Let´s come to the STL string class.
I think, this is a very good and very comfortable thing, but, if you want to get into the programming thing, I think, this is distorting the truth.
Think of a programming beginner, who says that it is mors convenient to make something like this:

std::string1 = "Hello";
std::string2 = string1;

You don´t really know what exactly is happening when you use this STL stuff, regarding the string copy action, because the beginner never lerns it, and the beginner could never reach a higher level of progamming.
I prefer to code the hard way first, to understand what´s going on, and then switch to the more convenient structures.
But on the other hand, if you really want to know what´s going on, lern assembler...

But you are absolutly right, the STL is a great thing in programmers life.

Thanks again
Alex

Share this post


Link to post
Share on other sites
const TCHAR *
and TCHAR const *
mean exactly the same thing. It's mostly a matter of style. The argument in favor of the second is that you typically read types backwards:

int const * * const: const pointer to a pointer to a const int.


As for your arguments relative to the STL: beginners don't need to know what is happening in the background. This is the school of thought that says: learn to program correctly before you learn how it works. [edit] I must add that this only applies to implementation, and not concepts. Beginners should know about the characteristics of a doubly linked list before they go off and use std::list. [/edit] I tend to subscribe to this view, but please do note that it is only my opinion.


jfl.

Share this post


Link to post
Share on other sites
Quote:

As for your arguments relative to the STL: beginners don't need to know what is happening in the background. This is the school of thought that says: learn to program correctly before you learn how it works. I tend to subscribe to this view, but please do note that it is only my opinion.

Wow, I have never heard about that... That sound great and true.

Greeting
Alex

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!