Question about allocate memory!!

Hi, I have question on allocating dynamic memory for my sequence project!!
The header file is this.
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
#include <cstdlib>  // Provides size_t

namespace main_savitch_4
{
    class sequence
    {
    public:
        // TYPEDEFS and MEMBER CONSTANTS
        typedef double value_type;
        typedef std::size_t size_type;
        static const size_type DEFAULT_CAPACITY = 30;
        // CONSTRUCTORS and DESTRUCTOR
        sequence(size_type initial_capacity = DEFAULT_CAPACITY);
        sequence(const sequence& source);
		~sequence( );
        // MODIFICATION MEMBER FUNCTIONS
		void resize(size_type new_capacity);
        void start( );
        void advance( );
        void insert(const value_type& entry);
        void attach(const value_type& entry);
        void remove_current( );
        void operator =(const sequence& source);
        // CONSTANT MEMBER FUNCTIONS
        size_type size( ) const;
        bool is_item( ) const;
        void show();
        value_type current( ) const;
    private:
        value_type* data;
        size_type used;
        size_type current_index;
		size_type capacity;
    };
}


And the implementation file is this!!
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
#include <iostream>
#include <cassert>
#include <string.h>
#include <algorithm>
#include "sequence2.h"

using namespace std;
using std::cout;
using std::endl;

namespace main_savitch_4{
	//Constructors
	sequence::sequence(size_type initial_capacity){
		capacity = initial_capacity;
		used = 0;
		current_index = 0;
		data = new value_type[initial_capacity];
	}

	sequence::sequence(const sequence& source){
		capacity = source.capacity;
		used = source.used;
		current_index = source.current_index;
		data = new value_type[source.capacity];
		for(int i = 0; i < used; i++){
			data[i] = source.data[i];
		}
	}


	void sequence::operator =(const sequence& source){
		if(this == &source) return;
		value_type* new_data = new value_type[source.capacity];
		delete[] data;
		copy(source.data, source.data+used, new_data);
		used = source.used;
		capacity = source.capacity;
		data = new_data;
	}


	//Destructor
	sequence::~sequence(){
		delete[] data;
	}


	void sequence::insert(const value_type& entry){
		assert(used < capacity);
		if(used == 0){
			data[0] = entry;
			used++;
			//cout<<"data = " <<data[current_index]<<"used = "<<used<<endl;
		}
		else {
			for(int index = used; index > current_index; index--){
					data[index] = data[index-1];

			}
			data[current_index] = entry;
			used++;
		}
	}

	void sequence::attach(const value_type& entry){
		//assert(used < capacity);
		if(!(is_item())){
			data[current_index] = entry;
			used++;
		}
		else {
			for(int i = used; i > current_index+1; i--){
				data[i] = data[i-1];
			}
			++current_index;
			data[current_index] = entry;
			used++;
		}

	}

	void sequence::remove_current(){
		assert(is_item());
		//cout<<"index removing == " << current_index << endl;
		if(is_item()) {
			for(int i = current_index; i < used-1; i++){
				data[i] = data[i+1];
			}
			used--;
		}
		return;
	}
	bool sequence::is_item() const{
		return (current_index < used);
	}

	void sequence::resize(size_type new_capacity){
		//assert(new_capacity > used);
		if(capacity == new_capacity) return;
		if(new_capacity < used)
			return;
		value_type* temp;
		temp = new value_type[capacity];
		copy(data, data+used, temp);
		delete[] data;
		data = temp;
	}

	void sequence::start(){
		if(used == 0) return;
		current_index = 0;
		//cout<<"current_index == " << current_index<<endl;
		return;
	}

	void sequence::advance(){
		if(used == 0) return;
		current_index++;
	}

	void sequence::show(){
		cout<<"--------show function -----------"<<endl;
		for(int i = 0; i < used; i++){
			cout<<"data["<<i<<"] = " << data[i]<<endl;
		}
		cout<<"current_index is " << current_index << endl;
	}

	sequence::value_type sequence::current() const {
		assert(is_item());
		return data[current_index];
	}

	sequence::size_type sequence::size() const{
		return used;
	}
}


For this project I have a test file to test my program..it is such a long code so I will just post where I get trouble!!
1
2
3
4
5
6
7
8
9
10
int test5( )
{
    sequence original; // A sequence that we'll copy.
    double items[2*original.DEFAULT_CAPACITY];
    size_t i;
    .
    .
    .
    
}

I have tried to debug it and I found that the problem was when the program try to allocate dynamic memory for
sequence original;
in the implementation file!! I have no idea why this keep having error. This test code is fifth test, which means that there were four other test codes before this!!
All tests passed before this. I debugged this test code and found when the constructor tried to allocate memory it gives me error!!
Anyone have comment on this??
thanks
I have tried to debug it and I found that the problem was when the program try to allocate dynamic memory for
sequence original;
in the implementation file!!

How did you find the problem was that statement?


I debugged this test code and found when the constructor tried to allocate memory it gives me error!!

What error?
I'll just put my 2 cents in:

Is it to do with your namespace ? Do you have a using main_savitch_4; or even better, refer to sequence with main_savitch_4::sequence

I would have a short name for a namespace - like sav, so you can do sav::sequence

If I am wrong I will leave you in the vastly superior hands of cire :)
reply to cire!!
I am using eclipse to write code and it has the debug feature!! I used it!
and the program keep crashed at test 5 so I made a break point there and see what happened..and I noticed that the program stops and gives me error massage that say
[New Thread 4448.0x1080]
warning: HEAP[sequence.exe]:
warning: Heap block at 00A41CC0 modified at 00A41DB8 past requested size of f0
after test 4 when the destructor deletes the object.

For TheIdeasMan!!
The name space is not mine!!! I thought that name main_savitch is too long but this is what I got from the text book..!!
after test 4 when the destructor deletes the object.

So it's very likely that the problem occurs in test 4 and not test 5, since the line in test 5 that you fingered doesn't actually modify any memory on the heap.

After looking over your code, I suspect it's related to current_index as the invariant that makes it's use possible (it should always be less than or equal to used) is not guaranteed. It is not updated in operator=.
Topic archived. No new replies allowed.