2

Accelerated C++の例に従って、カスタム STL コンテナーを作成しましstd::vectorVecVec::clear()成功に勇気づけられて、ベクトルをクリアする a を追加しようとするまで、すべてがうまくいきました。

これが最新のクラス定義です(この質問に関連する部分のみ):

template <class T>
class Vec {
public:
    Vec() { create(); }

    size_type size() const { return avail - data; }
    size_type capacity() const { return limit - data; }
    void clear();

    // operators that return iterators
    iterator begin() { return data; }
    const_iterator begin() const { return data; }
    iterator end() { return avail; }
    const_iterator end() const { return avail; }

    void push_back( const T& val ) {
        if ( avail == limit ) grow();
        unchecked_append( val );
    }       
private:
    iterator data;  // points to beginning of data 
    iterator avail; // points to end of initialized data
    iterator limit; // points to end of data

    std::allocator<T> alloc;    // object to handle data allocation

    void create();
    // functions to support push_back()
    void grow();
    void unchecked_append( const T& );
};

// Creates an empty vector.
template <class T> 
void Vec<T>::create() { data = avail = limit = 0; }

// All the elements of the vector are dropped: their destructors are called, 
// and then they are removed from the vector container, 
// leaving the container with a size  of 0. 
// The capacity remains the same, however.
template <class T>
void Vec<T>::clear()
{
    std::cout << "capacity before clear: " << capacity() << std::endl;
    std::cout << "data = " << data << " limit = " << limit << std::endl;
    if (data) {
        iterator it = avail;
        // destroy objects in reverse order
        while ( it != data ) {
            alloc.destroy(--it);
        }
    }
    data = avail = 0;
    std::cout << "capacity after clear: " << capacity() << std::endl;
    std::cout << "data = " << data << " limit = " << limit << std::endl;
}

// Controls how the vector should grow if it needs more space.
template <class T>
void Vec<T>::grow()
{
    // Allocate twice as much storage as is currently used.
    // If matrix is empty, allocate one element.
    size_type new_size = std::max( 2*(limit-data), ptrdiff_t(1) );

    // Allocate new space and copy existing elements
    iterator new_data = alloc.allocate( new_size );
    iterator new_avail = std::uninitialized_copy( data, avail, new_data );

    // Deallocate old space
    uncreate();

    // Reset pointers to new values
    data = new_data;
    avail = new_avail;
    limit = data + new_size;
}

// Create space for one element at the end and put given value there.
template <class T>
void Vec<T>::unchecked_append( const T& val )
{
    alloc.construct( avail, val );
    avail++;
}

私はこれを使用してテストします

Vec<int> v;

for ( int i = 0; i < 100; i++ ) {
    v.push_back(i);
}
std::cout << "size=" << v.size() << " capacity=" << v.capacity() << std::endl;
v.clear();
std::cout << "size=" << v.size() << " capacity=" << v.capacity() << std::endl;

次の出力が得られます。

size=100 capacity=128
capacity before clear: 128
data = 0x100100280 limit = 0x100100480
capacity after clear: 1074004256
data = 0 limit = 0x100100480
size=0 capacity=1074004256

何らかの理由clear()で、limitポインターを上書きします。変更すらしていないのに、どうしてこれができるのでしょうか。コードはとてもシンプルに見えますが、何が欠けているのかわかりません。

ありがとう!

4

2 に答える 2

2

data0 に設定すると、失われます (したがって、リークします) 。クリアすると、使用可能な (割り当てられた) 要素のみが取り除かれ、バッファー ( data) は残ります。

に置き換える必要がdata = avail = 0;ありavail = data;ます。

于 2010-08-08T22:22:12.163 に答える
1

これは難しいことではありません。clear()次のいずれかを行う必要があります。

  • ベクトルを初期状態に戻します。( limitNULL に設定し、バッファ メモリを解放します。 と比較data = avail = limit = 0;してくださいdata = avail = 0;)
  • 空にしますが、縮小しないでください。(そのままにしておきますが、 ( ) にdata設定availします)dataavail = data

これらのルールは実装されていません。(最初のバージョンが悪い)そのため、容量が正しくありません。

于 2010-08-08T22:18:58.257 に答える