Hi,
I am maintaining a C++ project which is a server which continuously
receives requeste from clients.
I have noticed that we overload the new operator and in it then call
malloc to allocate memory.
Specifically the code is something like this.
template <class T>
class CSimpleList
{
public:
CSimpleList()
{
// some code
}
inline void* operator new(size_t tSize){ return malloc(tSize); }
inline void operator delete( void* p ){ free(p); }
};
Is it all right to use it like this. Because i was under the impression
that even after you overload the new operator, it does memory
allocation implicitly(i.e even if you dont do a malloc or some other
kind of memory allocation).
So in the above case, wont memory be allocated twice.
Thanks
Vinu 6 2440 Is it all right to use it like this. Because i was under the impression that even after you overload the new operator, it does memory allocation implicitly(i.e even if you dont do a malloc or some other kind of memory allocation). So in the above case, wont memory be allocated twice.
No, it won't. Operator new does allocation by calling function operator
new(), nothing more, end of story. In fact, in many systems the default
operator new() function does nothing more than just a malloc call.
ben
Hi,
Will the constructor be called if I use a malloc() inside the
overloaded new ?
> Will the constructor be called if I use a malloc() inside the overloaded new ?
Yes.
T* i = new T;
is equivalent to:
T* i = (T*) malloc(sizeof(T ));
i->T::T(); // pseudo C++ code
ben
benben wrote: Will the constructor be called if I use a malloc() inside the overloaded new ? Yes.
T* i = new T;
is equivalent to:
T* i = (T*) malloc(sizeof(T )); i->T::T(); // pseudo C++ code
No, it is equivalent to
T* i = (T*)operator new(sizeof(T));
i->T::T(); // pseudo C++ code
To the OP: What you overload is the operator new call (forwards to
malloc() most of the times, but may do anything you want, including
printing "hello world" and returning 123), nothing else. The 'new
operator' (as opposed to the operator new) cannot be overloaded.
Jonathan
So Whats the difference between "operator new" and "new operator".
what i understand is "operator new" is just like "operator ==" or any
of the other operators.
So then whats "new operator"
Thanks
Vinu
Jonathan Mcdougall wrote: benben wrote: Will the constructor be called if I use a malloc() inside the overloaded new ?
Yes.
T* i = new T;
is equivalent to:
T* i = (T*) malloc(sizeof(T )); i->T::T(); // pseudo C++ code
No, it is equivalent to
T* i = (T*)operator new(sizeof(T)); i->T::T(); // pseudo C++ code
To the OP: What you overload is the operator new call (forwards to malloc() most of the times, but may do anything you want, including printing "hello world" and returning 123), nothing else. The 'new operator' (as opposed to the operator new) cannot be overloaded.
Jonathan
Don't top-post. Rearragned. To the OP: What you overload is the operator new call (forwards to malloc() most of the times, but may do anything you want, including printing "hello world" and returning 123), nothing else. The 'new operator' (as opposed to the operator new) cannot be overloaded.
So Whats the difference between "operator new" and "new operator".
what i understand is "operator new" is just like "operator ==" or any of the other operators.
So then whats "new operator"
See my post in http://tinyurl.com/by9l2.
Jonathan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Nitin Bhardwaj |
last post by:
Thanx in advance for the response...
I wanna enquire ( as it is asked many a times in Interviews that i
face as an Engg PostGraduate ) about the overloading capability of the
C++ Language.
Why can't the = (assignment) operator be overloaded as a friend
function ?
I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it
said the following :
|
by: Andy Jarrell |
last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain
overloaded operators don't seem to come with the inheritance. For example:
// TestA.h ---------------------------------------
#include <iostream>
enum Aval
{
FIRST_VALUE,
|
by: rokia |
last post by:
in a project, I use many,many stl such as stack,list,vctor etc.
somewhere the vector's size is more than 2K. is this a efficient way?
|
by: BekTek |
last post by:
Hi..
I'm so sorry about that I've postes so many questions recently.. :)
I'm still confused about the differences between malloc and operator new..
I know that when we work with class object and use operator new/delete, the
ctor and dtor
get called as expected, but malloc and free do not..
But Herbal Sutter mentioned in his greate book 'exceptional c++' about free
storage
|
by: masood.iqbal |
last post by:
I have a few questions regarding overloaded typecast
operators and copy constructors that I would like an
answer for. Thanks in advance.
Masood
(1) In some examples that I have seen pertaining to
casting class A to class B, the implementation of the
| |
by: Barry |
last post by:
Recently, I have to communicate C++ technical problems in English, I
found that `malloc', `strcpy', `operator()' and something like that hard
to express,
malloc = or `memory allocate'
strcpy =`S T R C P Y' or `string copy'
Is there any recommendation on the pronunciation for them?
|
by: subramanian100in |
last post by:
overloaded operator=()
--------------------------------
overloaded assignment operator should be a non-static MEMBER function
of a class. This ensures that the first operand is an lvalue.
If the overloaded assignment operator function is allowed to be a non-
member function then we may be able to write the following:
Suppose we have a class Test for which operator+() is defined,
suppose we have
|
by: itdevries |
last post by:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.
I've got my own custom vector class, as a part of that I've overloaded
the + operator by means of a friend function. Everything worked fine
until I decided to use a variable array size (by using new/delete),
now I get an error when a temporary object is deleted (e.g. after
addition), the error occurs at the delete...
|
by: mojumbo |
last post by:
Problem:
I have a structure which needs to store its data in contiguous memory
by there is a dynamic element which can't be defined at compile time.
It needs to be aligned along a 4 byte boundary. This is what my
structure looks like:
START
struct tsBob
|
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...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |