Detect if memory is "deletable"

  • Thread starter Thread starter maruthir123
  • Start date Start date
M

maruthir123

Hi, I have a wchar_t pointer. Based on some conditions I assign string
literals to it and on some other conditions, I allocate memory and
assign it to this. Is there a way to find out while deleting if the
memory is deletable. I get an error if I try to delete this pointer
when Literals are assigned.

Thanks,
Maruthi
 
Hi, I have a wchar_t pointer. Based on some conditions I assign string
literals to it and on some other conditions, I allocate memory and
assign it to this. Is there a way to find out while deleting if the
memory is deletable. I get an error if I try to delete this pointer
when Literals are assigned.

you could use _CrtIsValidHeapPointer to determine if a pointer is pointing
to heap memory or not.
string literals will not be in heap memory, so you should be able to make a
distinction.
sadly, this function is only available in debug builds.

_msize is another option. but it also has its share of issues.

I do not know your design constraints, but an alternative approach would be
to always allocate the memory and then copy the literal into that memory
block.
that way you always know for sure.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Hi, I have a wchar_t pointer. Based on some conditions I assign string
literals to it and on some other conditions, I allocate memory and
assign it to this. Is there a way to find out while deleting if the
memory is deletable. I get an error if I try to delete this pointer
when Literals are assigned.

this is poor design. Try something more along the lines:

const char* a = NULL;
const char* const b = "abc";

if (blablabla)
a = new char[1200];
else
a = b;

// do the job

if (a != b)
delete[] a;


B.
 
Hi, I have a wchar_t pointer. Based on some conditions I assign string
literals to it and on some other conditions, I allocate memory and
assign it to this. Is there a way to find out while deleting if the
memory is deletable. I get an error if I try to delete this pointer
when Literals are assigned.

this is poor design. Try something more along the lines:

const char* a = NULL;
const char* const b = "abc";

if (blablabla)
a = new char[1200];
else
a = b;

// do the job

if (a != b)
delete[] a;

This is just as bad, because if you use different string literals, you'd
have to check against each and every one of them.
Another issue is that if you add new string literals to your application,
you'd have to go through your program to update the places where you check
if your pointer points to a string literal or not.

the best solution IMO is still to always allocate a buffer, and simply make
a copy of the string literal if the string is constant.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Bronek Kozicki said:
Hi, I have a wchar_t pointer. Based on some conditions I assign string
literals to it and on some other conditions, I allocate memory and
assign it to this. Is there a way to find out while deleting if the
memory is deletable. I get an error if I try to delete this pointer
when Literals are assigned.

this is poor design. Try something more along the lines:

const char* a = NULL;
const char* const b = "abc";

if (blablabla)
a = new char[1200];
else
a = b;

// do the job

if (a != b)
delete[] a;


B.

Along the lines suggested above, put all known static string literals in an
array: Replace "delete[] x" to call a function to check if the string is a
literal:

char** arrayof_known_strings[]={"Unknown", "Error", "Warning", 0};
void delete_if_not_a_known_static_string(char* x)
{
for (int i= 0; arrayof_known_strings; i++)
if (x == arrayof_known_strings)
return;
delete[] x;
}
 
Thanks all, gives me a lot of Ideas!
-Maruthi

Code said:
Bronek Kozicki said:
Hi, I have a wchar_t pointer. Based on some conditions I assign string
literals to it and on some other conditions, I allocate memory and
assign it to this. Is there a way to find out while deleting if the
memory is deletable. I get an error if I try to delete this pointer
when Literals are assigned.

this is poor design. Try something more along the lines:

const char* a = NULL;
const char* const b = "abc";

if (blablabla)
a = new char[1200];
else
a = b;

// do the job

if (a != b)
delete[] a;


B.

Along the lines suggested above, put all known static string literals in an
array: Replace "delete[] x" to call a function to check if the string is a
literal:

char** arrayof_known_strings[]={"Unknown", "Error", "Warning", 0};
void delete_if_not_a_known_static_string(char* x)
{
for (int i= 0; arrayof_known_strings; i++)
if (x == arrayof_known_strings)
return;
delete[] x;
}
 
Bronek Kozicki said:
this is poor design. Try something more along the lines:

No kidding. Yet I haven't seen a decent answer here yet. If you didn't
allocate the string, you have no business deleting it. Period. Establish
memory ownership rules and respect them. Simply comparing to known literal
strings buys you nothing. There are at least a dozen different allocators
out there (alloca, malloc, new, new [], CoTaskAlloc, LocalAlloc, HeapAlloc,
GlobalAlloc, SysAllocString, SafeArrayCreate, ...) and you have to use the
correct matching deallocator. The first four are CRT-specific, so multiple
by about 10 different versions of MSVC, Borland, etc.

Since we're in the C++/CLI newsgroup anyway, let me ask this question. What
happens when your string is part of a System::String? Like
System::String^ str = ....;
pin_ptr<wchar_t> wsz = PtrToStringChars(str);
your_function(wsz);


Do you really want to call delete [] on that string now?

There are two standard memory ownership schemes. Win32 core API uses
caller-managed buffers. The only core Win32 functions that allocate and
free memory are the ones designed for that purpose.

The other scheme is COM-style. If a library wants to accept ownership of
parameters (necessary to avoid marshalling the data back during RPC), then
it specifies that the argument must have been allocated using the functions
it provides (think of BSTR). Any out parameter allocated within the
library, the caller must send back to the library for deallocation. This
also plays well with reference counting.

Of course, GC changes the whole picture.
const char* a = NULL;
const char* const b = "abc";

if (blablabla)
a = new char[1200];
else
a = b;

// do the job

if (a != b)
delete[] a;


B.
 
No kidding. Yet I haven't seen a decent answer here yet. If you didn't
allocate the string, you have no business deleting it.

If the OP has a design that calls for the deallocation of string memory
where he can't know how it was allocated, my original solution is simple and
un-ambiguous.

allocate a new buffer and copy the string data in it.
if you allocate it with new[], you can deallocate it with delete[], no
matter where the original string came from.

whether the software design of the OP is good is a point that can be argued,
but my solution is correct and simple.


--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top