Why are non-placement `new` and `delete` built into the language and not just regular functions?

If the user’s goal was to create an object in some memory location, then new seemed like a natural approach since forwarding references, variadic templates and placement new were not a thing back in those days. As correctly pointed out by @T.C. templates were released in 1990 and placement new in 1989. Variadic templates on … Read more

How to control memory allocation strategy in third party library code?

Ugh, my sympathy. This is going to depend a lot on your compiler, your libc, etc. Some rubber-meets-road strategies that have “worked” to varying degrees for us in the past (/me braces for downvotes) are: The operator new / operator delete overloads you suggested — although note that some compilers are picky about not having … Read more

Setup std::vector in class constructor

Just do: MyClass::MyClass(int m_size) : size(m_size), vec(m_size, 0) You already seem to know about initializer lists, why not initialize vector there directly? vec = new vector<int>(size,0); is illegal because new returns a pointer and in your case vec is an object. Your second option: vector<int> temp(size,0); vec = temp; although it compiles, does extra work … Read more

What is the difference between the new operator and Class.newInstance()?

The new operator creates a new object of a type that’s known statically (at compile-time) and can call any constructor on the object you’re trying to create. It’s the preferred way of creating an object – it’s fast and the JVM does lots of aggressive optimizations on it. Class.forName().newInstance() is a dynamic construct that looks … Read more

Scalar `new T` vs array `new T[1]`

If T doesn’t have trivial destructor, then for usual compiler implementations, new T[1] has an overhead compared to new T. The array version will allocate a little bit larger memory area, to store the number of elements, so at delete[], it knows how many destructors must be called. So, it has an overhead: a little … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)