new-operator

delete[] supplied a modified new-ed pointer. Undefined Behaviour?

时光毁灭记忆、已成空白 提交于 2019-12-01 18:16:59
问题 I saw some code as below during a peer-code-review session: char *s = new char[3]; *s++ = 'a'; *s++ = 'b'; *s++='\0'; delete []s; // this may or may not crash on some or any day !! Firstly, I know that in Standard C++, pointing to one-past the array-size is O.K. though accessing it results in undefined behaviour. So I believe the last line *s++='\0' is fine. But if I recall correctly, the C++ standard mandates that delete should be supplied the same pointer that new returned. This I believe

Is the pointer guaranteed to be > a certain value?

穿精又带淫゛_ 提交于 2019-12-01 18:16:57
问题 In C++ when i do new (or even malloc) is there any guarantee that the return address will be greater than a certain value? Because... in this project i find it -very- useful to use 0-1k as a enum. But i wouldn't want to do that if its possible to get a value that low. My only target systems are 32 or 64bit CPUs with the OS window/linux and mac. Does the standard say anything about pointers? Does windows or linux say anything about their C runtime and what the lowest memory address (for ram)

Using new operator with return of a javascript function returns odd scope

删除回忆录丶 提交于 2019-12-01 18:05:29
Im trying to understand why new runs against the function rather than the return of the function in the example y = : function returnFunction(){ return function blah(str){ this.x = str; return this;}} y = new returnFunction()("blah") // output: Window {x: "blah"; top: Window, window: Window, location: Location, ....} x = new (returnFunction())("blah") // output: blah {x: "blah"} z = new function blah(){return this;}() // output: blah {} zz = new function(){return this;}() //note the missing function name // output: Object {} b = new function blib(str){this.x = str; return this} // blib {x:

Constructor invocation without parentheses [duplicate]

限于喜欢 提交于 2019-12-01 18:03:56
问题 This question already has answers here : Can we omit parentheses when creating an object using the “new” operator? (6 answers) Closed 5 years ago . Is there any difference between var obj1 = new Constructor; and var obj2 = new Constructor(); given that Constructor is a constructor function? 回答1: According to the MDN docs: [...] "new foo" is equivalent to "new foo()", i.e. if no argument list is specified, "foo" is called without arguments. 来源: https://stackoverflow.com/questions/11347046

delete[] supplied a modified new-ed pointer. Undefined Behaviour?

放肆的年华 提交于 2019-12-01 17:51:45
I saw some code as below during a peer-code-review session: char *s = new char[3]; *s++ = 'a'; *s++ = 'b'; *s++='\0'; delete []s; // this may or may not crash on some or any day !! Firstly, I know that in Standard C++, pointing to one-past the array-size is O.K. though accessing it results in undefined behaviour. So I believe the last line *s++='\0' is fine. But if I recall correctly, the C++ standard mandates that delete should be supplied the same pointer that new returned. This I believe means that the returned pointer must not be tampered-with. I guess it is because new might keep some

make sure object only created by factory (C#)

女生的网名这么多〃 提交于 2019-12-01 17:39:18
How do I make sure that a certain class is only instantiated by a factory and not by calling new directly? EDIT: I need the factory to be a separate class (for dependency injection purposes) so I can't make it a static method of the class to be instantiated, and so I can't make new private. If, for some reason, you need the factory and the constructed class to be in separate assemblies (which means simply using internal won't work), and you can ensure that your factory gets a chance to run first, you can do this: // In factory assembly: public class Factory { public Factory() { token = new

In C++, why is `new` needed to dynamically create an object rather just allocation?

喜夏-厌秋 提交于 2019-12-01 17:35:24
I've got this trivial class hierarchy: class Base { public: virtual int x( ) const = 0; }; class Derived : public Base { int _x; public: Derived( int x ) : _x(x) { } int x( ) const { return _x; } }; If I use malloc to allocate an instance of Derived , and then try to access the polymorphic function x , program crashes (I get a segmentation fault): int main( ) { Derived *d; d = (Derived*) malloc( sizeof(Derived) ); *d = Derived( 123 ); std::cout << d->x() << std::endl; // crash return 0; } Of course my actual application is a lot more complex (it's a sort of memory pool). I'm pretty sure it's

Override delete operator

旧巷老猫 提交于 2019-12-01 17:20:48
I want to override delete operator in my class. Here's what I am trying to do,but not succeeding. class Complex{ void *operator new(size_t s); void operator delete(void *ptr); }; void Complex::operator delete(void *ptr){ delete ptr; } I get the error: deleting void* is undefined As the error message indicates, you can't delete a void* . Try this: // See http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=40 #include <new> // for size_t class Complex { public: Complex() {} ~Complex() {} static void* operator new (size_t size) { return new char[size]; } static void operator delete

Can I detect whether I've been given a new object as a parameter?

我们两清 提交于 2019-12-01 17:09:54
Short Version For those who don't have the time to read my reasoning for this question below: Is there any way to enforce a policy of "new objects only" or "existing objects only" for a method's parameters ? Long Version There are plenty of methods which take objects as parameters, and it doesn't matter whether the method has the object "all to itself" or not. For instance: var people = new List<Person>(); Person bob = new Person("Bob"); people.Add(bob); people.Add(new Person("Larry")); Here the List<Person>.Add method has taken an "existing" Person (Bob) as well as a "new" Person (Larry), and

In C++, why is `new` needed to dynamically create an object rather just allocation?

允我心安 提交于 2019-12-01 17:08:31
问题 I've got this trivial class hierarchy: class Base { public: virtual int x( ) const = 0; }; class Derived : public Base { int _x; public: Derived( int x ) : _x(x) { } int x( ) const { return _x; } }; If I use malloc to allocate an instance of Derived , and then try to access the polymorphic function x , program crashes (I get a segmentation fault): int main( ) { Derived *d; d = (Derived*) malloc( sizeof(Derived) ); *d = Derived( 123 ); std::cout << d->x() << std::endl; // crash return 0; } Of