p0y-cpp-review - princeton university

15
Stuff++

Upload: others

Post on 10-May-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: P0Y-cpp-review - Princeton University

Stuff++

Page 2: P0Y-cpp-review - Princeton University

Stack Variables

Page 3: P0Y-cpp-review - Princeton University

Heap Variables

Page 4: P0Y-cpp-review - Princeton University

And now in C++

Page 5: P0Y-cpp-review - Princeton University

Arrays

Page 6: P0Y-cpp-review - Princeton University

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

ctors and dtors

Page 8: P0Y-cpp-review - Princeton University

ctors and dtors

Ctor called. this = 0x7fff56249a38Ctor called. this = 0x7fff56249a08Dtor called. this = 0x7fff56249a08Dtor called. this = 0x7fff56249a38

Page 9: P0Y-cpp-review - Princeton University

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

ctors and dtors

Ctor called with 7. this = 0x7fff5309ba38Dtor called. this = 0x7fff5309ba38

Page 11: P0Y-cpp-review - Princeton University

ctors and dtors and assignments

Ctor calledCtor calledCopy ctor calledAssignment calledDtor calledDtor calledDtor called

Page 12: P0Y-cpp-review - Princeton University

* 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

const

Page 14: P0Y-cpp-review - Princeton University

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

Find the bug

cpp_precept.cpp:8:11: warning: address of stack memory associated with local variable 'THE_NUMBER' returned [-Wreturn-stack-address]