forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex12_11.cpp
34 lines (31 loc) · 1.11 KB
/
ex12_11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/***************************************************************************
* @file The code is for the exercises in C++ Primmer 5th Edition
* @author Alan.W
* @date 23 DEC 2013
* @remark
***************************************************************************/
//
// Exercise 12.11:
// What would happen if we called process as follows?
// An error was generated at run time : double free or corruption.
// See the comments below.
#include <iostream>
#include <vector>
#include <string>
#include <memory>
void process(std::shared_ptr<int> ptr)
{
std::cout << "inside the process function:" << ptr.use_count() << "\n";
}
int main()
{
std::shared_ptr<int> p(new int(42));
/**
* @brief std::shared_ptr<int>(p.get()) construct a temporary shared_ptr and copy it
* to the parameter.However it is not a copy of p. As a result, at end of this
* main function p will free the memory that has been freed inside process ().
* That's why "double freed or corruption" was generated.
*/
process(std::shared_ptr<int>(p.get()));
return 0;
}