new-operator

About constructors/destructors and new/delete operators in C++ for custom objects

好久不见. 提交于 2019-12-20 02:35:14
问题 Suppose I have a Linked List I created myself. It has its own destructor, which frees the memory. This Linked List does not overload new or delete. Now, I'm trying to create an array of said linked lists (open hashing, if I understand correctly). Then I allocate the necessary memory inside the constructor of this open hashing class. The new operator being called inside the constructor is enough to correctly allocate the memory for the array, right? I'm not sure because I haven't overloaded

make sure object only created by factory (C#)

亡梦爱人 提交于 2019-12-19 18:48:08
问题 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. 回答1: 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

thread.start_new_thread vs threading.Thread.start

青春壹個敷衍的年華 提交于 2019-12-19 15:28:32
问题 What is the difference between thread.start_new_thread and threading.Thread.start in python? I have noticed that when start_new_thread is called, the new thread terminates as soon as the calling thread terminates. threading.Thread.start is the opposite: the calling thread waits for other threads to terminate. 回答1: The thread module is the low-level threading API of Python. Its direct usage isn't recommended, unless you really need to. The threading module is a high-level API, built on top of

thread.start_new_thread vs threading.Thread.start

妖精的绣舞 提交于 2019-12-19 15:28:25
问题 What is the difference between thread.start_new_thread and threading.Thread.start in python? I have noticed that when start_new_thread is called, the new thread terminates as soon as the calling thread terminates. threading.Thread.start is the opposite: the calling thread waits for other threads to terminate. 回答1: The thread module is the low-level threading API of Python. Its direct usage isn't recommended, unless you really need to. The threading module is a high-level API, built on top of

thread.start_new_thread vs threading.Thread.start

一曲冷凌霜 提交于 2019-12-19 15:25:48
问题 What is the difference between thread.start_new_thread and threading.Thread.start in python? I have noticed that when start_new_thread is called, the new thread terminates as soon as the calling thread terminates. threading.Thread.start is the opposite: the calling thread waits for other threads to terminate. 回答1: The thread module is the low-level threading API of Python. Its direct usage isn't recommended, unless you really need to. The threading module is a high-level API, built on top of

Using malloc instead of new, and calling the copy constructor when the object is created

自作多情 提交于 2019-12-19 09:03:16
问题 I wanted to try out TBB's scalable_allocator, but was confused when I had to replace some of my code. This is how allocation is done with the allocator: SomeClass* s = scalable_allocator<SomeClass>().allocate( sizeof(SomeClass) ); EDIT: What's shown above is not how allocation is done with scalable_allocator. As ymett correctly mentioned, allocation is done like this: int numberOfObjectsToAllocateFor = 1; SomeClass* s = scalable_allocator<SomeClass>().allocate( numberOfObjectsToAllocateFor );

Operator new[] does not receive extra bytes

限于喜欢 提交于 2019-12-19 06:39:32
问题 I have such code #include <cstdlib> class Foo { int m_data; public : Foo() : m_data(0) { } /*~Foo() { }*/ static void* operator new[](const size_t size) { return malloc(size); } static void operator delete[](void* data) { free(data); } }; int main() { Foo* objects = new Foo[5]; delete [] objects; } In this case I receive value of size in operator new overloading as 20 bytes as I wanted ( sizeof(int) * 5 ). But if I uncomment the destructor I get size as 24 bytes. Yeah, I now that these extra

size_t parameter new operator

夙愿已清 提交于 2019-12-18 21:36:13
问题 I have a point in my mind which I can't figure out about new operator overloading. Suppose that, I have a class MyClass yet MyClass.h MyClass.cpp and main.cpp files are like; //MyClass.h class MyClass { public: //Some member functions void* operator new (size_t size); void operator delete (void* ptr); //... }; //MyClass.cpp void* MyClass::operator new(size_t size) { return malloc(size); } void MyClass::operator delete(void* ptr) { free(ptr); } //main.cpp //Include files //... int main() {

size_t parameter new operator

♀尐吖头ヾ 提交于 2019-12-18 21:36:08
问题 I have a point in my mind which I can't figure out about new operator overloading. Suppose that, I have a class MyClass yet MyClass.h MyClass.cpp and main.cpp files are like; //MyClass.h class MyClass { public: //Some member functions void* operator new (size_t size); void operator delete (void* ptr); //... }; //MyClass.cpp void* MyClass::operator new(size_t size) { return malloc(size); } void MyClass::operator delete(void* ptr) { free(ptr); } //main.cpp //Include files //... int main() {

Array of structs and new / delete

↘锁芯ラ 提交于 2019-12-18 16:30:29
问题 I have a struct like this: class Items { private: struct item { unsigned int a, b, c; }; item* items[MAX_ITEMS]; } Say I wanted to 'delete' an item, like so: items[5] = NULL; And I created a new item on that same spot later: items[5] = new item; Would I still need to call delete[] to clean this up? Or won't this be needed since bounds of array items[] are known before compiling? Is setting that pointer to NULL valid or should I be calling delete there? 回答1: You need to call delete before