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

why construction called when i overload the operator new in c++ by calling malloc

1
hi:
i overload the operator new, codes shown as below:

void * __cdecl operator new(unsigned int size, const char *file, int line)
{
void *ptr = (void *)malloc(size);

return ptr;
}

and when i want to create a class object

CTest::CTest()
{
int a = 0;
}
CTest::~CTest()
{
int b = 0;
}

int main()
{
CTest* test = new CTest;
delete test;
return 0;
}

it is very obvious that i overload the operator new by malloc, so when i use the overloaded new operator to create a class object, the CTest's construction should not be called, while the result is complete opposite. And
my question: why the class's construction function can be called when i create it by malloc but not new?
Mar 3 '12 #1
1 1514
weaknessforcats
9,208 Expert Mod 8TB
When you create an object either using or not using the new operator, the compiler allocates the memory for your onject and then calls the class constructor.

When you overload operator new, all you are doing is affecting wthe compiler allocates memory for your object. Once that's done, the class constructor is called.

BTW: Overriding operator new so you can use malloc() is pointless since the default implementation of operator new is to call malloc().
Mar 3 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Matthew Monopole | last post by:
Since in integer we can use either a = -b and a = b - c shouldn't it when we overload operator there'd be two way to overload the minus operator? if so, what's the prototypes? class...
10
by: Ryan McGeary | last post by:
In a <select> drop-down, the onchange event isn't called when scrolling through the dropdown using the mouse-wheel and when crossing over a new <optgroup>. Using the example below, notice how...
5
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
5
by: Paul Wilkinson | last post by:
How can I write a method that is called when an object goes out of scope? I've looked all over, so far I've only found discussions of how to write methods that don't = )
2
by: vcmkrishnan.techie | last post by:
Hi all, In managed C++, can I overload the operator to access data members in the following way Create object objTime; seconds = obj; or seconds = obj;
6
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
2
by: Mahain | last post by:
How we overload '=' operator using friend function in c++. if not why ????????????????????
0
by: Nathan Sokalski | last post by:
I have a page with an UpdatePanel that contains several TextBoxes and Buttons. There is also a ListBox outside of the UpdatePanel that is used as a trigger to populate these TextBoxes. The Buttons...
1
by: Maximus | last post by:
I am trying to find a way to overload operator new of a class to use a specialzied memory manager. In my design multiple memory managers exist. I need to pass the instance of a memory manager into...
2
by: Markjan | last post by:
I have a problem with classes and structures in classes (C++) I have to overload operator . class Data { public: class Proxy { //for overload operator Data& _a; int...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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)...
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.