smartpointer demo
This commit is contained in:
@ -7,6 +7,7 @@
|
|||||||
#include "user/demo/LinkedListDemo.h"
|
#include "user/demo/LinkedListDemo.h"
|
||||||
#include "user/demo/PCSPKdemo.h"
|
#include "user/demo/PCSPKdemo.h"
|
||||||
#include "user/demo/PreemptiveThreadDemo.h"
|
#include "user/demo/PreemptiveThreadDemo.h"
|
||||||
|
#include "user/demo/SmartPointerDemo.h"
|
||||||
#include "user/demo/TextDemo.h"
|
#include "user/demo/TextDemo.h"
|
||||||
#include "user/demo/VBEdemo.h"
|
#include "user/demo/VBEdemo.h"
|
||||||
|
|
||||||
@ -68,9 +69,9 @@ void MainMenu::run() {
|
|||||||
case 'e':
|
case 'e':
|
||||||
choosen_demo = new ArrayDemo();
|
choosen_demo = new ArrayDemo();
|
||||||
break;
|
break;
|
||||||
// case 'r':
|
case 'r':
|
||||||
// choosen_demo = new SmartPointerDemo();
|
choosen_demo = new SmartPointerDemo();
|
||||||
// break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (choosen_demo != NULL) {
|
if (choosen_demo != NULL) {
|
||||||
|
|||||||
90
c_os/user/demo/SmartPointerDemo.cc
Normal file
90
c_os/user/demo/SmartPointerDemo.cc
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#include "user/demo/SmartPointerDemo.h"
|
||||||
|
#include "user/lib/mem/UniquePointer.h"
|
||||||
|
// #include <memory>
|
||||||
|
|
||||||
|
void SmartPointerDemo::run() {
|
||||||
|
kout.clear();
|
||||||
|
kout << "Output is written to log to be able to trace memory allocations/deallocations" << endl;
|
||||||
|
|
||||||
|
// <memory> legit smartpointer
|
||||||
|
// {
|
||||||
|
// log << INFO << "Allocating new unique_ptr<int>..." << endl;
|
||||||
|
// std::unique_ptr<int> ptr1 = std::make_unique<int>(1);
|
||||||
|
// std::unique_ptr<int> ptr2 = std::make_unique<int>(2);
|
||||||
|
|
||||||
|
// log << INFO << "Moving..." << endl;
|
||||||
|
// ptr2 = std::move(ptr1);
|
||||||
|
// log << INFO << "Leaving scope..." << endl;
|
||||||
|
// }
|
||||||
|
// log << INFO << "Should be deleted by now..." << endl;
|
||||||
|
|
||||||
|
{
|
||||||
|
log << INFO << "Allocating new unique_ptr<int>..." << endl;
|
||||||
|
bse::unique_ptr<int> ptr = bse::make_unique<int>(1);
|
||||||
|
log << INFO << "Leaving scope..." << endl;
|
||||||
|
}
|
||||||
|
log << INFO << "Should be deleted by now..." << endl;
|
||||||
|
|
||||||
|
{
|
||||||
|
log << INFO << "Allocating new unique_ptr<int>..." << endl;
|
||||||
|
bse::unique_ptr<int> ptr1 = bse::make_unique<int>(1);
|
||||||
|
bse::unique_ptr<int> ptr2;
|
||||||
|
|
||||||
|
log << INFO << "*ptr1 == " << *ptr1 << ", (bool)ptr2 == " << (bool)ptr2 << endl;
|
||||||
|
log << INFO << "Moving ptr1 => ptr2 (no allocations should happen)..." << endl;
|
||||||
|
// ptr2 = ptr1; // Doesn't work, copy assignment is deleted
|
||||||
|
ptr2 = std::move(ptr1);
|
||||||
|
log << INFO << "(bool)ptr1 == " << (bool)ptr1 << ", *ptr2 == " << *ptr2 << endl;
|
||||||
|
|
||||||
|
log << INFO << "Leaving scope..." << endl;
|
||||||
|
}
|
||||||
|
log << INFO << "Should be deleted by now..." << endl;
|
||||||
|
|
||||||
|
{
|
||||||
|
log << INFO << "Allocating (2) new unique_ptr<int>..." << endl;
|
||||||
|
bse::unique_ptr<int> ptr1 = bse::make_unique<int>(1);
|
||||||
|
bse::unique_ptr<int> ptr2 = bse::make_unique<int>(1);
|
||||||
|
|
||||||
|
log << INFO << "Moving ptr1 => ptr2 (ptr1 should be freed)..." << endl;
|
||||||
|
ptr2 = std::move(ptr1);
|
||||||
|
|
||||||
|
log << INFO << "Leaving scope..." << endl;
|
||||||
|
}
|
||||||
|
log << INFO << "Should be deleted by now..." << endl;
|
||||||
|
|
||||||
|
// =====================================================================
|
||||||
|
|
||||||
|
{
|
||||||
|
log << INFO << "Allocating new unique_ptr<int[]>..." << endl;
|
||||||
|
bse::unique_ptr<int[]> ptr = bse::make_unique<int[]>(10);
|
||||||
|
ptr[0] = 1;
|
||||||
|
log << INFO << "ptr[0] == " << ptr[0] << endl;
|
||||||
|
}
|
||||||
|
log << INFO << "Should be deleted by now..." << endl;
|
||||||
|
|
||||||
|
{
|
||||||
|
log << INFO << "Allocating new unique_ptr<int[10]>..." << endl;
|
||||||
|
bse::unique_ptr<int[]> ptr1 = bse::make_unique<int[]>(10);
|
||||||
|
bse::unique_ptr<int[]> ptr2;
|
||||||
|
|
||||||
|
log << INFO << "Moving ptr1 => ptr2 (no allocations should happen)..." << endl;
|
||||||
|
ptr2 = std::move(ptr1);
|
||||||
|
|
||||||
|
log << INFO << "Leaving scope..." << endl;
|
||||||
|
}
|
||||||
|
log << INFO << "Should be deleted by now..." << endl;
|
||||||
|
|
||||||
|
{
|
||||||
|
log << INFO << "Allocating (2) new unique_ptr<int[10]>..." << endl;
|
||||||
|
bse::unique_ptr<int> ptr1 = bse::make_unique<int>(10);
|
||||||
|
bse::unique_ptr<int> ptr2 = bse::make_unique<int>(10);
|
||||||
|
|
||||||
|
log << INFO << "Moving ptr1 => ptr2 (ptr1 should be freed)..." << endl;
|
||||||
|
ptr2 = std::move(ptr1);
|
||||||
|
|
||||||
|
log << INFO << "Leaving scope..." << endl;
|
||||||
|
}
|
||||||
|
log << INFO << "Should be deleted by now..." << endl;
|
||||||
|
|
||||||
|
scheduler.exit();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user