|
|
Back to UserFriendly Strip Comments Index
|
Strange C++ problem. | by Michiel | 2005-03-10 07:59:02 |
|
Memory bug | by ToLazyToThink | 2005-03-10 08:04:39 |
|
Ok, thanks. | by Michiel | 2005-03-10 08:17:34 |
|
If it's not super top secret | by ToLazyToThink | 2005-03-10 08:29:34 |
| It is for school. |
by Michiel |
2005-03-10 08:36:45 |
But I'm sure they won't mind if I ask for help with a bug. ;) As long as you don't design my recursive algorithm for me.
But ok. I will post the code. It must be something inside my Vec class. It's an attempt at a somewhat lighter vector class, with some special features I need. For example:
Vec<int> bla;
bla[7] = 3; // If there are no 7 places reserved, reserve them now and increase vec-size to 8
bla(7) = 3; // If there are no 7 places reserved, modulo around the vec-size
Here's the code then:
=====================================================
#ifndef VEC
#define VEC
template<class T>
class Vec {
public:
Vec();
Vec(const unsigned beginSize);
Vec(const Vec<T> &);
~Vec();
const Vec<T> & operator=(const Vec<T> &);
T & operator[](const unsigned index);
T & operator()(const unsigned index);
const unsigned size() const;
private:
T * array;
unsigned vecSize;
unsigned reserved;
// Private member functions
void changeReserve(const unsigned newReserve);
};
template<class T>
void Vec<T>::changeReserve(const unsigned newReserve) {
T * temp;
unsigned i;
reserved = newReserve;
temp = new T[reserved];
for (i = 0; i < vecSize && i < reserved; i++)
temp[i] = array[i];
delete[] array;
array = temp;
}
template<class T>
Vec<T>::Vec() {
reserved = 10;
vecSize = 0;
array = new T[reserved];
}
template<class T>
Vec<T>::Vec(unsigned beginSize) {
reserved = beginSize;
vecSize = 0;
array = new T[reserved];
}
template<class T>
Vec<T>::Vec(const Vec<T> & newVec) {
unsigned i;
vecSize = newVec.vecSize;
reserved = newVec.reserved;
array = new T[reserved];
for (i = 0; i < vecSize; i++)
array[i] = newVec.array[i];
}
template<class T>
Vec<T>::~Vec() {
delete [] array;
}
template<class T>
const Vec<T> & Vec<T>::operator=(const Vec<T> & newVec) {
unsigned i;
vecSize = newVec.vecSize;
if (reserved < vecSize) {
while (reserved < vecSize)
reserved *= 2;
changeReserve(reserved);
}
for (i = 0; i < vecSize; i++)
array[i] = newVec.array[i];
return *this;
}
template<class T>
T & Vec<T>::operator[](const unsigned index) {
if (index + 1 > vecSize) {
if (index + 1 > reserved) {
while (index + 1 > reserved)
reserved *= 2;
changeReserve(reserved);
}
vecSize = index + 1;
}
return array[index];
}
template<class T>
T & Vec<T>::operator()(const unsigned index) {
return array[index % vecSize];
}
template<class T>
const unsigned Vec<T>::size() const {
return vecSize;
}
#endif // VEC
=====================================================
Tell me if you need the code for the algorithm too.
Thanks! |
|
[ Reply ] |
|
Small correction | by Michiel | 2005-03-10 08:40:17 |
|
White space is a goodthing | by ToLazyToThink | 2005-03-10 09:05:28 |
|
I think it is, yes, because | by Michiel | 2005-03-10 09:10:53 |
|
If the new reserve is less than the vector size | by ToLazyToThink | 2005-03-10 09:15:04 |
|
Ah, yes. | by Michiel | 2005-03-10 09:17:03 |
|
Sounds like it | by ToLazyToThink | 2005-03-10 09:24:15 |
|
Yes, but I've tried all that stuff already... | by Michiel | 2005-03-10 09:27:01 |
|
What does it do when you step through? (n/t) | by ToLazyToThink | 2005-03-10 09:36:19 |
|
Strangely enough, it works just fine. | by Michiel | 2005-03-10 09:56:41 |
|
Up to you, I'm need to get my but in gear | by ToLazyToThink | 2005-03-10 09:58:35 |
|
Ok then. Here goes. (first piece) | by Michiel | 2005-03-10 10:14:01 |
|
(second piece) | by Michiel | 2005-03-10 10:14:27 |
|
|
[Todays Cartoon Discussion]
[News Index]
|
|