0

List クラスでサイズ 2 から始まる動的配列を作成しようとしています。Insert メソッドを使用して値を挿入すると、十分なスペースがあるかどうかがチェックされ、そうでない場合は配列のサイズが変更されます。サイズ + 2 ... 問題は、VS がクラッシュしていて、ヒープの破損について不平を言っていることです。また、cout が表示されないため、コピー コンストラクターが呼び出されていないと思います。

list.h ファイル:

class List
{
public:

    //  DEFAULT Constructor
    List();
    // Deconstructor to free memory allocated 
    ~List();// Prevent memory leaks

    // COPY Constructor for pointers
    List(const List& value);// copy constructor

    //Modification methods
    void Insert(const int);

    // User ACCESS methods
    void display(void) const;

private:
    int size;// MAX size of the array          
    int count;// current number of elements in the dynamic array

protected:
    int *intptr;// Our int pointer
};

list.cpp 実装ファイル:

#include "list.h" // Include our Class defintion
#include <iostream>

using namespace std;

// CONSTRUCTOR
List::List() {
    size = 2; // initial size of array
    count = 0;
    intptr = new int[size]; // Start off 2 integers sized array
}
// DECONSTRUCTOR
List::~List() {
    delete[] intptr; // free allocated memory
}

// Copy constructor

List::List(const List& value) {
    size = value.size;
    cout << "Copy con size : " << size << endl;
    count = value.count;

    cout << "Compy count : " << count << endl;
    if (count < size) {
        intptr = new int[size]; // Allocate new data
    } else {
        intptr = new int[size + 2]; // Allocate new data
    }

    for (int index = 0; index < count; index++) {
        intptr[index] = value.intptr[index];
    }

    size = size + 2;
    delete[] intptr;
    intptr = value.intptr;
}

void List::Insert(const int value) {
    // do we have room?
    if (count < size) {
        intptr[count] = value;
    } else { // if not we need to add more elements to array
        intptr[count] = value; // DEEP copy invoked with copy constructor
    }

    cout << "SIZE: " << size << endl;
    cout << "COUNT" << count << endl;
    count++; // Increase items added in array
}

void List::display() const {
    for (int i = 0; i < count; i++)
        cout << intptr[i] << endl;
}

main.cpp テスター

#include <iostream>
#include "list.h"

int main()
{
    List mylist;

    mylist.Insert(5);
    mylist.Insert(6);
    mylist.Insert(2);
    mylist.Insert(8);
    mylist.Insert(4);
    mylist.Insert(5);
    mylist.Insert(9);
    mylist.Insert(8);
    mylist.Insert(5);
    mylist.Insert(9);
    mylist.Insert(8);
    mylist.Insert(5);
    mylist.Insert(9);
    mylist.Insert(8);
    mylist.Insert(5);
    mylist.Insert(9);

    mylist.display();

    system("PAUSE");
    return 0;
}
4

4 に答える 4

8

List::Insert(const int value)メソッドはコピーコンストラクターをまったく呼び出しません。配列List内に書き込むだけです。intptrcountよりも大きくなるsizeと、配列の外側に書き込むため、エラーが発生します。

コピー コンストラクターで行うことをInsertメソッドに直接移動する必要があります。

于 2012-10-19T21:39:15.733 に答える
1

Insert()特にメソッドで、配列を正しく管理していません。代わりにこれを試してください:

#include "list.h" // Include our Class defintion 
#include <iostream> 

// CONSTRUCTOR 
List::List()  
{ 
    intptr = new int[2];
    size = 2;
    count = 0; 

    std::cout << "Initial size : " << size << " count : " << count << std::endl; 
} 

// DECONSTRUCTOR 
List::~List() 
{ 
    delete [] intptr; // free allocated memory 
} 

// Copy constructor 
List::List(const List& value) 
{ 
    intptr = new int[value.size]; // Allocate new data 
    size = value.size; 
    count = value.count; 

    for(int index = 0; index < count; ++index) 
        intptr[index] = value.intptr[index]; 

    std::cout << "Copy size : " << size << " count : " << count << std::endl; 
} 

void List::Insert(const int value) 
{ 
    if (count == size)
    { 
        int *newintptr = new int[size+2];

        for(int index = 0; index < size; ++index) 
            newintptr[index] = intptr[index]; 

        delete[] intptr;
        intptr = newintptr;
        size += 2;
    }

    intptr[count] = value; 
    ++count;

    std::cout << "New size : " << size << " count : " << count << std::endl; 
} 

void List::display() const 
{ 
    for(int i = 0; i < count; i++) 
        std::cout << intptr[i] << std::endl; 
} 

#include <iostream> 
#include "list.h" 

int main() 
{ 
    List mylist; 

    mylist.Insert(5); 
    mylist.Insert(6); 
    mylist.Insert(2); 
    mylist.Insert(8); 
    mylist.Insert(4); 
    mylist.Insert(5); 
    mylist.Insert(9); 
    mylist.Insert(8); 
    mylist.Insert(5); 
    mylist.Insert(9); 
    mylist.Insert(8); 
    mylist.Insert(5); 
    mylist.Insert(9); 
    mylist.Insert(8); 
    mylist.Insert(5); 
    mylist.Insert(9); 

    mylist.display(); 
    system("PAUSE"); 

    List mylist2(myList); // copy construct a new list

    mylist2.display(); 
    system("PAUSE"); 

    return 0; 
} 

そうは言っても、std::vector代わりに実際に使用する必要があります。例:

#include <iostream>            
#include <vector>
#include <algorithm>

void displayValue(int value)
{
    std::cout << value << std::endl; 
}

int main()            
{            
    std::vector<int> mylist;            

    mylist.push_back(5);            
    mylist.push_back(6);            
    mylist.push_back(2);            
    mylist.push_back(8);            
    mylist.push_back(4);            
    mylist.push_back(5);            
    mylist.push_back(9);            
    mylist.push_back(8);            
    mylist.push_back(5);            
    mylist.push_back(9);            
    mylist.push_back(8);            
    mylist.push_back(5);            
    mylist.push_back(9);            
    mylist.push_back(8);            
    mylist.push_back(5);            
    mylist.push_back(9);            

    std::for_each(mylist.begin(), myList.end(), displayValue);
    system("PAUSE");            

    std::vector<int> myList2(myList);

    std::for_each(mylist2.begin(), myList2.end(), displayValue);
    system("PAUSE");            

    return 0;            
 }       

さらに一歩進んで、カスタムListクラスを使い続けたい場合は、少なくともstd::vectorその内部で使用してください。

#include <vector>

class List  
{  
public:  
    //  DEFAULT Constructor  
    List();  

    //Modification methods  
    void Insert(const int);  

    // User ACCESS methods  
    void display(void) const;  

protected:  
    std::vector<int> intvec;
};

#include "list.h" // Include our Class defintion 
#include <iostream> 

// CONSTRUCTOR 
List::List()  
{ 
    intvec.reserve(2);
    std::cout << "Initial size : " << intvec.capacity() << " count : " << intvec.size() << std::endl; 
} 

// Copy constructor 
List::List(const List& value) 
{ 
    intvec = value.intvec;
    std::cout << "Copy size : " << invec.capacity() << " count : " << intvec.size() << std::endl; 
} 

void List::Insert(const int value) 
{ 
    intvec.push_back(value); 
    std::cout << "New size : " << intvec.capacity() << " count : " << intvec.size() << std::endl; 
} 

void List::display() const 
{ 
    for(std::vector<int>::const_iterator iter = intvec.begin(), end = intvec.end(); iter != end; ++iter) 
        std::cout << *iter << std::endl; 
} 

#include <iostream> 
#include "list.h" 

int main() 
{ 
    List mylist; 

    mylist.Insert(5); 
    mylist.Insert(6); 
    mylist.Insert(2); 
    mylist.Insert(8); 
    mylist.Insert(4); 
    mylist.Insert(5); 
    mylist.Insert(9); 
    mylist.Insert(8); 
    mylist.Insert(5); 
    mylist.Insert(9); 
    mylist.Insert(8); 
    mylist.Insert(5); 
    mylist.Insert(9); 
    mylist.Insert(8); 
    mylist.Insert(5); 
    mylist.Insert(9); 

    mylist.display(); 
    system("PAUSE"); 

    List mylist2(myList); // copy construct a new list

    mylist2.display(); 
    system("PAUSE"); 

    return 0; 
} 
于 2012-10-19T21:53:36.143 に答える
-1

std::vector を使用します。これはすべて既に実行されており、コードよりも安全かつ高速に実行されます。

于 2012-10-19T21:38:37.627 に答える