I am studying the std::share_pointer with visual studio 2019 and I wrote a program just implements swapping two integers.
#include <iostream>
#include <memory>
void swap0(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
void swap1(std::shared_ptr<int> a, std::shared_ptr<int> b)
{
int t = *a;
*a = *b;
*b = t;
}
int main()
{
int a = 10;
int b = 20;
std::cout << a << " " << b << std::endl; // 10 20
std::shared_ptr<int> pa(&a);
std::shared_ptr<int> pb(&b);
swap1(pa, pb);
std::cout << a << " " << b << std::endl; // 10 20
}
But the program showed a dialog box titled Microsoft Visual C++ Runtime Library. Here's the information of the dialog box.
Debug Assertion Failed!
Program:
....../ConsoleApplication1.exe
File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp
Line: 904
Express: _CrtIsValidHeapPointer(block)
......
Then I tried the same code with MinGW, The program was running normally. Did I abuse the shared_ptr?