Can you obtain the size of an array allocated with new T[]?

delete [] does know the size that was allocated. However, that knowledge resides in the runtime or in the operating system’s memory manager, meaning that it is not available to the compiler during compilation. And sizeof() is not a real function, it is actually evaluated to a constant by the compiler, which is something it … Read more

Why new std::nothrow version is not widely used [duplicate]

However, I hardly see this version in my experience. You would use it (or, equivalently, catch the exception from the default version) if you can handle the failure locally; perhaps by requesting to free some other memory and then retrying, or by trying to allocate something smaller, or using an alternative algorithm that doesn’t need … Read more

Is it possible to use a C++ smart pointers together with C’s malloc?

If you are using shared_ptr or unique_ptr, you can specify a custom deleter. For example, struct free_delete { void operator()(void* x) { free(x); } }; This can be used with shared_ptr like so: std::shared_ptr<int> sp((int*)malloc(sizeof(int)), free_delete()); If you are using unique_ptr, the deleter is a part of the unique_ptr‘s type, so the deleter needs to … Read more

Will new operator return NULL? [duplicate]

On a standards-conforming C++ implementation, no. The ordinary form of new will never return NULL; if allocation fails, a std::bad_alloc exception will be thrown (the new (nothrow) form does not throw exceptions, and will return NULL if allocation fails). On some older C++ compilers (especially those that were released before the language was standardized) or … Read more

call parent constructor in ruby

Ruby doesn’t have constructors, therefore it’s obviously not possible to call them, parent or otherwise. Ruby does have methods, however, and in order to call the parent’s method with the same name as the currently executing method, you can use the super keyword. [Note: super without arguments is a shortcut for passing the same arguments … Read more

What does the ‘new’ keyword actually do in Java, and should I avoid creating new objects?

Yes, if you called myMethod() 10 times it will create 10 unique and separate objects. The new keyword does exactly what it says on the tin, it creates a brand new object, irrespective of whether one already exists. It creates a new object and stuffs the reference to that object inside the variable it has … Read more

Difference between global non-throwing ::operator new and std::malloc

The main differences, aside from syntax and free vs. delete, are you can portably replace ::operator new; malloc comes with realloc, for which new has no equivalent; new has the concept of a new_handler, for which there is no malloc equivalent. (Replacing malloc opens up a can of worms. It can be done, but not … Read more

Differences between new Integer(123), Integer.valueOf(123) and just 123

new Integer(123) will create a new Object instance for each call. According to the javadoc, Integer.valueOf(123) has the difference it caches Objects… so you may (or may not) end up with the same Object if you call it more than once. For instance, the following code: public static void main(String[] args) { Integer a = … Read more

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