473,387 Members | 1,942 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,387 software developers and data experts.

Weird thinking and delete operator

Hello all,

I have to admit, that the idea that occurred to me recently is weird.
It was somehow inspired by the huge response on the "delete operator"
thread, but goes into a somewhat different direction.

From my understanding there are 2 "ways" of handling memory:
-- one (from C) is the malloc/realloc/free set of API functions
-- the other one (from C++) is the new/new[]/delete/delete[] set of
API functions.

However, since C++ is somewhat based on C (though not exactly C with
object orientation), you can use both in a C++ program. Right?

Now, since the above mentioned thread circled very much on the
question whether I can delete an array with or without brackets (usind
delete or delete[]) it came to my mind, that one might accidentally or
intentedly mix up new and free. Since free always tries to free the
entire memory block pointed to by it's parameter, it might actually do
what was intended with delete[], though probably missing out on
calling the appropriate destructors, which delete[] would have done.
(thus highly inappropriate when pointing to complex classes,
containing types that require destructors to be deallocated
appropriately)

My point is, that I might at some point end up somewhere, where I do
not know wheter my pointer is pointing to an array or a simple type
(e.g. if a function accepts void pointers to different types). Well,
if I use free to free up the used memory, it doesn't matter. Right?

In my concrete situation, I have a void pointer pointing to either a
long, a double or a TCHAR (other datatypes might be added later).
Unfortunately, this pointer is allocated down in a library of mine and
thus I need to provide a library-function to free this
pointer.(Haven't yet figured out how to have memory allocated in a DLL
free-able in the application using the DLL - maybe someone can answer
this question on the side.). In my first attempt, I've started
allocating memory with "pvValue = new long" (for example) and used
"delete pvValue;" in my other library-function (for deallocating the
memory). I think that this way I might fail to properly handle the
memory in case I am pointing to a TCHAR!
Thus I'm currently thinking about falling back to malloc/free set of
functions and along that path came to the thought of mixing up new and
free.

Does anybody know what would happen?

Alas, I'm afraid this topic got me a bit confused. However any input
of yours is highly appreciated!

Bye,
regards,
Carsten.
Jul 19 '05 #1
1 2585

"Pelle" <pe*******@gmx.de> wrote in message news:2c**************************@posting.google.c om...
Thus I'm currently thinking about falling back to malloc/free set of
functions and along that path came to the thought of mixing up new and
free.

Does anybody know what would happen?
Freeing stuff not Malloc'd and deleting stuff not new'd is undefined behavior.
Don't do that.

malloc has a number of problems for C++ (which is why new exists). It
doesn't properly construct the object. It's not type safe.
In my concrete situation, I have a void pointer pointing to either a
long, a double or a TCHAR (other datatypes might be added later).


This sounds like a rather bogus C++ design. You sould always simulate
malloc with:
void* FakeMalloc(size_t n) { return new char[n]; }
void FakeFree(void* p) { delete (char*) p; }

As for DLL's, that's really machine specific. But the answer to that issue
is probably the same to the real answer to your problem. Provide a function
in your library that's complementary to what allocates the poihnter to delete
it. Nice and symetrical.
Jul 19 '05 #2

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

Similar topics

2
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
2
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
6
by: Lucas Kanebley Tavares | last post by:
Hello all, I have a templatized class which has an attribute as: "T *data", all constructors initialize it to zero, and then allocate memory for the array (and that IS done correctly, I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.