Comments (8)
what's the meaning of "preexited"?
preexited: pre + exited --- exist at or from an earlier time.
such as local objects. and example of the book:
const string &manip()
{
string ret;
// transform ret in some way
if (!ret.empty())
return ret; // WRONG: returning a reference to a local object!
else
return "Empty"; // WRONG: "Empty" is a local temporary string
}
Tip
One good way to ensure that the return is safe is to ask: To what preexisting
object is the reference referring?
---- Never Return a Reference or Pointer to a Local Object (6.3.2. Functions That Return a Value)
from cppprimer.
it will crash. (Windows + vs2013) I want to know why
loops endlessly.
check this: http://stackoverflow.com/questions/20184299/how-to-understand-and-fix-the-segmentation-fault-in-this-code
from cppprimer.
Function func can take 0 parameter
check this question: Mooophy/Cpp-Primer#129 , @Mooophy give very clear explanation.
and check this question: http://stackoverflow.com/questions/26530659/confused-about-array-parameters
You may have more better understanding.
from cppprimer.
@Ocxs is a good learner. 👍
from cppprimer.
@pezy @Mooophy 在SO上问的问题,根据被采纳的答案说的,我将我的理解说一下,您看对不对(英文不是太好)由于static 定义的迭代器it,导致其只会被初始化一次,直到程序结束才会释放。所以在第一次调用vector_print(_v);
没问题,但是由于while(cin >> s)
,s
被push_back到_v,然后就会导致迭代器失效,再次调用vector_print(_v);
,由于it
是用static声明的,他只能被初始化一次,所以这叫导致这时候的it
实际上指的就不是新加元素的_v
,解引用it
实际是解引用尾后迭代器,这是非法的在C++语法中?不知道理解的对不对?
Thank you for your explanation ! I got it .(BTW,I had some questions to invite you to answer in segmentfault ,if you have free time ,please help me .Thanks in advance . 👍
from cppprimer.
@Mooophy Thanks ! I will continue to work hard! 😄
from cppprimer.
不知道理解的对不对?
对的。就是这样。和你使用 const vector<T> _v
导致崩溃,其实大同小异。参数不加引用,第二次递归进入该函数的时候,此_v
早已不是彼_v
了。
from cppprimer.
@pezy Thanks a lot! 😄
from cppprimer.
Related Issues (20)
- Wording is wrong for exercise 2.10 HOT 2
- ex16_29 include error file,It compile failed HOT 2
- about 16.2.7 HOT 2
- Exercise 7.36
- Exercise 6.53
- Exercise 5.3 correct answer HOT 1
- Ex 6_8 HOT 1
- Ex 6_9 (added #include "fact.cpp")
- Ex 6_10 (added "return 0;")
- Exercise 13.37
- Answer to Exercises 3.37 is not entirely true.
- Ex16.49
- The new soulution of Exercise 5.12
- Ex.7.53 will not compile under the C++14 standard
- #include<exception>
- ex7_22.h 'class' instead of 'struct'
- Bug report exercise 16.12
- An error in exercise 16.28, in file <unique_pointer.h>
- Small modification for answer of Exercise 3.9 - mention `s` is a empty string
- Exercise 9.28(The loop may not stop)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppprimer.