new-operator

“placement new” advantage scenarios [duplicate]

自闭症网瘾萝莉.ら 提交于 2020-01-04 11:05:58
问题 This question already has answers here : What uses are there for “placement new”? (22 answers) Closed 5 years ago . I have two cases for allocation of memory using new operator. class xx{ public: int x; xx(){} ~xx(){} }; class yy : public xx { public: int y; yy(){} ~yy(){} }; int main(int argc, char *argv[]) { yy *y1 = new yy(); //y1 constructor is called //CASE-1 yy *y2 = y1; //CASE-2 yy *y3 = new (y1) yy(); return 0; } In CASE-1 I am just allocating y1 memory to y2 without destroying y1

“placement new” advantage scenarios [duplicate]

a 夏天 提交于 2020-01-04 11:04:33
问题 This question already has answers here : What uses are there for “placement new”? (22 answers) Closed 5 years ago . I have two cases for allocation of memory using new operator. class xx{ public: int x; xx(){} ~xx(){} }; class yy : public xx { public: int y; yy(){} ~yy(){} }; int main(int argc, char *argv[]) { yy *y1 = new yy(); //y1 constructor is called //CASE-1 yy *y2 = y1; //CASE-2 yy *y3 = new (y1) yy(); return 0; } In CASE-1 I am just allocating y1 memory to y2 without destroying y1

Error operator new[] : function does not take 1 arguments

佐手、 提交于 2020-01-03 20:03:11
问题 I have code that overloads operator new . The code below works fine under Linux (gcc4x) but not Windows (Visual C++ 2008 Express Edition) The code under Visual Studio 2008 Express Edition reports error C2660: operator new[] : function does not take 1 arguments class dummy{}; void* operator new[] (size_t size, dummy gcp) { return ::operator new[](size); //error } int main() { dummy dummyobj; dummy* ptr = new (dummyobj) dummy[5]; return 0; } 回答1: You might need to #include <new> . 来源: https:/

Error operator new[] : function does not take 1 arguments

隐身守侯 提交于 2020-01-03 20:01:18
问题 I have code that overloads operator new . The code below works fine under Linux (gcc4x) but not Windows (Visual C++ 2008 Express Edition) The code under Visual Studio 2008 Express Edition reports error C2660: operator new[] : function does not take 1 arguments class dummy{}; void* operator new[] (size_t size, dummy gcp) { return ::operator new[](size); //error } int main() { dummy dummyobj; dummy* ptr = new (dummyobj) dummy[5]; return 0; } 回答1: You might need to #include <new> . 来源: https:/

Is such assignment a good idea in C++

若如初见. 提交于 2020-01-02 07:14:14
问题 A lot of classes has assignment operator (operator=) the same code as in destructor and than very similar code of copy constructor. So is it good idea to implement the assignment in such way? Point& operator=(const Point& point) { if(&point != this) { //Call the destructor this->~Point(); //Make the placement new //Assignment is made because some compilers optimise such code as just // new Point; Point* p_n = new (this) Point(point); //We where placing in this place so pointers should be

DI container, factory, or new for ephemeral objects?

北慕城南 提交于 2020-01-02 02:00:33
问题 When dealing with objects that require data known only at runtime, such as a username and password, where should object instantiation happen: by using new, in a factory, or in a DI container? For example, I could just new an object once I have the data: UserCredentials creds = new UserCredentials(dialog.getUsername(), dialog.getPassword()); Or, I could use a factory: UserCredentials creds = CredentialsFactory.create(dialog.getUsername(), dialog.getPassword()); Or, I could use a provider

What is this second new?

醉酒当歌 提交于 2020-01-02 00:49:09
问题 What is the second line? (Seen while answering another question.) int * x = new int [1] ; int * y = new (x) int; After the second line x and y have the same value (point to a same place). What's the difference between y = x and the second line? Is it like a constructor or something? 回答1: It's placement new. It constructs a new int in the memory pointed to by x . If you try: int * x = new int [1]; *x = 5; std::cout << *x << std::endl; int * y = new (x) int; *y = 7; std::cout << *x << std::endl

Getting dynamically allocated array size

僤鯓⒐⒋嵵緔 提交于 2020-01-01 16:58:07
问题 In "The C++ Programming Language" book Stroustrup says: "To deallocate space allocated by new , delete and delete[] must be able to determine the size of the object allocated. This implies that an object allocated using the standard implementation of new will occupy slightly more space than a static object. Typically, one word is used to hold the object’s size. That means every object allocated by new has its size located somewhere in the heap. Is the location known and if it is how can I

How does C++ free the memory when a constructor throws an exception and a custom new is used

北城以北 提交于 2020-01-01 08:12:04
问题 I see the following constructs: new X will free the memory if X constructor throws. operator new() can be overloaded. The canonical definition of an operator new overload is void *operator new(size_t c, heap h) and the corresponding operator delete . The most common operator new overload is placement new, which is void *operator new(void *p) { return p; } You almost always cannot call delete on the pointer given to placement new . This leads to a single question: How is memory cleaned up when

How does C++ free the memory when a constructor throws an exception and a custom new is used

雨燕双飞 提交于 2020-01-01 08:11:48
问题 I see the following constructs: new X will free the memory if X constructor throws. operator new() can be overloaded. The canonical definition of an operator new overload is void *operator new(size_t c, heap h) and the corresponding operator delete . The most common operator new overload is placement new, which is void *operator new(void *p) { return p; } You almost always cannot call delete on the pointer given to placement new . This leads to a single question: How is memory cleaned up when