473,725 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2609

"Pelle" <pe*******@gmx. de> wrote in message news:2c******** *************** ***@posting.goo gle.com...
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
2593
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 during construction, a form of
1
3846
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 are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
5
5245
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 discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
20
4150
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
912
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 nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
2
2083
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 operator, placement, nothrow, arrays, etc... My books cover the topic, I've found FAQs on the web that cover the topic, and so on, but all the sources I've found are disjointed. There's a bit on this page, a bit on that page, and so on. The...
3
4650
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 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
10
2094
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 sizeof(foo)-sized buffer but does NOT call foo::foo().
6
1391
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 checked). What I find it strange, is that at one point I have to reallocate the data, but if I put this in my operator= overload: if (data != 0) delete data; //mem leak here
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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
9401
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
9176
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
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
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...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
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...
1
3221
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

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.