c++ traps and pitfalls - petr zemek...2016/04/22  · petr zemek: c++ traps and pitfalls 12/24...

42
C++ Traps and Pitfalls Petr Zemek Principal Developer Security/Engineering/VirusLab http://petrzemek.net C++ Developers’ Guild, 2016-04-22

Upload: others

Post on 01-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

C++ Traps and Pitfalls

Petr ZemekPrincipal Developer

Security/Engineering/VirusLabhttp://petrzemek.net

C++ Developers’ Guild, 2016-04-22

Page 2: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Introduction

What is a pitfall?

Example:

1 std::ifstream inputFile("C:\test.txt");2 if (!inputFile) {3 std::cerr << "Failed to open file!";4 }

Petr Zemek: C++ Traps and Pitfalls 2 / 24

Page 3: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Introduction

What is a pitfall?

Example:

1 std::ifstream inputFile("C:\test.txt");2 if (!inputFile) {3 std::cerr << "Failed to open file!";4 }

Petr Zemek: C++ Traps and Pitfalls 2 / 24

Page 4: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Virtual Functions and Default Parameters

1 class A {2 public:3 virtual void foo(int i = 1) {4 std::cout << i << ’\n’;5 }6 };78 class B: public A {9 public:

10 virtual void foo(int i = 2) override {11 std::cout << i << ’\n’;12 }13 };1415 std::unique_ptr<A> p(new B);16 p->foo();

Petr Zemek: C++ Traps and Pitfalls 3 / 24

Page 5: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Virtual Functions Inside Ctors/Dtors

1 class A {2 public:3 A() { foo(); }4 virtual ˜A() { foo(); }56 virtual void foo() {7 std::cout << "A::foo()\n";8 }9 };

1011 class B: public A {12 public:13 virtual void foo() override {14 std::cout << "B::foo()\n";15 }16 };1718 std::unique_ptr<A> p(new B);

Petr Zemek: C++ Traps and Pitfalls 4 / 24

Page 6: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Evaluation Order

1 auto result = filterInput(2 std::shared_ptr<Filter>(new Filter),3 getInput()4 );

• auto tmp = new Filter;

• auto input = getInput();

• auto filter = std::shared ptr<Filter>(tmp);

• auto result = filterInput(filter, input);

Petr Zemek: C++ Traps and Pitfalls 5 / 24

Page 7: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Evaluation Order

1 auto result = filterInput(2 std::shared_ptr<Filter>(new Filter),3 getInput()4 );

• auto tmp = new Filter;

• auto input = getInput();

• auto filter = std::shared ptr<Filter>(tmp);

• auto result = filterInput(filter, input);

Petr Zemek: C++ Traps and Pitfalls 5 / 24

Page 8: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Evaluation Order

1 auto result = filterInput(2 std::shared_ptr<Filter>(new Filter),3 getInput()4 );

• auto tmp = new Filter;

• auto input = getInput();

• auto filter = std::shared ptr<Filter>(tmp);

• auto result = filterInput(filter, input);

Petr Zemek: C++ Traps and Pitfalls 5 / 24

Page 9: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Evaluation Order

1 auto result = filterInput(2 std::shared_ptr<Filter>(new Filter),3 getInput()4 );

• auto tmp = new Filter;

• auto input = getInput();

• auto filter = std::shared ptr<Filter>(tmp);

• auto result = filterInput(filter, input);

Petr Zemek: C++ Traps and Pitfalls 5 / 24

Page 10: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Evaluation Order

1 auto result = filterInput(2 std::shared_ptr<Filter>(new Filter),3 getInput()4 );

• auto tmp = new Filter;

• auto input = getInput();

• auto filter = std::shared ptr<Filter>(tmp);

• auto result = filterInput(filter, input);

Petr Zemek: C++ Traps and Pitfalls 5 / 24

Page 11: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Evaluation Order

1 auto result = filterInput(2 std::shared_ptr<Filter>(new Filter),3 getInput()4 );

• auto tmp = new Filter;

• auto input = getInput();

• auto filter = std::shared ptr<Filter>(tmp);

• auto result = filterInput(filter, input);

Petr Zemek: C++ Traps and Pitfalls 5 / 24

Page 12: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction

1 class A {2 public:3 // ...45 };

6 A a(1);7 A b{1};8 A c = 1;9 auto d = A(1);

Petr Zemek: C++ Traps and Pitfalls 6 / 24

Page 13: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Version I)

1 class A {2 public:3 A(int);45 };

6 A a(1); // A(int)7 A b{1}; // A(int)8 A c = 1; // A(int), A(const A &)?9 auto d = A(1); // A(int), A(const A &)?

Petr Zemek: C++ Traps and Pitfalls 7 / 24

Page 14: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Version II)

1 class A {2 public:3 explicit A(int);45 };

6 A a(1); // A(int)7 A b{1}; // A(int)8 A c = 1; // fails to compile9 auto d = A(1); // A(int), A(const A &)?

Petr Zemek: C++ Traps and Pitfalls 8 / 24

Page 15: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Version III)

1 class A {2 public:3 A(int);4 A(const A &) = delete;5 };

6 A a(1); // A(int)7 A b{1}; // A(int)8 A c = 1; // fails to compile9 auto d = A(1); // fails to compile

Petr Zemek: C++ Traps and Pitfalls 9 / 24

Page 16: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Version IV)

1 class A {2 public:3 A(int);4 A(std::initializer_list<float>);5 };

6 A a(1); // A(int)7 A b{1}; // A(std::initializer_list<float>)8 A c = 1; // A(int), A(const A &)?9 auto d = A(1); // A(int), A(const A &)?

Petr Zemek: C++ Traps and Pitfalls 10 / 24

Page 17: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Riddle I)

1 class A {2 public:3 A();4 A(int);5 A(std::initializer_list<int>);6 };

7 A b{1, 2}; // A(std::initializer_list<int>)8 A b{1}; // A(std::initializer_list<int>)9 A a{}; // ?

Petr Zemek: C++ Traps and Pitfalls 11 / 24

Page 18: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Riddle I)

1 class A {2 public:3 A();4 A(int);5 A(std::initializer_list<int>);6 };

7 A b{1, 2}; // A(std::initializer_list<int>)8 A b{1}; // A(std::initializer_list<int>)9 A a{}; // A()

Petr Zemek: C++ Traps and Pitfalls 11 / 24

Page 19: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Riddle II)

1 auto i{1};2 auto j = {1};

What are the types of i and j?

i j

C++14 std::initializer list<int> std::initializer list<int>

C++1z int std::initializer list<int>

GCC 4.9 (C++14) std::initializer list<int> std::initializer list<int>

GCC 5 (C++14) int std::initializer list<int>

Clang 3.7 (C++14) std::initializer list<int> std::initializer list<int>

Clang 3.8 (C++14) int std::initializer list<int>

MSVC 2015 int std::initializer list<int>

Petr Zemek: C++ Traps and Pitfalls 12 / 24

Page 20: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Riddle II)

1 auto i{1};2 auto j = {1};

What are the types of i and j?

i j

C++14 std::initializer list<int> std::initializer list<int>

C++1z int std::initializer list<int>

GCC 4.9 (C++14) std::initializer list<int> std::initializer list<int>

GCC 5 (C++14) int std::initializer list<int>

Clang 3.7 (C++14) std::initializer list<int> std::initializer list<int>

Clang 3.8 (C++14) int std::initializer list<int>

MSVC 2015 int std::initializer list<int>

Petr Zemek: C++ Traps and Pitfalls 12 / 24

Page 21: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Riddle II)

1 auto i{1};2 auto j = {1};

What are the types of i and j?

i j

C++14 std::initializer list<int> std::initializer list<int>

C++1z int std::initializer list<int>

GCC 4.9 (C++14) std::initializer list<int> std::initializer list<int>

GCC 5 (C++14) int std::initializer list<int>

Clang 3.7 (C++14) std::initializer list<int> std::initializer list<int>

Clang 3.8 (C++14) int std::initializer list<int>

MSVC 2015 int std::initializer list<int>

Petr Zemek: C++ Traps and Pitfalls 12 / 24

Page 22: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Riddle II)

1 auto i{1};2 auto j = {1};

What are the types of i and j?

i j

C++14 std::initializer list<int> std::initializer list<int>

C++1z int std::initializer list<int>

GCC 4.9 (C++14) std::initializer list<int> std::initializer list<int>

GCC 5 (C++14) int std::initializer list<int>

Clang 3.7 (C++14) std::initializer list<int> std::initializer list<int>

Clang 3.8 (C++14) int std::initializer list<int>

MSVC 2015 int std::initializer list<int>

Petr Zemek: C++ Traps and Pitfalls 12 / 24

Page 23: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Riddle II)

1 auto i{1};2 auto j = {1};

What are the types of i and j?

i j

C++14 std::initializer list<int> std::initializer list<int>

C++1z int std::initializer list<int>

GCC 4.9 (C++14) std::initializer list<int> std::initializer list<int>

GCC 5 (C++14) int std::initializer list<int>

Clang 3.7 (C++14) std::initializer list<int> std::initializer list<int>

Clang 3.8 (C++14) int std::initializer list<int>

MSVC 2015 int std::initializer list<int>

Petr Zemek: C++ Traps and Pitfalls 12 / 24

Page 24: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Riddle II)

1 auto i{1};2 auto j = {1};

What are the types of i and j?

i j

C++14 std::initializer list<int> std::initializer list<int>

C++1z int std::initializer list<int>

GCC 4.9 (C++14) std::initializer list<int> std::initializer list<int>

GCC 5 (C++14) int std::initializer list<int>

Clang 3.7 (C++14) std::initializer list<int> std::initializer list<int>

Clang 3.8 (C++14) int std::initializer list<int>

MSVC 2015 int std::initializer list<int>

Petr Zemek: C++ Traps and Pitfalls 12 / 24

Page 25: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Object Construction (Riddle III)

1 class A {2 public:3 A();4 A(const A &) = delete;5 };67 auto a = A(); // fails to compile89 auto p = std::unique_ptr<int>(); // OK (why?)

Petr Zemek: C++ Traps and Pitfalls 13 / 24

Page 26: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Move Semantics

1 class Processor {2 public:3 // ...45 void process(const std::string d) {6 // ...78 data = std::move(d);9 }

1011 private:12 std::string data;1314 // ...15 };

Petr Zemek: C++ Traps and Pitfalls 14 / 24

Page 27: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Move Semantics (Huh?)

1 class Processor {2 public:3 // ...45 void process(const std::string d) {6 // ...78 data = std::move(d);9 }

1011 private:12 std::string data;1314 // ...15 };

Petr Zemek: C++ Traps and Pitfalls 15 / 24

Page 28: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Move Semantics (Explanation)

1 class string {2 public:3 // ...45 string &operator=(const string &other);6 string &operator=(string &&other);78 // ...9 };

1011 // ...1213 void process(const std::string d) {14 // ...1516 data = std::move(d); // copies d!17 }

Petr Zemek: C++ Traps and Pitfalls 16 / 24

Page 29: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Operations Over Integers

std::cout << -sizeof(char) << ’\n’;

Prints:

4294967295 (32b system)18446744073709551615 (64b system)

Petr Zemek: C++ Traps and Pitfalls 17 / 24

Page 30: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Operations Over Integers

std::cout << -sizeof(char) << ’\n’;

Prints:

4294967295 (32b system)18446744073709551615 (64b system)

Petr Zemek: C++ Traps and Pitfalls 17 / 24

Page 31: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Comparing Signed and Unsigned Integers

1 int i = -1;2 unsigned int j = 1;34 if (i < j) {5 // ...6 }

Petr Zemek: C++ Traps and Pitfalls 18 / 24

Page 32: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Iterating Over Containers

1 std::vector<int> v;2 // ...34 for (unsigned int i = 0; i < v.size(); ++i) {5 // ...6 }

On a 64b system:

i = 0 // v.size() == 4294967296 (UINT_MAX + 1)i = 1 // v.size() == 4294967296... // v.size() == 4294967296i = 4294967294 // v.size() == 4294967296i = 4294967295 // v.size() == 4294967296i = 0 // v.size() == 4294967296i = 1 // v.size() == 4294967296...

Petr Zemek: C++ Traps and Pitfalls 19 / 24

Page 33: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Iterating Over Containers

1 std::vector<int> v;2 // ...34 for (unsigned int i = 0; i < v.size(); ++i) {5 // ...6 }

On a 64b system:

i = 0 // v.size() == 4294967296 (UINT_MAX + 1)i = 1 // v.size() == 4294967296... // v.size() == 4294967296i = 4294967294 // v.size() == 4294967296i = 4294967295 // v.size() == 4294967296i = 0 // v.size() == 4294967296i = 1 // v.size() == 4294967296...

Petr Zemek: C++ Traps and Pitfalls 19 / 24

Page 34: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Default Lambda Captures

1 class DivFilter {2 public:3 DivFilter(int divisor);45 void addFilter(Filters &filters) {6 filters.push_back(7 [=](int value) {8 return value % divisor == 0;9 }

10 );11 }1213 private:14 int divisor;15 };

Petr Zemek: C++ Traps and Pitfalls 20 / 24

Page 35: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Default Lambda Captures (Continued)

1 class DivFilter {2 public:3 //...4 void addFilter(Filters &filters) {5 filters.push_back(6 [divisor](int value) {7 return value % divisor == 0;8 }9 );

10 }1112 private:13 int divisor;14 };

Fails to compile:

error: capture of non-variable ‘DivFilter::divisor’[divisor](int value) {ˆ

Petr Zemek: C++ Traps and Pitfalls 21 / 24

Page 36: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Default Lambda Captures (Continued)

1 class DivFilter {2 public:3 //...4 void addFilter(Filters &filters) {5 filters.push_back(6 [divisor](int value) {7 return value % divisor == 0;8 }9 );

10 }1112 private:13 int divisor;14 };

Fails to compile:

error: capture of non-variable ‘DivFilter::divisor’[divisor](int value) {ˆ

Petr Zemek: C++ Traps and Pitfalls 21 / 24

Page 37: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Default Lambda Captures (Explanation)

1 class DivFilter {2 public:3 //...4 void addFilter(Filters &filters) {5 filters.push_back(6 [this](int value) {7 return value % this->divisor == 0;8 }9 );

10 }1112 private:13 int divisor;14 };

Petr Zemek: C++ Traps and Pitfalls 22 / 24

Page 38: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Default Lambda Captures (Solution)

1 class DivFilter {2 public:3 //...4 void addFilter(Filters &filters) {5 filters.push_back(6 [divisor = divisor](int value) {7 return value % divisor == 0;8 }9 );

10 }1112 private:13 int divisor;14 };

Petr Zemek: C++ Traps and Pitfalls 23 / 24

Page 39: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

References and Further Information

Scott MeyersEffective Modern C++O’Reilly Media, 2014, 336 pages

Miroslav ViriusPasti a propasti jazyka C++Computer Press, 2005, 376 pages

• Stephan T. Lavavej: Don’t Help the Compiler (GoingNative’13)

• https://www.youtube.com/watch?v=AKtHxKJRwp4

• Scott Meyers: An Effective C++11/14 Sampler (GoingNative’13)

• https://www.youtube.com/watch?v=BezbcQIuCsY

• Piotr Padlewski: C++ WAT (CppCon’15)

• https://www.youtube.com/watch?v=rNNnPrMHsAA

Petr Zemek: C++ Traps and Pitfalls 24 / 24

Page 40: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Nick Lewycky’s reallochttp://blog.regehr.org/archives/767

1 #include <stdio.h>2 #include <stdlib.h>34 int main() {5 int *p = malloc(sizeof(int));6 int *q = realloc(p, sizeof(int));7 *p = 1;8 *q = 2;9 if (p == q) {

10 printf("%d %d\n", *p, *q);11 }12 return 0;13 }

$ clang -O2 -o realloc realloc.c$ ./realloc1 2

Page 41: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Nick Lewycky’s reallochttp://blog.regehr.org/archives/767

1 #include <stdio.h>2 #include <stdlib.h>34 int main() {5 int *p = malloc(sizeof(int));6 int *q = realloc(p, sizeof(int));7 *p = 1;8 *q = 2;9 if (p == q) {

10 printf("%d %d\n", *p, *q);11 }12 return 0;13 }

$ clang -O2 -o realloc realloc.c$ ./realloc1 2

Page 42: C++ Traps and Pitfalls - Petr Zemek...2016/04/22  · Petr Zemek: C++ Traps and Pitfalls 12/24 Object Construction (Riddle II) 1 auto i{1}; 2 auto j = {1}; What are the types of i

Explanation of Nick Lewycky’s realloc

Decompiled code (e.g. via retdec.com):

1 #include <stdint.h>2 #include <stdio.h>3 #include <stdlib.h>45 int main() {6 char *mem = malloc(4);7 char *mem2 = realloc(mem, 4);8 *(int32_t *)mem = 1;9 *(int32_t *)mem2 = 2;

10 if (mem == mem2) {11 printf("%d %d\n", 1, 2);12 }13 return 0;14 }