0

有很多这样的问题,但是在查看了一些案例之后,我想这个问题是针对特定案例的,所以我发布了我的代码并指出了问题发生的位置,您可以耐心阅读我的代码吗?

uniBTree.h

#ifndef uniBTree_H
#define uniBTree_H

#include "uniTreeNode.h"
#include <cassert>

template<class T>
class uniBTree {
    private:
      uniTreeNode<T> *root;
      int delete_helper(uniTreeNode<T> *);
      uniTreeNode<T> *insert_helper(uniTreeNode<T> *, const T);
      void in_print_helper(const uniTreeNode<T> *) const;
      void pre_print_helper(const uniTreeNode<T> *) const;
      void post_print_helper(const uniTreeNode<T> *) const;

    public:
      uniBTree(void);
      uniBTree(uniTreeNode<T> *r);
      ~uniBTree(void);
      void insert(const T i);
      void in_print(void) const;
      void pre_print(void) const;
      void post_print(void) const;
};

template<class T>
uniBTree<T>::uniBTree(void)
{
    root = NULL;
}

template<class T>
uniBTree<T>::uniBTree(uniTreeNode<T> *r)
{
    root = r;
}

template<class T>
int uniBTree<T>::delete_helper(uniTreeNode<T> *n)
{
    int count = 0;
    if (n == NULL)
        return 0;

    count += delete_helper(n->get_left());
    count += delete_helper(n->get_right());
    delete n;
    count++;
    return count;
}

template<class T>
uniBTree<T>::~uniBTree(void)
{
    int count = delete_helper(root);
    std::cout << "uniBTree<T>::~uniBTree<T>(void)\n";
    std::cout << count << " nodes deleted\n";
}

template<class T>
void uniBTree<T>::in_print() const
{
    in_print_helper(root);
}

template<class T>
void uniBTree<T>::pre_print() const
{
    pre_print_helper(root);
}

template<class T>
void uniBTree<T>::post_print() const
{
    post_print_helper(root);
}

template<class T>
void uniBTree<T>::in_print_helper(const uniTreeNode<T> *current) const
{
    if (current == NULL)
        return;
    in_print_helper(current->get_left());
    current->print();
    in_print_helper(current->get_right());
}

template<class T>
void uniBTree<T>::pre_print_helper(const uniTreeNode<T> *current) const
{
    if (current == NULL)
        return;
    current->print();
    pre_print_helper(current->get_left());
    pre_print_helper(current->get_right());
}

template<class T>
void uniBTree<T>::post_print_helper(const uniTreeNode<T> *current) const
{
    if (current == NULL)
        return;
    post_print_helper(current->get_left());
    post_print_helper(current->get_right());
    current->print();
}

template<class T>
void uniBTree<T>::insert(const T i)
{
    if (root == NULL)
        root = new uniTreeNode<T>(i, NULL, NULL);
    else
        insert_helper(root, i);
}

template<class T>
uniTreeNode<T> *uniBTree<T>::insert_helper(uniTreeNode<T> *current, const T i)
{
    if (current == NULL) {//this is will only dealed by attempting to visit leaves...
        //if root is null, it'll be handled in insert
        uniTreeNode<T> *child = new uniTreeNode<T>(i, NULL, NULL);
        assert(child != NULL);
        return(child);
    }
    if (i < current->get_data()) 
        current->set_left(insert_helper(current->get_left(), i));
    else 
        current->set_right(insert_helper(current->get_right(), i));
    return(current);
}

#endif

uniTreeNode.h

#ifndef uniTreeNode_H//for redefinition
#define uniTreeNode_H

#include <iostream>
//using namespace std; don't use using namespace xxx and include source file in .h file

template<typename T>
class uniTreeNode {
     private:
         T data;
         uniTreeNode<T> *left;
         uniTreeNode<T> *right;
     public:
         //uniTreeNode<T>(void);
         uniTreeNode(T d, uniTreeNode<T> *l, uniTreeNode<T> *r);
         T get_data(void) const;
         uniTreeNode<T> *get_left(void) const;
         uniTreeNode<T> *get_right(void) const;
         void set_left(uniTreeNode<T> *l);
         void set_right(uniTreeNode<T> *r);
         void print() const;
};

template<typename T>
uniTreeNode<T>::uniTreeNode/*remember syntax here*/
(T d , uniTreeNode<T> *l = NULL, uniTreeNode<T> *r = NULL)
{
     data = d;
     left = l;
     right = r;
}

template<typename T>
T uniTreeNode<T>::get_data(void) const
{
     return data;
}

template<typename T>
uniTreeNode<T> * uniTreeNode<T>::get_left(void) const
{
    return left;
}

template<typename T>
uniTreeNode<T> * uniTreeNode<T>::get_right(void) const
{
    return right;
}

template<typename T>
void uniTreeNode<T>::set_left(uniTreeNode<T> *l)
{
    left = l;
}

template<typename T>
void uniTreeNode<T>::set_right(uniTreeNode<T> *r)
{
    right = r;
}

template<typename T>
void uniTreeNode<T>::print() const
{
   std::cout << "data is " << data << std::endl;
}

#endif

日期.h

#include <ostream>
class date{
    private:
            int y;
            int m;
            int d;
    public:
            date();//default constructor
            date(const long int);//used by cplr as convert constructor
            date(int, int , int);
            friend bool operator<(const date &d1, const date &d2);//d1 is for left-hand date
            friend bool operator>(const date &d1, const date &d2);
            bool operator==(date d);
            bool operator!=(date d);
            date &operator=(date d);
            friend std::ostream &operator<<(std::ostream &out, date d);
            friend std::istream &operator>>(std::istream &in, date d);
};

日期.cc

#include <iostream>
#include <cstdio>
#include <time.h>
#include <cstring>
#include "date.h"

date::date(){
    y = m = d = 0;
}

date::date(int Y, int M, int D){
    y = Y;
    m = M;
    d = D;
}

date::date(const long int s){//#second since 1970/1/1 00:00:00
    struct tm *buf;
    buf = gmtime(&s);
    y = (buf->tm_year+1900);
    m = buf->tm_mon+1;
    d = buf->tm_mday;
}

bool operator<(const date &d1, const date &d2){
     bool result;//sizeof(bool) is 1
     if(d1.y < d2.y) result = true;
     else if(d1.y == d2.y){
           if(d1.m < d2.m) result = true;
           else if(d1.m == d2.m){
                   if(d1.d < d2.d) result = true;
                   else result = false;
           }
           else result = false;
     }
     else result = false;

     return result;
}

bool operator>(const date &d1, const date &d2){
    bool result;//sizeof(bool) is 1
    if(d1.y > d2.y) result = true;
    else if(d1.y == d2.y){
            if(d1.m > d2.m) result = true;
            else if(d1.m == d2.m){
                    if(d1.d > d2.d) result = true;
                    else result = false;
            }
            else result = false;
    }
    else result = false;

    return result;
}

bool date::operator==(date d){
    return (this->y==d.y && this->m==d.m && this->d==d.d); 
}

bool date::operator!=(date d){
    return (this->y!=d.y || this->m!=d.m || this->d!=d.d);
}

date &date::operator=(date d){
    this->y = d.y;
    this->m = d.m;
    this->d = d.d;
    return *this;
}

std::ostream &operator<<(std::ostream &out, date d){
    out << d.y << "/" << d.m << "/" << d.d << std::endl;
    return out;
}

std::istream &operator>>(std::istream &in, date d){
    in >> d.y >> d.m >> d.d ;
    return in;
}

主功能

#include "uniBTree.h"
#include "date.h"
#include <cstdio>

int main(){
    date d1 = 100000000;//convert constructor
    uniTreeNode<date> node(d1, NULL, NULL);
    printf("%p %p\n", node.get_left(), node.get_right());
    std::cout << node.get_data() << std::endl;


    date d2 = 86401;
    date d3 = 200000000;

    uniBTree<date> btree(&node);

    return 0;
}

我测试发现它&node是无效的。我认为这是因为它试图btree在程序结束时“释放”,当遇到 root 时,因为它指向node,它不能执行好事情。

我有两个问题:

  1. 如果node像我所做的那样构造一个,(uniTreeNode<date> node(xxx, xxx, xxx);)是程序“新”的对象吗?
  2. 对于uniTreeNode<T>类模板,我没有写它的析构函数!!那么,就像我上面所说的,当 root 指向的节点btree要被释放时,是否存在所谓的“默认析构函数”?它在这里被调用吗?最重要的是,程序是否使用了“DELETE”?

如果上述两个问题之一是否定的,那么问题出现的原因是什么?

编辑:现在问题出现了,但我该如何调整我的代码来解决这个问题?任何人任何想法?

编辑:只需像这样修改:

uniTreeNode<date> *nodeptr = new uniTreeNode<date>(d1, NULL, NULL);

ps 如果不是间接使用指针来引用我们的 btree 的根(因此使用 new),则不使用 new,也不应该使用 delete;通过这个选择, uniTreenode 的 delete_helper 应该使用这个:

if(n != root){
    delete n;
    count++;
}

但这并不能解决问题……最终的问题是:

"can we release object without using delete(because it isn't obtained from newing) in c++?"

回复:

我的“释放”/“分配”实际上是在说内存,没有说明它是如何完成的......但无论如何这是一个大问题

你说“你可以这样做,但它几乎总是错误的答案”;你的意思是我应该使用 DELETE 但不直接调用析构函数?(实际上这似乎根本不合适)-->请在此处证明

顺便说一句,对于那些由我新建的实例,如果我想释放它们,是否有必要通过声明删除它们?或者它们也会像那些自动变量实例一样被处理?(超出范围时返回,由编译器)->如果需要,请更正上述内容

另一个问:对于那些自动实例,是否有任何现有的语句可以用来做事,比如 DELETE 所做的事情?或者,如果我愿意,我只能调用析构函数?

4

2 回答 2

1

回答您的问题:

  1. 不,它在编译时在堆栈上分配内存,然后在上面运行构造函数。
  2. 您不能删除未使用 new 分配的指针。当对象节点在 main() 中完成时,编译器会插入对 uniTreeNode 的析构函数的调用(默认与否)。

因此推测,您不能对未使用 new 分配的指针使用 delete。

最简单的解决方法是使用 new 分配节点:

uniTreeNode<date>* node = new uniTreeNode<date>(d1);

uniBTree<date> btree(node);
于 2013-05-08T07:48:03.147 回答
0

学会使用valgrind

它会立即告诉您问题所在,您正在uniBTree析构函数中删除堆栈对象

==23648== Invalid free() / delete / delete[] / realloc()
==23648==    at 0x4A0736C: operator delete(void*) (vg_replace_malloc.c:480)
==23648==    by 0x400D78: uniBTree<date>::delete_helper(uniTreeNode<date>*) (uniBTree.h:48)
==23648==    by 0x400CD5: uniBTree<date>::~uniBTree() (uniBTree.h:56)
==23648==    by 0x400B91: main (main.cc:17)
==23648==  Address 0x7fefffab0 is on thread 1's stack
==23648== 

析构函数调用delete&node不是由new(你可以说是因为你没有写new!)

于 2013-05-08T07:48:32.120 回答