-1

If I do cin >> myVar does that data live on the stack? does it live in function 'scope' and is undefined outside my function? does it live inside an application boundary?

4

1 回答 1

3

cin uses a buffer and writes the data to myVar so it depends on how you declared myVar.

If myVar is a local parameter then its data is stored on the stack and goes out of scope at the end of your function.

If myVar was dynamically allocated from the heap (using the "new" keyword) then the data lives on the heap. In this case there are several ways to manage the scope/lifespan (i.e RAII, or smart pointers) of the data but the bottom line is that the programmer is responsible for ensuring the memory gets properly released. If the memory is not released before hand, it will be released when the program ends.

于 2013-10-19T02:21:16.760 回答