P0Y-cpp-review - Princeton University
Transcript of P0Y-cpp-review - Princeton University
![Page 1: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/1.jpg)
Stuff++
![Page 2: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/2.jpg)
Stack Variables
![Page 3: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/3.jpg)
Heap Variables
![Page 4: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/4.jpg)
And now in C++
![Page 5: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/5.jpg)
Arrays
![Page 6: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/6.jpg)
malloc vs. newmalloc new
allocates memory allocates memory
requires size and cast does not require size and cast
provides uninitialized memory calls the constructors
use in C code use in C++ code
![Page 7: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/7.jpg)
ctors and dtors
![Page 8: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/8.jpg)
ctors and dtors
Ctor called. this = 0x7fff56249a38Ctor called. this = 0x7fff56249a08Dtor called. this = 0x7fff56249a08Dtor called. this = 0x7fff56249a38
![Page 9: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/9.jpg)
ctors and dtors
Ctor called. this = 0x7fbdeb403978Ctor called. this = 0x7fbdeb403979Ctor called. this = 0x7fbdeb40397aDtor called. this = 0x7fbdeb40397aDtor called. this = 0x7fbdeb403979Dtor called. this = 0x7fbdeb403978
![Page 10: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/10.jpg)
ctors and dtors
Ctor called with 7. this = 0x7fff5309ba38Dtor called. this = 0x7fff5309ba38
![Page 11: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/11.jpg)
ctors and dtors and assignments
Ctor calledCtor calledCopy ctor calledAssignment calledDtor calledDtor calledDtor called
![Page 12: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/12.jpg)
* and &
declare a pointer int* p;
dereference a pointer *p = 5;
get memory address int i = 9; cout << &i;
declare a reference int& r = i;
![Page 13: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/13.jpg)
const
![Page 14: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/14.jpg)
Find the bug
cpp_precept.cpp:8:11: warning: address of stack memory associated with local variable 'THE_NUMBER' returned [-Wreturn-stack-address]
![Page 15: P0Y-cpp-review - Princeton University](https://reader034.fdocuments.in/reader034/viewer/2022051307/6279dcf309f6b12bcf4c6307/html5/thumbnails/15.jpg)
Find the bug
cpp_precept.cpp:8:11: warning: address of stack memory associated with local variable 'THE_NUMBER' returned [-Wreturn-stack-address]