массивы — динамический список целых чисел в переполнении стека

Я пытаюсь создать динамический массив в своем классе 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;
}

0

Решение

Вы не управляете массивом правильно, особенно в вашем 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;
}
1

Другие решения

Ваш List::Insert(const int value) метод не вызывает List Копировать конструктор вообще, он пишет только внутри intptr массив. когда count становится больше, чем sizeвы пишете вне массива, и поэтому у вас есть ошибки.

Вы должны переместить то, что вы делаете в конструкторе копирования, в Insert метод напрямую.

8

Причина для конструктора копирования заключается в создании нового объекта из существующего, но посмотрите на себя, конструктор копирования, что вы там делаете?

/* initialize my size and count, from value */
size = value.size;
count = value.count;

/* Check count and size */
if( count == size ) /* if other is full */
size += 2;

/* copy content from value.intptr into this->intptr */
//if (count < size)
//    intptr = new int[size]; // Allocate new data
//else
//    intptr = new int[size + 2]; // Allocate new data
intptr = new int[size];  /* Allocate my buffer */

/* It's better to use std::copy in place of a hand written loop */
//for(int index = 0; index < count; index++)
//    intptr[index] = value.intptr[index];
std::copy( value.intptr, value.intptr + value.count, intptr );

/* why you increase your size here?? shouldn't this indicate size of intptr? */
//size = size + 2;

/* After creating a new buffer and putting data into it, you destroy the buffer
and set your buffer equal to buffer of value? why? if value destroyed it will
destroy its intptr and your intptr point to a deleted memory
*/
//delete [] intptr;
// intptr = value.intptr;

Теперь посмотри на свой insert метод:

if(count < size) // do we have room?
{
intptr[count] = value;
}
else // if not we need to add more elements to array
{
/* As you already checked you do not have enough room to insert data to intptr
so why you do it here? shouldn't you first allocate a new buffer and then
copy data to it?
In comment you say DEEP copy with copy-constructor, which copy constructor
you expect to called here? you are assigning an int to another int, so where
is copy constructor?
*/
// intptr[count] = value; // DEEP copy invoked with copy constructor
int* tmp = new int[size + 2];
std::copy( intptr, intptr + size, tmp );
delete[] intptr;
intptr = tmp;
size += 2;
intptr[count] = value;
}

count++; // Increase items added in array
1

Используйте std :: vector. Он уже делает все это, и делает это безопаснее и быстрее, чем ваш код.

-1
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector