473,796 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1285
> 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 _CrtIsValidHeap Pointer 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************* *********@hotma il.com
Remove only "_nos_pam"

Jun 29 '06 #2
ma*********@yah oo.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************* *********@hotma il.com
Remove only "_nos_pam"
Jun 30 '06 #4

"Bronek Kozicki" <br**@rubikon.p l> wrote in message
news:eP******** *****@TK2MSFTNG P04.phx.gbl...
ma*********@yah oo.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_s trings[]={"Unknown", "Error", "Warning", 0};
void delete_if_not_a _known_static_s tring(char* x)
{
for (int i= 0; arrayof_known_s trings[i]; i++)
if (x == arrayof_known_s trings[i])
return;
delete[] x;
}

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

Code Jockey wrote:
"Bronek Kozicki" <br**@rubikon.p l> wrote in message
news:eP******** *****@TK2MSFTNG P04.phx.gbl...
ma*********@yah oo.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_s trings[]={"Unknown", "Error", "Warning", 0};
void delete_if_not_a _known_static_s tring(char* x)
{
for (int i= 0; arrayof_known_s trings[i]; i++)
if (x == arrayof_known_s trings[i])
return;
delete[] x;
}


Jul 1 '06 #6

"Bronek Kozicki" <br**@rubikon.p lwrote in message
news:eP******** *****@TK2MSFTNG P04.phx.gbl...
ma*********@yah oo.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_t wsz = PtrToStringChar s(str);
your_function(w sz);
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************* *********@hotma il.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
3924
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 it was done entirely using the Wizards. There is no log-in procedure. They want to amend it so that only one person can be logged in at any one time. So, if Joe Bloggs tries to open the application, and Fred Jones has already connected, Joe...
3
13258
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" clause in performance? Which is faster? Can someone clarify on that?
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.