code_junkie

C++ - What does “Stack automatic” mean?

Nov 10th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. {
  2. obj a;
  3. } // a is destroyed here
  4.  
  5. {
  6. obj* b = new obj;
  7. }
  8.  
  9. {
  10. FILE* pF = fopen( ... );
  11. // ... do sth with pF
  12. fclose( pF );
  13. }
  14.  
  15. {
  16. std::fstream f( ... );
  17. // do sth with f
  18. } // here f gets auto magically destroyed and the destructor frees the file
  19.  
  20. {
  21. string v( "bob" );
  22. string k;
  23.  
  24. v = k
  25. // v now contains "bob"
  26. } // v + k are destroyed here, and any memory used by v + k is freed
  27.  
  28. int main() {
  29. int a;
  30. auto int b;
  31. }
  32.  
  33. auto a = std::max(1.0, 4.0); // `a` now has type double.
  34.  
  35. MyObject x = MyObject(params); // onto the stack
  36.  
  37. MyObject * y = new MyObject(params); // onto the heap
Add Comment
Please, Sign In to add comment