new-operator

new[] / delete[] and throwing constructors / destructors in C++

眉间皱痕 提交于 2019-12-24 08:01:50
问题 What happens, in the following code, if construction / destruction of some array element throws? X* x = new X[10]; // (1) delete[] x; // (2) I know that memory leaks are prevented, but additionally: Ad (1), are the previously constructed elements destructed? If yes, what happens if destructor throws in such a case? Ad (2), are the not-yet-destructed elements destructed? If yes, what happens if destructor throws again? 回答1: Yes, if the constructor of x[5] throws, then the five array elements x

JS new.target vs. instanceof

两盒软妹~` 提交于 2019-12-24 05:08:29
问题 So I have done some reading on the new.target boolean added in Node 6.x. Here is a simple example of new.target provided on MDN function Foo() { if (!new.target) throw "Foo() must be called with new"; console.log("Foo instantiated with new"); } Foo(); // throws "Foo() must be called with new" new Foo(); // logs "Foo instantiated with new" But this reads a lot like what I am presently using the below code for var Foo = function (options) { if (!(this instanceof Foo)) { return new Foo(options);

Does Qt allready have its own new and delete operators?

自古美人都是妖i 提交于 2019-12-24 04:40:12
问题 I'm using a QGraphicsScene widget and showing upon it some points as QGraphicsRectItem . This means calling lots of new + addItem() when showing up, and removeItem() + delete to get rid of unused points. Of course, for performance issues, I've implemented my own new and delete operators, which basically recycle pre-allocated memory chunks. Those operators works very well with anything, except for Qt classes (I mean QObject derived classes). The error raised is different every time but always

Memory Allocation Crash

久未见 提交于 2019-12-24 01:15:49
问题 I have stumbled accross a strange problem in which I cannot understand. I am not an expert at C/C++ so bear with me. I have an NPC class, which derives from a Player class, which derives from a Sprite class. The sprite class contains a setupAnimation function, which allocates an array of floats containing coordinates on the texture, each element of the array refers to a frame of animation. This is all good and works fine. However, a problem occurs when I add an array of pointers to the NPC

Memory Allocation Crash

老子叫甜甜 提交于 2019-12-24 01:14:52
问题 I have stumbled accross a strange problem in which I cannot understand. I am not an expert at C/C++ so bear with me. I have an NPC class, which derives from a Player class, which derives from a Sprite class. The sprite class contains a setupAnimation function, which allocates an array of floats containing coordinates on the texture, each element of the array refers to a frame of animation. This is all good and works fine. However, a problem occurs when I add an array of pointers to the NPC

Does placement new zero out the memory?

拥有回忆 提交于 2019-12-23 22:15:11
问题 I have the following code : struct foo {}; void bar(foo *d) { new(d) foo(*d); } Does the expression new(d) foo(*d) leave the object pointed to by d unchanged? More specifically, is the above true if the class foo and all the objects contained recursively within it only have trivial copy constructors, then does new(d) foo(*d) leave *d unchanged? A situation in which that is not true could be, new first zeroes out the memory before calling the copy constructor. Are there such clauses in the C++

Is “new” in Erlang part of the official standard and should we use it?

£可爱£侵袭症+ 提交于 2019-12-23 20:28:57
问题 I ask this question as I have noticed that alot of OpenSource Erlang projects use "new" to pass parameters to Erlang modules, yet I hear at the same time that "new" is not part of the official language and may not be supported if it contains bugs. Before I use it in my own project I would like to clarify this issue. Update: I have since asked on the official Erlang mailing list for an answer: http://www.erlang.org/cgi-bin/ezmlm-cgi?4:mss:49535:201002:aicfhmngkhodmclhlnak 回答1: There is no

Javascript `new` operator & prototype

ⅰ亾dé卋堺 提交于 2019-12-23 20:24:59
问题 Say we create a Function named 'Shape', and add a property 'name' & method 'toString' on it's prototype: var Shape = function () {}; Shape.prototype.name = 'Shape'; Shape.prototype.toString = function () { return this.name; } console.log(Shape.name); // '' console.log(Shape.toString()); // function () {} var Triangle = new Shape(); console.log(Triangle.name); // 'Shape' console.log(Triangle.toString()); // 'Shape' 'NEW' operator did two things: 1. It points this to 'Triangle' 2. It points

Why the overrided operator new isn't call?

独自空忆成欢 提交于 2019-12-23 20:22:42
问题 I run the following code on VS2005: #include <iostream> #include <string> #include <new> #include <stdlib.h> int flag = 0; void* my_alloc(std::size_t size) { flag = 1; return malloc(size); } void* operator new(std::size_t size) { return my_alloc(size); } void operator delete(void* ptr) { free(ptr); } void* operator new[](std::size_t size) { return my_alloc(size); } void operator delete[](void* ptr) { free(ptr); } int main() { std::string str; std::getline(std::cin, str); std::cout << str;

pointer being freed was not allocated for pointer assignment

随声附和 提交于 2019-12-23 20:12:41
问题 I was trying to change a ListNode struct into a class format but am running into some issues when testing it. Getting a.out(7016) malloc: * error for object 0x7fff65333b10: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug chainLink.hpp #ifndef CHAINLINK_H #define CHAINLINK_H using namespace std; #include <iostream> #include <cstdlib> template <typename Object> class chainLink { private: Object storedValue; chainLink *nextLink; public: //Constructor