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

operator new, operator delete; inline and const specifier

12
int *p=new int(7)
delete p;

could be written as

void* buf=operator new(sizeof(int));
int* p=new (buf) int(7);
operator delete(p);

My question is how to use operator new, placement new to allocate and initialize the storage and use operator delete to deallocate the storage?

BTW, why the inline specifier for a member function can appear in the declaration or the definition or both;whereas the const specifier fo a member function must appear in both of them?
Apr 29 '07 #1
5 2640
AdrianH
1,251 Expert 1GB
int *p=new int(7)
delete p;

could be written as

void* buf=operator new(sizeof(int));
int* p=new (buf) int(7);
operator delete(p);

My question is how to use operator new, placement new to allocate and initialize the storage and use operator delete to deallocate the storage?

BTW, why the inline specifier for a member function can appear in the declaration or the definition or both;whereas the const specifier fo a member function must appear in both of them?
It could be written that way, but don't. ;) Your use of operator new is not necessarily bad, but the use of operator delete is real bad (though not in this case).

The reason it is bad is because when you call operator delete, the destructor is not called. For a type like int that has no destructor, that is ok, but it will cause a resource leak if you try it that does have a destructor to cleanup the resources.

As for how to use the new and delete operators to do in place initialisation and destruction, well... it depends as to why I guess.

Tell me why you want to do this, and I would be able to give you an explanation.

As to the inline question, that is probably due to the fact that it is not part of the function signature, where as const is. You omit the const keyword in one of the places and it will think it is for a completely different function. Inline is merely a hint to the compiler to inline the function.


Adrian
Apr 29 '07 #2
huan
12
It could be written that way, but don't. ;) Your use of operator new is not necessarily bad, but the use of operator delete is real bad (though not in this case).

The reason it is bad is because when you call operator delete, the destructor is not called. For a type like int that has no destructor, that is ok, but it will cause a resource leak if you try it that does have a destructor to cleanup the resources.

As for how to use the new and delete operators to do in place initialisation and destruction, well... it depends as to why I guess.

Tell me why you want to do this, and I would be able to give you an explanation.

As to the inline question, that is probably due to the fact that it is not part of the function signature, where as const is. You omit the const keyword in one of the places and it will think it is for a completely different function. Inline is merely a hint to the compiler to inline the function.


Adrian
I'm very appreciative for your patient reply.
Actually, I've lost one paragragh, which is the most important part.....
I know that this is not necessary, but I'd like to know how to use operater new
,placement new and operator delete when

int**p=new int*(new(int(7))) ;
Apr 30 '07 #3
AdrianH
1,251 Expert 1GB
I'm very appreciative for your patient reply.
Actually, I've lost one paragragh, which is the most important part.....
I know that this is not necessary, but I'd like to know how to use operater new
,placement new and operator delete when

int**p=new int*(new(int(7))) ;
But placement new is not necessary, you just wrote the valid code yourself right there.

Placement new is when you have memory already allocated or allocated in a special way and you wish to use it. But it takes some care to do it properly, esp if you have a destructor.

A simple example.
Expand|Select|Wrap|Line Numbers
  1. char allocatedMem[sizeof(int)];
  2. int * intValue = new(allocatedMem) int(7);
  3. ...
  4. intValue->~int();  // Not specificly needed in this case, but illistrates the point.
  5.  
Do you understand?


Adrian
Apr 30 '07 #4
huan
12
But placement new is not necessary, you just wrote the valid code yourself right there.

Placement new is when you have memory already allocated or allocated in a special way and you wish to use it. But it takes some care to do it properly, esp if you have a destructor.

A simple example.
Expand|Select|Wrap|Line Numbers
  1. char allocatedMem[sizeof(int)];
  2. int * intValue = new(allocatedMem) int(7);
  3. ...
  4. intValue->~int();  // Not specificly needed in this case, but illistrates the point.
  5.  
Do you understand?


Adrian
OK~!Got it~!
Thanks~!!:-)
Apr 30 '07 #5
AdrianH
1,251 Expert 1GB
OK~!Got it~!
Thanks~!!:-)
No prob. Glad to help.


Adrian
Apr 30 '07 #6

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

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
0
by: Tim Milstead | last post by:
(CODE AT END OF POST) I have got some code for finding memory leaks that works well with: new() and needs to be extended to work with new
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
0
by: ma740988 | last post by:
Consider #include <iostream> #include <string> #include <map> using namespace std; struct dstream // data_stream class {
9
by: Matthias Pospiech | last post by:
I have a Class for complex operations: class CComplex { public: inline CComplex() { re=im=0.0; } inline CComplex operator*(CComplex c) { return CComplex(re*c.re-im*c.im,re*c.im+im*c.re);}...
0
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in...
0
by: Nico | last post by:
I want to create a const_iterator that gives me access to dynamically generated objects. For example see the class Polygon that contains a list of points stored as following doubles in a vector...
9
by: Nico | last post by:
I want to create a const_iterator that gives me access to dynamically generated objects. For example see the class Polygon that contains a list of points stored as following doubles in a vector...
30
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
0
jinu1996
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...
0
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...

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.