473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Detect if memory is "deletable"

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

Jun 29 '06 #1
7 1269
> 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.
br**********************@hotmail.com
Remove only "_nos_pam"

Jun 29 '06 #2
ma*********@yahoo.com wrote:
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.
Jun 30 '06 #3
>> 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
br**********************@hotmail.com
Remove only "_nos_pam"
Jun 30 '06 #4

"Bronek Kozicki" <br**@rubikon.pl> wrote in message
news:eP*************@TK2MSFTNGP04.phx.gbl...
ma*********@yahoo.com wrote:
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]; i++)
if (x == arrayof_known_strings[i])
return;
delete[] x;
}

Jun 30 '06 #5
Thanks all, gives me a lot of Ideas!
-Maruthi

Code Jockey wrote:
"Bronek Kozicki" <br**@rubikon.pl> wrote in message
news:eP*************@TK2MSFTNGP04.phx.gbl...
ma*********@yahoo.com wrote:
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]; i++)
if (x == arrayof_known_strings[i])
return;
delete[] x;
}


Jul 1 '06 #6

"Bronek Kozicki" <br**@rubikon.plwrote in message
news:eP*************@TK2MSFTNGP04.phx.gbl...
ma*********@yahoo.com wrote:
>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:
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_twsz = 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.

Jul 4 '06 #7
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
br**********************@hotmail.com
Remove only "_nos_pam"
Jul 4 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: teddysnips | last post by:
One of my clients has asked me to make a change to one of their Access applications. The application is a Front End/Back End standard app. I didn't develop it, but looking at it tells me that...
3
by: Rahul Babbar | last post by:
Hi, I had the following doubts about the "For Read Only" clause. 1. How does a "for Read only" clause improve the performance? 2. How does a "for Read only" clause compare with "With UR"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.