i have to implement a sequence class that uses a dynamic array to store the items. i have implemented
the functions following the book with a similar class. but i'm having problems with the dynamic memory.
the prof provides a program that tests the implementation and gives a grade.
whenever delete [ ] data; is activated in the destructor, resize, and overloaded operator= functions the program crashes.
if i comment out delete [ ] data; the program works but my implementation fails the last test,which tests
for possible heap leaks. so far i'm getting 54/60 pts, the last test counts for 6 pts.
i can't spot the mistake. but i think the problem is with the destructor or resize or overloaded operator= functions.
any help will be appreciated.
crash error:
[main] C:\Documents and Settings\Owner.samuellaptop\My Documents\Computer Scienc
e\212\assignment 3\a.exe 1000 (0) handle_exceptions: Exception: STATUS_ACCESS_VI
OLATION
[main] a 1000 (0) handle_exceptions: Dumping stack trace to a.exe.core
Code for test program:
http://www-cs.engr.ccny.cuny.edu/~zhu/CSc212/Assignments/seq_ex2.cxx
Header File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#ifndef MAIN_SAVITCH_SEQUENCE2_H
#define MAIN_SAVITCH_SEQUENCE2_H
#include <cstdlib> // Provides size_t
namespace main_savitch_4
{
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef size_t size_type; //typedef std::size_t size_type;
static const size_type DEFAULT_CAPACITY = 30; //enum {DEFAULT_CAPACITY = 30}; //
// CONSTRUCTOR
sequence(size_type initial_capacity = DEFAULT_CAPACITY);
sequence(const sequence& source);
~sequence( );
// MODIFICATION MEMBER FUNCTIONS
void start( );
void advance( );
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current( );
void resize(size_type new_capacity);
void operator=(const sequence& source);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const;
bool is_item( ) const;
value_type current( ) const;
private:
value_type *data;
size_type used;
size_type current_index;
size_type capacity;
};
}
#endif
|
Implementation File:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include<cassert>
#include"sequence2.h"
#include<algorithm>
namespace main_savitch_4
{
const sequence::size_type sequence::DEFAULT_CAPACITY;
sequence::sequence(size_type initial_capacity)
{
data = new value_type[initial_capacity];
capacity = initial_capacity;
used = 0;
current_index = 0;
}
sequence::sequence(const sequence& source)
{
data = new value_type[source.capacity];
capacity = source.capacity;
used = source.used;
current_index = source.current_index;
copy(source.data, source.data + used, data);
}
sequence::~sequence()
{
delete [] data;
}
void sequence::start( )
{
current_index = 0;
}
void sequence::advance( )
{
if(is_item())
current_index++;
}
void sequence::insert(const value_type& entry)
{
if(used == capacity)
resize(used+1);
if(!(is_item()))
current_index = 0;
for(size_type i = used; i > current_index; i--)
{
data[i] = data[i-1];
}
data[current_index] = entry;
++used;
}
void sequence::attach(const value_type& entry)
{
if(used == capacity)
resize(used+1);
if(current_index >= used)
{
start();
advance();
advance();
}
if(used!=0)
{
for(size_type i = used; i > current_index+1; i--)
{
data[i] = data[i-1];
}
data[current_index+1] = entry;
current_index++;
}
data[current_index] = entry;
++used;
}
void sequence::remove_current( )
{
for(size_type i = current_index; i < used; i++)
{
data[i] = data[i+1];
}
used--;
}
void sequence::resize (size_type new_capacity)
{
value_type* larger_array;
if(new_capacity == capacity)
{return;}
if(new_capacity < used)
new_capacity = used;
else
{
larger_array = new value_type[new_capacity];
delete [] data;
copy(data, data + used, larger_array);
data = larger_array;
capacity = new_capacity;
}
}
void sequence::operator = (const sequence& source)
{
value_type* new_data;
if (this == &source)
return;
if(capacity != source.capacity)
{
new_data = new value_type[source.capacity];
delete [] data;
data = new_data;
capacity = source.capacity;
}
used = source.used;
current_index = source.current_index;
copy(source.data, source.data + used, data);
}
sequence::size_type sequence::size() const
{
return used;
}
bool sequence::is_item( ) const
{
return(current_index < used);
}
sequence::value_type sequence::current() const
{
return data[current_index];
}
}
|