new-operator

Call destructor and then constructor (resetting an object)

拥有回忆 提交于 2019-12-28 05:57:19
问题 I want to reset an object. Can I do it in the following way? anObject->~AnObject(); anObject = new(anObject) AnObject(); // edit: this is not allowed: anObject->AnObject(); This code is obviously a subset of typical life cycle of an object allocated by in placement new: AnObject* anObject = malloc(sizeof(AnObject)); anObject = new (anObject) AnObject(); // My step 2. // ... anObject->~AnObject(); // My step 1. free(anObject) // EDIT: The fact I used malloc instead of new doesn't carry any

What's the best way to use shortcuts and autocomplete to create a new object in Intellij IDEA?

早过忘川 提交于 2019-12-28 05:41:06
问题 Object + shortcut ➜ Object object = new Object(); Whether there is such shortcut? 回答1: new Obj , Tab to complete to new Object() , Ctrl + Alt + V ( Refactor | Introduce Variable ): Object o = new Object(); Change name if needed and press Enter to confirm. For the more convenient solution utilizing the live templates feature please check the answer below from @MarcG. 回答2: As of 2017 , improving on @aleksander's answer, I believe the best way to create a new object as efficiently as possible in

Allocating struct with variable length array member

删除回忆录丶 提交于 2019-12-28 03:06:07
问题 I know I can do new char[n] to create an array of n chars. This works even when n is not a compile time constant. But lets say I wanted a size variable followed by n chars: My first attempt at this is the following: struct Test { std::size_t size; char a[]; }; However it seems new Test[n] doesn't do what I expect, and instead allocates n size s. I've also found that sizeof(std::string) is 4 at ideone, so it seems it can allocate both the size and the char array in one block. Is there a way I

Will new return NULL in any case?

不问归期 提交于 2019-12-27 10:54:20
问题 I know that according to C++ standard in case the new fails to allocate memory it is supposed to throw std::bad_alloc exception. But I have heard that some compilers such as VC6 (or CRT implementation?) do not adhere to it. Is this true ? I am asking this because checking for NULL after each and every new statement makes code look very ugly. 回答1: VC6 was non-compliant by default in this regard. VC6's new returned 0 (or NULL ). Here's Microsoft's KB Article on this issue along with their

What happens when JVM executes new key word to create an object?

穿精又带淫゛_ 提交于 2019-12-25 04:47:22
问题 I know JVM uses stack and heap for allocation of memory for object reference, object value and memory for methods. But I am confused about the terminologies: METHOD AREA, HEAP and JAVA STACK and I have few question's. When we say "ClassName obj = new ClassName()", new creates an object on the HEAP(the instance variables and static variables too) and what is returned to the reference(obj)? Some people use to say it is CLASS TYPE, does it mean the hash code? When new creates the object on the

python 3 print function

给你一囗甜甜゛ 提交于 2019-12-25 03:22:47
问题 So I want to print this: * ** *** **** ***** And my code is: for row in range(1,6): for col in range(row): print('*', end="") print('') My question is about print function, since it includes new line. Knowing some C before, I just can't figure it out what the last print('') does, and why my code doesn't work without it. 回答1: The end='' parameter to the first call to print suppresses printing a newine after the * , and the second call to print prints only a newline. 回答2: The extra print line

awkwardness in creating object

只愿长相守 提交于 2019-12-25 02:56:24
问题 Temp1 t1=new Temp2(); Here Temp1 is superclass of Temp2. The code works perfectly fine and t1 do acts as a reference variable for Temp1 but how can Temp2() works as constructor for Temp1? 回答1: This is the basis for polymorphism: Imagine you have several child classes that inherit from you parent class. You want to use all these child classes through the interface / methods defined on your parent class, without worrying about the implementation details in each child class (each might do

class overloaded new and delete vs placement new with a bespoke memory class

我怕爱的太早我们不能终老 提交于 2019-12-25 00:27:19
问题 I am investigating the pros and cons between using class overloaded news and deletes vs placement news. By this I mean, either declaring every class I may wish to new and delete with their own operator overloads, or by using the memory manager to give me the memory I need via placement new. I have a memory manager that allows me to allocate memory from a number of pools: enum MemPool { kPool1, kPool2, } class MemoryManager { public: template <typename T> void* Allocate(MemPool pool); void

Global overload operator new/delete in MinGW

孤人 提交于 2019-12-24 21:28:57
问题 I want to overload new/delete operators in my application to catch all memory leaks. It works on Linux fine. But I got a problems on Windows. New/delete overloading works only for .exe but not for calls from .dll files. Furthemore if some object is created in my code but is deleting from .dll file it leads to app crash. Cppreference here says Versions (1-8) are replaceable: a user-provided non-member function with the same signature defined anywhere in the program, in any source file,

C++ Pointers and Object Instantiation

杀马特。学长 韩版系。学妹 提交于 2019-12-24 09:55:39
问题 This works: MyObject *o; o = new MyObject(); And this does not: MyObject o = new MyObject(); Why? 回答1: The keyword new returns a pointer. It must be assigned to a pointer of an object. This would also work: MyObject o = MyObject(); EDIT: As Seth commented, the above is equivalent to: MyObject o; The default constructor (i.e. without parameters) is called if no constructor is given. 回答2: Because they're not equivalent. Try: MyObject* o = new MyObject(); 回答3: new MyObject() returns a pointer to