gpt4 book ai didi

c++ - glibc 检测到 *** ./a.out : munmap_chunk(): invalid pointer:

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:24 29 4
gpt4 key购买 nike

这样的问题很多,但是看了一些案例后,我想这个问题是特定于案例的,所以我贴出我的代码并指出问题发生的地方,你可以耐心阅读我的代码吗?

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在程序结束时遇到根时,因为它指向 node , 它不能执行好事。

我有两个问题:

  1. 如果构造一个node就像我所做的那样,( uniTreeNode<date> node(xxx, xxx, xxx); ) 对象是程序“新建”的吗?
  2. 对于 uniTreeNode<T>类模板,我没有写它的析构函数!!所以,就像我上面说的,当 node, which is pointed by root of btree ,要发布的是不是有所谓的“默认析构函数”?它在这里叫吗?最重要的是,程序是否使用了“DELETE”?

如果以上两个问题其中一个为否,是否是问题产生的原因?

编辑:现在显示了问题,但我该如何调整我的代码来解决这个问题?有人有什么想法吗?

编辑:像这样修改:

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

附注如果不是间接使用指针来引用我们的 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++?"

回复:

我的“release”/“allocated”实际上是在谈论内存,没有具体说明它是如何完成的……但无论如何这是一个大问题

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

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

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

最佳答案

回答你的问题:

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

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

最简单的修复方法是使用 new 分配节点:

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

uniBTree<date> btree(node);

关于c++ - glibc 检测到 *** ./a.out : munmap_chunk(): invalid pointer:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16435060/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com