如何删除由指针数组组成的成员变量 下午好,我试图删除一个由指针数组组成的C++类成员变量。以下是代码的摘录: class NFA { public: dict<tuple2<__ss_int, __ss_int> *, dict<str *, __shedskin__::set<tuple2<__ss_int, __ss_int> *> *> *> *transitions; auto_vector< dict<str *, set<tuple2<__ss_int, __ss_int> *> *> > *tmpautovector; NFA() {} ~NFA() { delete this->tmpautovector NFA(tuple2<__ss_int, __ss_int> *start_state) { this->__class__ = cl_NFA; __init__(start_state); } void *add_transition(tuple2<__ss_int, __ss_int> *src, str *input, tuple2<__ss_int, __ss_int> *dest); } void *NFA::add_transition(tuple2<__ss_int, __ss_int> *src, str *input, tuple2<__ss_int, __ss_int> *dest) { dict<str *, set<tuple2<__ss_int, __ss_int> *> *> *tmpdict; if ((!(this->transitions)->__contains__(src))) { tmpdict = new dict<str *, set<tuple2<__ss_int, __ss_int> *> *>(); this->transitions->__setitem__(src, tmpdict); tmpautovector->push_back(tmpdict); } return NULL; } NFA类{ 公众: dict*转换; 自动向量*tmpautovector; NFA(){} ~NFA(){删除此->tmpautovector NFA(tuple2*启动状态){ 这->\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu; __初始化(启动状态); } void*add_转换(tuple2*src,str*input,tuple2*dest); } void*NFA::add_转换(tuple2*src、str*input、tuple2*dest){ dict*tmpdict; 如果((!(此->转换)->\uuuuu包含(src))){ tmpdict=新的dict(); 这->转换->设置项(src、tmpdict); tmpautovector->推回(tmpdict); } 返回NULL; }

如何删除由指针数组组成的成员变量 下午好,我试图删除一个由指针数组组成的C++类成员变量。以下是代码的摘录: class NFA { public: dict<tuple2<__ss_int, __ss_int> *, dict<str *, __shedskin__::set<tuple2<__ss_int, __ss_int> *> *> *> *transitions; auto_vector< dict<str *, set<tuple2<__ss_int, __ss_int> *> *> > *tmpautovector; NFA() {} ~NFA() { delete this->tmpautovector NFA(tuple2<__ss_int, __ss_int> *start_state) { this->__class__ = cl_NFA; __init__(start_state); } void *add_transition(tuple2<__ss_int, __ss_int> *src, str *input, tuple2<__ss_int, __ss_int> *dest); } void *NFA::add_transition(tuple2<__ss_int, __ss_int> *src, str *input, tuple2<__ss_int, __ss_int> *dest) { dict<str *, set<tuple2<__ss_int, __ss_int> *> *> *tmpdict; if ((!(this->transitions)->__contains__(src))) { tmpdict = new dict<str *, set<tuple2<__ss_int, __ss_int> *> *>(); this->transitions->__setitem__(src, tmpdict); tmpautovector->push_back(tmpdict); } return NULL; } NFA类{ 公众: dict*转换; 自动向量*tmpautovector; NFA(){} ~NFA(){删除此->tmpautovector NFA(tuple2*启动状态){ 这->\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu; __初始化(启动状态); } void*add_转换(tuple2*src,str*input,tuple2*dest); } void*NFA::add_转换(tuple2*src、str*input、tuple2*dest){ dict*tmpdict; 如果((!(此->转换)->\uuuuu包含(src))){ tmpdict=新的dict(); 这->转换->设置项(src、tmpdict); tmpautovector->推回(tmpdict); } 返回NULL; },c++,gcc,memory-leaks,C++,Gcc,Memory Leaks,我正在尝试使用auto_vector类来存储指针数组。以下是auto_vector类的一些函数。该类的文档说明这不是auto_ptr的STL向量。强烈反对使用STL向量来存储auto_ptr>当我访问auto_vecto时,我总是遇到分段错误r::operator member function>下面显示的GDB堆栈跟踪显示了分段错误。我想知道是否有可能修复此分段错误,或者是否有更好的方法删除由指针数组组成的成员变量。谢谢 // auto_vector.h // This file is (C)

我正在尝试使用auto_vector类来存储指针数组。以下是auto_vector类的一些函数。该类的文档说明这不是auto_ptr的STL向量。强烈反对使用STL向量来存储auto_ptr>当我访问auto_vecto时,我总是遇到分段错误r::operator member function>下面显示的GDB堆栈跟踪显示了分段错误。我想知道是否有可能修复此分段错误,或者是否有更好的方法删除由指针数组组成的成员变量。谢谢

// auto_vector.h
// This file is (C) 2002-2004 Royce Mitchell III
// and released under the LGPL & BSD licenses

#ifndef AUTO_VECTOR_H
#define AUTO_VECTOR_H

#include <sys/types.h>
#include <memory>




template<class T>
class auto_vector
{
public:
    explicit auto_vector ( size_t capacity = 0 )
        : _arr(0), _capacity(0), _end(0)
    {
        if ( capacity != 0 )
            _arr =  new std::auto_ptr<T>[capacity];
        _capacity = capacity;
    }

    ~auto_vector()
    {
        delete []_arr;
    }

    size_t size() const
    {
        return _end;
    }

    const std::auto_ptr<T>& operator [] ( size_t i ) 
        {
        return _arr[i];
    }

    std::auto_ptr<T>& operator [] ( size_t i )
        {
        return _arr[i];
    }

        void push_back ( std::auto_ptr<T>& p )
    {
        reserve ( _end + 1 );
        _arr[_end++] = p;
    }

        void push_back ( T * p )
    {
        reserve ( _end + 1 );
        std::auto_ptr<T> tmp(p);
        _arr[_end++] = tmp;
        //GCC is pedantic, this is an error.
        //_arr[_end++] = auto_ptr<T>(p);
    }

    void reserve ( size_t reqCapacity )
    {
        if ( reqCapacity <= _capacity )
            return;
        size_t newCapacity = 2 * _capacity;
        if ( reqCapacity > newCapacity )
            newCapacity = reqCapacity;
        // allocate new array
        std::auto_ptr<T> * arrNew = new std::auto_ptr<T> [newCapacity];
        // transfer all entries
        for ( size_t i = 0; i < _capacity; ++i )
            arrNew[i] = _arr[i];
        _capacity = newCapacity;
        // free old memory
        delete[] _arr;
        // substitute new array for old array
        _arr = arrNew;
    }

private:
    std::auto_ptr<T>  *_arr;
    size_t        _capacity;
    size_t        _end;
};

#endif//AUTO_VECTOR_H

Program received signal SIGSEGV, Segmentation fault.
0x0806b85a in std::auto_ptr<__shedskin__::setentry<__shedskin__::tuple2<int, int>*> >::get (this=0x0)
    at /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/memory:300
300           get() const throw() { return _M_ptr; }
(gdb) bt
#0  0x0806b85a in std::auto_ptr<__shedskin__::setentry<__shedskin__::tuple2<int, int>*> >::get (this=0x0)
    at /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/memory:300
#1  0x0806be61 in __shedskin__::set<__shedskin__::tuple2<int, int>*>::lookup (this=0x80a3c58, key=0x80a3c40, 
    hash=-1919631394) at ../Include/builtin.hpp:3223
#2  0x0806bfc7 in __shedskin__::set<__shedskin__::tuple2<int, int>*>::insert_key (this=0x80a3c58, key=0x80a3c40, 
    hash=-1919631394) at ../Include/builtin.hpp:3258
#3  0x080728ea in __shedskin__::set<__shedskin__::tuple2<int, int>*>::add (this=0x80a3c58, key=0x80a3c40)
    at ../Include/builtin.hpp:3278
#4  0x0806af0c in main (Argc_=<value optimized out>, Argv_=<value optimized out>) at ../Source/mdMatchupTest.cpp:93
(gdb) frame 1
#1  0x0806be61 in __shedskin__::set<__shedskin__::tuple2<int, int>*>::lookup (this=0x80a3c58, key=0x80a3c40, 
    hash=-1919631394) at ../Include/builtin.hpp:3223
3223        setentry<T>* entry = table->operator[](i)->get(); // &table[i]
//auto_vector.h
//本文件为(C)2002-2004罗伊斯·米切尔三世
//并根据LGPL和BSD许可证发布
#ifndef自动向量
#定义自动向量
#包括
#包括
模板
类自动向量
{
公众:
显式自动向量(大小容量=0)
:_arr(0),_capacity(0),_end(0)
{
如果(容量!=0)
_arr=新标准::自动ptr[容量];
_容量=容量;
}
~auto_vector()
{
删除[]_arr;
}
大小\u t大小()常量
{
返回端;
}
常数标准::自动ptr和操作员[](尺寸)
{
返回_arr[i];
}
标准::自动ptr和操作员[](尺寸i)
{
返回_arr[i];
}
无效回推(标准::自动回推&p)
{
储备(_end+1);
_arr[_end++]=p;
}
无效推回(T*p)
{
储备(_end+1);
标准:自动测试tmp(p);
_arr[_end++]=tmp;
//GCC是迂腐的,这是一个错误。
//_arr[_end++]=auto_ptr(p);
}
空隙预留量(尺寸和所需容量)
{
if(需求容量新容量)
新容量=新容量;
//分配新阵列
std::auto_ptr*arrNew=new std::auto_ptr[newCapacity];
//转移所有条目
用于(尺寸i=0;i<\u容量;++i)
arrNew[i]=_arr[i];
_容量=新容量;
//释放旧内存
删除[]_arr;
//用新数组替换旧数组
_arr=arrNew;
}
私人:
std::自动ptr*\U arr;
容量大小;
端部尺寸;
};
#endif//AUTO_VECTOR_H
程序接收信号SIGSEGV,分段故障。
std::auto_ptr::get中的0x0806b85a(此值=0x0)
位于/usr/lib/gcc/i386 redhat linux/4.1.2/../../../../../../../include/c++/4.1.2/内存:300
300 get()常量throw(){return\M\u ptr;}
(gdb)英国电信
#std::auto_ptr::get中的0 0x0806b85a(此=0x0)
位于/usr/lib/gcc/i386 redhat linux/4.1.2/../../../../../../../include/c++/4.1.2/内存:300
#1 0x0806be61位于舍德斯金的集合::查找中(此=0x80a3c58,键=0x80a3c40,
hash=-1919631394)位于../Include/builtin.hpp:3223
#2 0x0806bfc7 in_uuuShedskin_uuuuuu::set::insert_u键(此键=0x80a3c58,键=0x80a3c40,
hash=-1919631394)位于../Include/builtin.hpp:3258
#3 0x080728ea在谢德斯金的集合中::添加(此=0x80a3c58,键=0x80a3c40)
位于../Include/builtin.hpp:3278
#在../Source/mdMatchupTest.cpp:93处的main(Argc_=,Argv_=)中有4个0x0806af0c
(gdb)第1帧
#1 0x0806be61位于舍德斯金的集合::查找中(此=0x80a3c58,键=0x80a3c40,
hash=-1919631394)位于../Include/builtin.hpp:3223
3223 setentry*entry=table->operator[](i)->get();//&table[i]

<代码> > NFA和 AudioValue<代码>这里的作者有一个错误。为什么成员指针?你应该利用RAII的优势,这正是C++优于其他语言的地方!你可以保证当一个有效的自动对象超出范围时,它的析构函数将被调用。,无论堆栈是否因异常而倒转,或堆栈是否完全展开。请利用此保证

对于初学者,扔掉
aut_vector
类。你不需要它。使用
std::vector
。作为自动成员,当然不是指针成员。然后移除析构函数;你不需要它


在这里,您消除了一个完整的类和析构函数。如果您有一个复制构造函数,您应该有一个赋值运算符,您也应该有一个赋值运算符,还有一个交换成员,这是一个很好的主意,那么您也可以消除这些。这就消除了大量代码,并获得了大量正确性“两个鸟,一个石头。< /P> < P>我有一个感觉,代码< NFA< /Cord>和<代码> AutoValpIs<代码>这里有点错误。为什么是成员指针?你应该利用RAII,这正是C++优于其他语言的地方!你可以保证当一个有效的自动对象超出其范围的DE。将调用structor,无论堆栈是否因异常而倒带,或堆栈是否完全展开。请利用此保证

I have solved the segmentation fault problem. It occured when memory allocated with malloc was deallocated with delete, thereby corrupting the heap. I replaced all the malloc's with the new operator. The reason the malloc was mismatched with delete was because the python to C++ converter program used the Boehm garbage collector. We are taking out the Boehm garbage collector and replacing it with new and delete. The Boehm garbage collector has two disadvantages, First, it pauses at random  times during garbage collection. Second, it has a finite amount of heap memory which can only be changed at compile time. As a result, it has scalability issues with large applications.
The auto_vector class runs Okay. I tested it with Valgrind and there were no major problems. The memory leak has been addressed. Thank you for all the Boost suggestions. 
对于初学者,扔掉
aut_vector
类。你不需要它。使用
std::vector
。作为自动成员,当然不是指针成员。然后移除析构函数;你不需要它

在这里,您消除了一个完整的类和析构函数