shadow193 Temat założony przez niniejszego użytkownika |
Dlaczego wycieki pamieci? - konstruktor klasy BST » 2015-04-10 11:36:42 Witam, mam pytanie odnosnie tworzenia obiektów, mianowicie : Program kompilowany jest za pomoca Visual studio, w programie wystepuja wycieki pamieci - Co w kodzie jest nie tak? Uprzejmie prosze o wyjasnienie problemu i ewentualne porady. Cialo klasy: #include "BSTnode.h" class BSTree { private: BSTnode * root; BSTnode * sentinel; void setroot( BSTnode * ); void set_sentinel( BSTnode * ); void tree_init(); public: BSTree(); ~BSTree(); BSTnode * getroot() const; BSTnode * getsentinel() const; void recursive_tree_destruction( BSTnode * ); void add( int ); void add( BSTnode *, BSTnode *, int ); void operator +( const int ); };
Metody: #include "BSTree.h"
void BSTree::setroot( BSTnode * newroot ) { this->root = newroot; }
void BSTree::set_sentinel( BSTnode * newsenti ) { this->sentinel = newsenti; }
void BSTree::tree_init() { BSTnode * newsentinel = new BSTnode(); newsentinel->setfather( newsentinel ); newsentinel->setleft( newsentinel ); newsentinel->setright( newsentinel ); this->set_sentinel( newsentinel ); this->setroot( newsentinel ); }
BSTree::BSTree() { this->tree_init(); }
BSTree::~BSTree() { this->recursive_tree_destruction( this->getroot() ); BSTnode * helper = this->getsentinel(); delete helper; }
void BSTree::recursive_tree_destruction( BSTnode * node ) { if( node != this->getsentinel() ) { this->recursive_tree_destruction( node->getleft() ); this->recursive_tree_destruction( node->getright() ); delete node; } }
BSTnode * BSTree::getroot() const { return this->root; }
BSTnode * BSTree::getsentinel() const { return this->sentinel; }
void BSTree::add( int newval ) { BSTnode * senti = this->getsentinel(); if( this->getroot() == senti ) { BSTnode * newnode = new BSTnode( senti, senti, senti, newval ); } else { this->add( this->getroot(), senti, newval ); } }
void BSTree::add( BSTnode * actnode, BSTnode * sentinel, int newval ) { if( newval < actnode->getvalue() ) { if( actnode->getleft() == sentinel ) { BSTnode * newone = new BSTnode( actnode, sentinel, sentinel, newval ); actnode->setleft( newone ); } else { this->add( actnode->getleft(), sentinel, newval ); } } else { if( newval > actnode->getvalue() ) { if( actnode->getright() == sentinel ) { BSTnode * newone = new BSTnode( actnode, sentinel, sentinel, newval ); actnode->setright( newone ); } else { this->add( actnode->getright(), sentinel, newval ); } } } }
void BSTree::operator +( const int newvalue ) { this->add( newvalue ); }
Cialo klasy pojedynczego wezla: class BSTnode { private: BSTnode * father; BSTnode * left; BSTnode * right; int value; void setfather( BSTnode * ); void setleft( BSTnode * ); void setright( BSTnode * ); void setvalue( int ); public: friend class BSTree; BSTnode(); ~BSTnode() = default; BSTnode( BSTnode *, BSTnode *, BSTnode *, int ); BSTnode( BSTnode *, int ); int getvalue( void ) const; BSTnode * getfather( void ) const; BSTnode * getleft( void ) const; BSTnode * getright( void ) const; };
Metody klasy pojedynczego wezla: #include "BSTnode.h" BSTnode::BSTnode() { this->right = nullptr; this->left = nullptr; this->right = nullptr; }
BSTnode::BSTnode( BSTnode * newdaddy, BSTnode * newlefty, BSTnode * newrighty, int newvalue ) { this->father = newdaddy; this->left = newlefty; this->right = newrighty; this->value = newvalue; }
void BSTnode::setfather( BSTnode * newdaddy ) { this->father = newdaddy; }
void BSTnode::setleft( BSTnode * newlefty ) { this->left = newlefty; }
void BSTnode::setright( BSTnode * newrighty ) { this->right = newrighty; }
void BSTnode::setvalue( int newvalue ) { this->value = newvalue; }
BSTnode * BSTnode::getfather() const { return this->father; }
BSTnode * BSTnode::getleft() const { return this->left; }
BSTnode * BSTnode::getright() const { return this->right; }
int BSTnode::getvalue() const { return this->value; }
Pozdrawiam, shadow193 |
|
michal11 |
» 2015-04-10 13:14:03 |
|
shadow193 Temat założony przez niniejszego użytkownika |
» 2015-04-10 13:29:35 Z takiego narzedzia wlasnie korzystalem stwierdzajac wycieki pamieci. Jesli chodzi o nieczytelnosc kodu, to ucze sie dopiero i cenie sobie wszelkie wskazowki, niejednokrotnie pytalem prowadzacych laboratoria z programowania jak najlepiej formatowac kod i odpowiedz byla zawsze - "Nie ma tragedii tutaj, pisz jak Ci wygodnie". Jesli ktos jest w stanie dac mi jakiegos linka do elegancko napisanego kodu ktory dobrze sie czyta bardzo chetnie przyjme. |
|
Brunon3 |
» 2015-04-10 13:33:52 1. Komentuj. 2. Nie żałuj pustych linii, które coś oddzielają (ale nie przesadzaj). 3. Dziel kod na funkcje/metody. 4. Dziel kod na pliki. 5. Nazywaj zmienne/funkcje/metody tak, by byłe same w sobie swoją dokumentacją. 6. Nazywaj zmienne/funkcje/metody tak krótko jak to możliwe, nie zapominając o podpunkcie (5). 7. Stosuj się do najbardziej znanych "powszechnych standardów"; nazywaj klasy z dużej litery, itp. 8. Ograniczaj zmienne globalne, o ile nie są potrzebne. Jeżeli są potrzebne, to z nich korzystaj. 9. Nie używaj goto. |
Takie moje "9 Przykazań Programistycznych". ;) -BD. |
|
shadow193 Temat założony przez niniejszego użytkownika |
» 2015-04-10 13:40:34 Brunon3: Moim zdaniem moj kod spelnia kazda z Twoich zasad i jedyne do czego ewentualnie mozna sie przyczepic to oszczedzanie linii, czy uwaga kolegi wyzej zatem jest sluszna i ten kod jest nieczytelny?? |
|
michal11 |
» 2015-04-10 13:40:53 Mi osobiście przeszkadza pisanie this wszędzie i dawania klamerek w tej samej linii co nazwa funkcji(java style). Mógłbyś jeszcze dać trochę enterów ale to akurat zależy od kodu. |
|
Brunon3 |
» 2015-04-10 13:44:33 Mi osobiście przeszkadza pisanie this wszędzie i dawania klamerek w tej samej linii co nazwa funkcji(java style). |
Rzeczywiście, tym this tylko sobie utrudniasz. Ale to już zależy jak kto woli. :) Mógłbyś jeszcze dać trochę enterów ale to akurat zależy od kodu. |
Również tak uważam. ;) Choć ten kod aż tak bardzo tego nie wymaga. -BD. |
|
shadow193 Temat założony przez niniejszego użytkownika |
» 2015-04-10 13:51:39 Swoja droga Panie/Panowie nie wiecie co może wywoływać te wycieki pamięci ?? Rozmawiałem z ludźmi na IRC i dowiedziałem się, że konstruktor jest po prostu źle napisany, ale szczerze nie wiem jak i dlaczego tutaj coś poprawiać - Proszę o pomoc. |
|
« 1 » 2 3 |