Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Dlaczego wycieki pamieci? - konstruktor klasy BST

Ostatnio zmodyfikowano 2015-04-11 11:57
Autor Wiadomość
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:
C/C++
#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; // oszczednosc pamieci - 1 wezel straznika
    void recursive_tree_destruction( BSTnode * );
    void add( int );
    void add( BSTnode *, BSTnode *, int );
   
    void operator +( const int );
};

Metody:
C/C++
#include "BSTree.h"

//Prywatne metody
void BSTree::setroot( BSTnode * newroot ) {
    this->root = newroot;
}

void BSTree::set_sentinel( BSTnode * newsenti ) {
    this->sentinel = newsenti;
}

void BSTree::tree_init() {
    //Czy ta funkcja stwarza luke ??
    BSTnode * newsentinel = new BSTnode();
    newsentinel->setfather( newsentinel );
    newsentinel->setleft( newsentinel );
    newsentinel->setright( newsentinel );
    this->set_sentinel( newsentinel );
    this->setroot( newsentinel );
}
/*------------------------------------------------------*/
//Metody publiczne

BSTree::BSTree() {
    this->tree_init();
}

BSTree::~BSTree() {
    this->recursive_tree_destruction( this->getroot() );
    BSTnode * helper = this->getsentinel();
    //this->set_sentinel(nullptr);
    //this->setroot(nullptr);
    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 ) {
        //Przypadek pustego drzewa
        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:
C/C++
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*);
    BSTnode( BSTnode *, BSTnode *, BSTnode *, int );
    BSTnode( BSTnode *, int );
    //BSTnode(int);
   
    int getvalue( void ) const;
    BSTnode * getfather( void ) const;
    BSTnode * getleft( void ) const;
    BSTnode * getright( void ) const;
};
Metody klasy pojedynczego wezla:
C/C++
#include "BSTnode.h"
BSTnode::BSTnode() {
    this->right = nullptr;
    this->left = nullptr;
    this->right = nullptr;
}


BSTnode::BSTnode( BSTnode * newdaddy, BSTnode * newlefty, BSTnode * newrighty, int newvalue ) {
    //constructor
    this->father = newdaddy;
    this->left = newlefty;
    this->right = newrighty;
    this->value = newvalue;
}

//Metody prywatne, modyfikujace wlasnosci obiektu
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;
}
//Metody publiczne
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;
}
//^ metody pobierania wartosci prywatnych
Pozdrawiam, shadow193
P-130482
michal11
» 2015-04-10 13:14:03
http://cpp0x.pl/artykuly/?id=4

Może pomoże, szczerze mówiąc to nie wczytywałem się w kod, masz bardzo nieczytelny styl pisania.
P-130494
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.
P-130496
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.
P-130498
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??
P-130499
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.
P-130500
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.
P-130502
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.
P-130503
« 1 » 2 3
  Strona 1 z 3 Następna strona