new-operator

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

纵然是瞬间 提交于 2020-01-01 08:11:09
问题 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 do “Object()” and “new Object()” differ in JavaScript?

独自空忆成欢 提交于 2019-12-31 11:49:26
问题 In JavaScript, what's the difference between var x = Object(); and var x = new Object(); ? 回答1: This is pulled directly from the ECMAScript specification: 15.2.1 The Object Constructor Called as a Function When Object is called as a function rather than as a constructor, it performs a type conversion. 15.2.1.1 Object ( [ value ] ) When the Object function is called with no arguments or with one argument value, the following steps are taken: If value is null, undefined or not supplied, create

How do “Object()” and “new Object()” differ in JavaScript?

荒凉一梦 提交于 2019-12-31 11:48:19
问题 In JavaScript, what's the difference between var x = Object(); and var x = new Object(); ? 回答1: This is pulled directly from the ECMAScript specification: 15.2.1 The Object Constructor Called as a Function When Object is called as a function rather than as a constructor, it performs a type conversion. 15.2.1.1 Object ( [ value ] ) When the Object function is called with no arguments or with one argument value, the following steps are taken: If value is null, undefined or not supplied, create

new on stack instead of heap (like alloca vs malloc)

僤鯓⒐⒋嵵緔 提交于 2019-12-31 09:44:07
问题 Is there a way to use the new keyword to allocate on the stack (ala alloca ) instead of heap ( malloc ) ? I know I could hack up my own but I'd rather not. 回答1: To allocate on the stack, either declare your object as a local variable by value , or you can actually use alloca to obtain a pointer and then use the in-place new operator: void *p = alloca(sizeof(Whatever)); new (p) Whatever(constructorArguments); However, while using alloca and in-place new ensures that the memory is freed on

new on stack instead of heap (like alloca vs malloc)

。_饼干妹妹 提交于 2019-12-31 09:44:00
问题 Is there a way to use the new keyword to allocate on the stack (ala alloca ) instead of heap ( malloc ) ? I know I could hack up my own but I'd rather not. 回答1: To allocate on the stack, either declare your object as a local variable by value , or you can actually use alloca to obtain a pointer and then use the in-place new operator: void *p = alloca(sizeof(Whatever)); new (p) Whatever(constructorArguments); However, while using alloca and in-place new ensures that the memory is freed on

C++ STL allocator vs operator new

心不动则不痛 提交于 2019-12-31 08:08:17
问题 According to C++ Primer 4th edition, page 755, there is a note saying: Modern C++ programs ordinarily ought to use the allocator class to allocate memory. It is safer and more flexible. I don't quite understand this statement. So far all the materials I read teach using new to allocate memory in C++. An example of how vector class utilize allocator is shown in the book. However, I cannot think of other scenarios. Can anyone help to clarify this statement? and give me more examples? When should

when should I use the new operator in C++

雨燕双飞 提交于 2019-12-29 09:14:29
问题 Say I have a class called Money which has parameters Dollars and Cents I could initialize it in the followings 2 ways: Money a(3,15); Money *b=new Money(3,15); My question is when should I use (1) and when should I use (2) 回答1: The first one creates a Money object on the stack, its lifespan is within the scope of when it was created. Meaning when you hit a } it goes out of scope and the memory is returned. Use this when you want to create an object within one function. The second one creates

Hibernate new keyword with distinct

浪尽此生 提交于 2019-12-29 06:43:34
问题 I need to take hql that is currently : select distinct a from Asset as a where ... and change it to select new com.org.AssetDTO(a.id, a.address, a.status) from Asset as a where ... My problem is with the distinct keyword. Where does it belong in an hql query where you're using the new Object query type. One thought was to use a sub-select and have my distinct there. I've tried adding distinct a.id but that doesn't work. 回答1: Ok for anyone interested the proper syntax is select distinct new

Hibernate new keyword with distinct

可紊 提交于 2019-12-29 06:43:28
问题 I need to take hql that is currently : select distinct a from Asset as a where ... and change it to select new com.org.AssetDTO(a.id, a.address, a.status) from Asset as a where ... My problem is with the distinct keyword. Where does it belong in an hql query where you're using the new Object query type. One thought was to use a sub-select and have my distinct there. I've tried adding distinct a.id but that doesn't work. 回答1: Ok for anyone interested the proper syntax is select distinct new

Using the 'new' modifier in C#

只谈情不闲聊 提交于 2019-12-28 20:35:23
问题 I read that the new modifer hides the base class method. using System; class A { public void Y() { Console.WriteLine("A.Y"); } } class B : A { public new void Y() { // This method HIDES A.Y. // It is only called through the B type reference. Console.WriteLine("B.Y"); } } class Program { static void Main() { A ref1 = new A(); // Different new A ref2 = new B(); // Polymorpishm B ref3 = new B(); ref1.Y(); ref2.Y(); //Produces A.Y line #xx ref3.Y(); } } Why does ref2.Y(); produce A.Y as output?