c++-faq

Why does the use of 'new' cause memory leaks?

只愿长相守 提交于 2019-12-16 19:53:36
问题 I learned C# first, and now I'm starting with C++. As I understand, operator new in C++ is not similar to the one in C#. Can you explain the reason of the memory leak in this sample code? class A { ... }; struct B { ... }; A *object1 = new A(); B object2 = *(new B()); 回答1: What is happening When you write T t; you're creating an object of type T with automatic storage duration . It will get cleaned up automatically when it goes out of scope. When you write new T() you're creating an object of

Why does the use of 'new' cause memory leaks?

只愿长相守 提交于 2019-12-16 19:51:20
问题 I learned C# first, and now I'm starting with C++. As I understand, operator new in C++ is not similar to the one in C#. Can you explain the reason of the memory leak in this sample code? class A { ... }; struct B { ... }; A *object1 = new A(); B object2 = *(new B()); 回答1: What is happening When you write T t; you're creating an object of type T with automatic storage duration . It will get cleaned up automatically when it goes out of scope. When you write new T() you're creating an object of

What is “rvalue reference for *this”?

妖精的绣舞 提交于 2019-12-16 19:04:13
问题 Came across a proposal called "rvalue reference for *this" in clang's C++11 status page. I've read quite a bit about rvalue references and understood them, but I don't think I know about this. I also couldn't find much resources on the web using the terms. There's a link to the proposal paper on the page: N2439 (Extending move semantics to *this), but I'm also not getting much examples from there. What is this feature about? 回答1: First, "ref-qualifiers for *this" is a just a "marketing

C++ delete - It deletes my objects but I can still access the data?

半城伤御伤魂 提交于 2019-12-16 18:51:50
问题 I have written a simple, working tetris game with each block as an instance of a class singleblock. class SingleBlock { public: SingleBlock(int, int); ~SingleBlock(); int x; int y; SingleBlock *next; }; class MultiBlock { public: MultiBlock(int, int); SingleBlock *c, *d, *e, *f; }; SingleBlock::SingleBlock(int a, int b) { x = a; y = b; } SingleBlock::~SingleBlock() { x = 222; } MultiBlock::MultiBlock(int a, int b) { c = new SingleBlock (a,b); d = c->next = new SingleBlock (a+10,b); e = d-

C++ delete - It deletes my objects but I can still access the data?

安稳与你 提交于 2019-12-16 18:51:49
问题 I have written a simple, working tetris game with each block as an instance of a class singleblock. class SingleBlock { public: SingleBlock(int, int); ~SingleBlock(); int x; int y; SingleBlock *next; }; class MultiBlock { public: MultiBlock(int, int); SingleBlock *c, *d, *e, *f; }; SingleBlock::SingleBlock(int a, int b) { x = a; y = b; } SingleBlock::~SingleBlock() { x = 222; } MultiBlock::MultiBlock(int a, int b) { c = new SingleBlock (a,b); d = c->next = new SingleBlock (a+10,b); e = d-

How come a non-const reference cannot bind to a temporary object?

安稳与你 提交于 2019-12-16 18:16:11
问题 Why is it not allowed to get non-const reference to a temporary object, which function getx() returns? Clearly, this is prohibited by C++ Standard but I am interested in the purpose of such restriction, not a reference to the standard. struct X { X& ref() { return *this; } }; X getx() { return X();} void g(X & x) {} int f() { const X& x = getx(); // OK X& x = getx(); // error X& x = getx().ref(); // OK g(getx()); //error g(getx().ref()); //OK return 0; } It is clear that the lifetime of the

How does the compilation/linking process work?

雨燕双飞 提交于 2019-12-16 18:15:37
问题 How does the compilation and linking process work? (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.) 回答1: The compilation of a C++ program involves

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

℡╲_俬逩灬. 提交于 2019-12-14 03:08:19
问题 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

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

随声附和 提交于 2019-12-13 06:53:39
问题 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

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

六眼飞鱼酱① 提交于 2019-12-13 03:59:41
问题 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