Dynamic Array Memory Problems

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];
}




}

I can't even compile it, it's giving me this error message


sequence2.cpp: In copy constructor `main_savitch_4::sequence::sequence(const main_savitch_4::sequence&)':
sequence2.cpp:25: error: `copy' undeclared (first use this function)
sequence2.cpp:25: error: (Each undeclared identifier is reported only once for each function it appears in.)

sequence2.cpp: In member function `void main_savitch_4::sequence::resize(size_t)':
sequence2.cpp:108: error: `copy' undeclared (first use this function)
sequence2.cpp: In member function `void main_savitch_4::sequence::operator=(const main_savitch_4::sequence&)':
sequence2.cpp:134: error: `copy' undeclared (first use this function)

make.exe: *** [sequence2.o] Error 1
For some reason the algorithm copy function does not work on some compilers, for some i had to use a for loop.

Code with for loop:
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
157
158
159
160
161
#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;
	 for(size_type i=0; i < used; i++)
		data[i]=source.data[i];
    //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);
    for(size_type i=0; i < used; i++)
      larger_array[i]=data[i];
	 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);
for(size_type i=0; i < used;i++)
		new_data[i]=source.data[i];
}

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];
}




}
hey, I'm still checking but I think I found something wrong with the resize method

1
2
3
    larger_array = new value_type[new_capacity];    // allocating memory for new array
    delete [] data;                                                       // deleting old array
    copy(data, data + used, larger_array);                 // assigning deleted array values to new array? 


See, you already deleted the data array hence it's a dangling pointer and points to nothing.
then when you try to access the elements in the copy, or for loop well you'll get a buffer overflow
because data[0] to data[n] doesn't exist anymore. Hence that's why your program is crashing.
Anyways I hope that helps you in correcting the errors. ^_^
Last edited on
Topic archived. No new replies allowed.