c++-faq

How might I overload the “new” operator to allocate memory from a secondary memory device?

我与影子孤独终老i 提交于 2020-01-20 04:22:27
问题 I am looking for a syntax to allocate memory from a secondary memory device and not from the default heap. How can i implement it? Using malloc() would by default take it from heap... Surely there must be another way! 回答1: #include <new> void* operator new(std::size_t size) throw(std::bad_alloc) { while (true) { void* result = allocate_from_some_other_source(size); if (result) return result; std::new_handler nh = std::set_new_handler(0); std::set_new_handler(nh); // put it back // this is

How do I prevent a class from being allocated via the 'new' operator? (I'd like to ensure my RAII class is always allocated on the stack.)

余生颓废 提交于 2020-01-18 07:13:26
问题 I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from being allocated via the 'new' operator? 回答1: All you need to do is declare the class' new operator private: class X { private: // Prevent heap allocation void * operator new (size_t); void * operator new[] (size_t); void operator delete (void *); void operator delete[] (void*); // ... // The rest of the implementation for X // ... }; Making 'operator new' private effectively prevents code outside

What is an undefined reference/unresolved external symbol error and how do I fix it?

佐手、 提交于 2020-01-16 19:47:05
问题 What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them? Feel free to edit/add your own. 回答1: Compiling a C++ program takes place in several steps, as specified by 2.2 (credits to Keith Thompson for the reference): The precedence among the syntax rules of translation is specified by the following phases [see footnote] . Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set

Valid expressions for default function arguments

♀尐吖头ヾ 提交于 2020-01-13 09:23:26
问题 What are all the possible types of valid expressions for a default argument in a function or member function? 回答1: Anything that is correct within context of assignment to a variable of function parameter's type. Edit The default arguments during compilation are evaluated in terms of type correctness etc, but they are not calculated and no assignment takes place until run-time. You can specify a constructor of a yet to be defined class as a default argument and it's fine, as long as class is

What are the differences between struct and class in C++?

a 夏天 提交于 2020-01-11 07:51:10
问题 This question was already asked in the context of C#/.Net. Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design. I'll start with an obvious difference: If you don't specify public: or private: , members of a struct are public by default; members of a class are private by default. I'm sure there are other differences to be found in the obscure corners of the C++

Default constructor with empty brackets

时光总嘲笑我的痴心妄想 提交于 2020-01-06 23:42:31
问题 Is there any good reason that an empty set of round brackets (parentheses) isn't valid for calling the default constructor in C++? MyObject object; // ok - default ctor MyObject object(blah); // ok MyObject object(); // error I seem to type "()" automatically everytime. Is there a good reason this isn't allowed? 回答1: Most vexing parse This is related to what is known as "C++'s most vexing parse". Basically, anything that can be interpreted by the compiler as a function declaration will be

What happens when an exception goes unhandled in a multithreaded C++11 program?

你。 提交于 2019-12-29 02:47:30
问题 If I have a C++11 program running two threads, and one of them throws an unhandled exception, what happens? Will the entire program die a fiery death? Will the thread where the exception is thrown die alone (and if so, can I obtain the exception in this case)? Something else entirely? 回答1: Nothing has really changed. The wording in n3290 is: If no matching handler is found, the function std::terminate() is called The behavior of terminate can be customized with set_terminate , but: Required

Meaning of default initialization changed in C++11?

佐手、 提交于 2019-12-28 05:43:11
问题 C++2003 8.5/5 says: To default-initialize an object of type T means: — if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); — if T is an array type, each element is default-initialized; — otherwise, the object is zero-initialized . [Emphasis added.] The C++2011 standard changed that last item to — otherwise, no initialization is performed . This seems like it would be a breaking

What are the advantages of using nullptr?

浪子不回头ぞ 提交于 2019-12-27 10:33:04
问题 This piece of code conceptually does the same thing for the three pointers (safe pointer initialization): int* p1 = nullptr; int* p2 = NULL; int* p3 = 0; And so, what are the advantages of assigning pointers nullptr over assigning them the values NULL or 0 ? 回答1: In that code, there doesn't seem to be an advantage. But consider the following overloaded functions: void f(char const *ptr); void f(int v); f(NULL); //which function will be called? Which function will be called? Of course, the

What are the advantages of using nullptr?

家住魔仙堡 提交于 2019-12-27 10:30:33
问题 This piece of code conceptually does the same thing for the three pointers (safe pointer initialization): int* p1 = nullptr; int* p2 = NULL; int* p3 = 0; And so, what are the advantages of assigning pointers nullptr over assigning them the values NULL or 0 ? 回答1: In that code, there doesn't seem to be an advantage. But consider the following overloaded functions: void f(char const *ptr); void f(int v); f(NULL); //which function will be called? Which function will be called? Of course, the