Witam, cwicze działanie wskaznikow unique oraz shared w C++.
Bawie sie na takiej oto liscie dwukierunkowej.
Pierwszy program to lista 1 kierunkowa, analizuje go valgrindem i wszystko jest ok (shared_ptr sprzątają po sobie) :
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace std;
class EmptyListError : public std::runtime_error
{
public :
EmptyListError(std::string msg) :
runtime_error(msg)
{}
};
class NotFoundError : public std::runtime_error
{
public :
NotFoundError(std::string msg) :
runtime_error(msg)
{}
};
class Node
{
public:
Node(const int v) :
next(nullptr),
value(v)
{}
std::shared_ptr<Node> next;
int value;
};
class List
{
public:
List();
void add(shared_ptr<Node> node); // dodaje element na koniec listy
shared_ptr<Node> get(const int value); // zwraca element o wskazanej wartości
private:
shared_ptr<Node> first;
};
List::List() :
first(nullptr)
{}
void List::add(shared_ptr<Node> node)
{
if(node->next)
{
cerr<< "Nie można dodać ponownie tego samego węzła" << endl;
return;
}
if(!first)
{
first = node;
}
else
{
shared_ptr<Node> current = first;
while(current->next)
{
current = current->next;
}
current->next = node;
}
}
shared_ptr<Node> List::get(const int value)
{
if(!first)
{
throw EmptyListError("Lista jest pusta !");
}
else
{
shared_ptr<Node> current = first;
do
{
if(current->value == value)
{
cout << "Found value " << current->value << endl;
return current;
}
else
{
cout << "Going through " << current->value << endl;
current = current->next;
}
} while(current);
throw NotFoundError("Not found");
//cout << "Not found: value " << value << endl;
return nullptr;
}
}
int main()
{
List lista;
auto node4 = make_shared<Node>(4);
auto node7 = make_shared<Node>(7);
lista.add(node4);
lista.add(make_shared<Node>(2));
lista.add(node4);
lista.add(make_shared<Node>(9));
lista.add(node7);
try {
auto node= lista.get(11);
}
catch(EmptyListError & e) {
cout << e.what() <<endl;
}
catch(NotFoundError & e){
cout<<e.what()<<endl;
}
return 0;
}
Teraz przerobiłem tą liste na dwukierunkową, kod ponizej. Pytanie co jest nie tak tutaj, bo wg valgrinda : total heap usage: 1 allocs, 0 frees, 64 bytes allocated, czyli coś nie sprząta po sobie, pytanie dlaczego. ?
KOD :
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace std;
class EmptyListError : public std::runtime_error
{
public :
EmptyListError(std::string msg) :
runtime_error(msg)
{}
};
class NotFoundError : public std::runtime_error
{
public :
NotFoundError(std::string msg) :
runtime_error(msg)
{}
};
class Node
{
public:
Node(const int v) :
next(nullptr),
prev(nullptr),
value(v)
{}
std::shared_ptr<Node> next;
std::shared_ptr<Node> prev;
int value;
};
class List
{
public:
List();
void add(shared_ptr<Node> node); // dodaje element na koniec listy
shared_ptr<Node> get(const int value); // zwraca element o wskazanej wartości
private:
shared_ptr<Node> first;
};
List::List() :
first(nullptr)
{}
void List::add(shared_ptr<Node> node)
{
if(node->next)
{
cerr<< "Nie można dodać ponownie tego samego węzła" << endl;
return;
}
if(!first)
{
first = node;
node->prev=first;
}
else
{
shared_ptr<Node> current = first;
while(current->next)
{
current = current->next;
}
current->next = node;
node->prev=current;
}
}
shared_ptr<Node> List::get(const int value)
{
if(!first)
{
throw EmptyListError("Lista jest pusta !");
}
else
{
shared_ptr<Node> current = first;
do
{
if(current->value == value)
{
cout << "Found value " << current->value << endl;
return current;
}
else
{
cout << "Going through " << current->value << endl;
current = current->next;
}
} while(current);
throw NotFoundError("Not found");
//cout << "Not found: value " << value << endl;
return nullptr;
}
}
int main()
{
List lista;
auto node4 = make_shared<Node>(4);
lista.add(node4);
return 0;
}