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?
5 2640
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
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))) ;
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. - char allocatedMem[sizeof(int)];
-
int * intValue = new(allocatedMem) int(7);
-
...
-
intValue->~int(); // Not specificly needed in this case, but illistrates the point.
-
Do you understand?
Adrian
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. - char allocatedMem[sizeof(int)];
-
int * intValue = new(allocatedMem) int(7);
-
...
-
intValue->~int(); // Not specificly needed in this case, but illistrates the point.
-
Do you understand?
Adrian
OK~!Got it~!
Thanks~!!:-)
OK~!Got it~!
Thanks~!!:-)
No prob. Glad to help.
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
by: ma740988 |
last post by:
Consider
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct dstream // data_stream class
{
|
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);}...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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...
|
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: 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...
|
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...
| | |