deleting strings
-
We delete an array of char this way:
delete[] str; str=NULL
But, if the string is created likestr=_T("test");
or#define str _T("test")
, trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I shouldstrcpy
those strings before use, but lazy me...
[VISUAL STUDIO 6.0] [MFC] [WIN98/2]
-
We delete an array of char this way:
delete[] str; str=NULL
But, if the string is created likestr=_T("test");
or#define str _T("test")
, trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I shouldstrcpy
those strings before use, but lazy me...
[VISUAL STUDIO 6.0] [MFC] [WIN98/2]
You can't. Well, not without keeping some extra information anywhere. Really this is a design problem, not an implementation problem - it's good practice to allocate and free memory at the same level of a program, so that level already knows how it allocated the memory. For example char* str = new char[100]; somefunction(str); delete[] str; char* str = "Hello world"; somefunction(str); // No need to delete anything The point is that "somefunction" didn't allocate the memory so it can't know how to free it and shouldn't try. Basically whenever you allocate some memory you need to think about how it's going to be freed, and ensure that it's done at the same level as it was allocated, which is probably the only place that knows how to do it.
-
We delete an array of char this way:
delete[] str; str=NULL
But, if the string is created likestr=_T("test");
or#define str _T("test")
, trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I shouldstrcpy
those strings before use, but lazy me...
[VISUAL STUDIO 6.0] [MFC] [WIN98/2]
There's no portable way of determining if object is on the heap or not. Switch to std::string or CString and forget about it. Tomasz Sowinski -- http://www.shooltz.com
*** Vodka. Connecting people. ***
-
We delete an array of char this way:
delete[] str; str=NULL
But, if the string is created likestr=_T("test");
or#define str _T("test")
, trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I shouldstrcpy
those strings before use, but lazy me...
[VISUAL STUDIO 6.0] [MFC] [WIN98/2]
You may want to try with
IsBadWritePtr
. Do some experimentations with it to see how it handles strings (literal and dynamically-allocated strings). Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
- TreeBeard -
We delete an array of char this way:
delete[] str; str=NULL
But, if the string is created likestr=_T("test");
or#define str _T("test")
, trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I shouldstrcpy
those strings before use, but lazy me...
[VISUAL STUDIO 6.0] [MFC] [WIN98/2]
-
We delete an array of char this way:
delete[] str; str=NULL
But, if the string is created likestr=_T("test");
or#define str _T("test")
, trying to delete it results in an exception. Now, my question is this: how can I figure out if the string is deletable or not? (must be a const prob) I know I shouldstrcpy
those strings before use, but lazy me...
[VISUAL STUDIO 6.0] [MFC] [WIN98/2]
Try
const LPCSTR str = _T("test");
and when the compiler complains about const do as it says. Don't cast the const away :) Todd Smith
-
I used to code java like that! Does it work for c++ too?
[VISUAL STUDIO 6.0] [MFC] [WIN98/2]