![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
dynamic array problem
Hey guys!
I'm trying to write a dynamic array. I was thinking of using the std STL library, but then I thought that if I write this then i'll understand better... well, i honestly can't understand why it doesnt work: #pragma once
typedef unsigned int uint;
template<typename T>
class DynamicArray {
public:
//constructors
DynamicArray() {
size = 0;
data = 0;
}
DynamicArray(const DynamicArray &src) {
if(src.data) {
size = src.size;
data = new T[size];
//copy data
T *p = &src.data[0];
T *q = &data[0];
while(*p)
*q++ = *p++;
} else {
size = 0;
data = 0;
}
}
//destructors
~DynamicArray() {
delete[] data;
}
///...
//methods
void Append(const T &t) { //append item to end
if(data) {
T *tmp = &data[0];
size++;
data = new T[size];
//copy old data
T *p = &tmp[0];
T *q = &data[0];
while(*p)
*q++ = *p++;
*q = t;
//free old mem
delete[] tmp; /// causing problems
} else {
size = 1;
data = new T(t);
}
}
void Remove() { //remove item at end
if(data) {
T *tmp = &data[0];
size--;
data = new T[size];
//copy old data
T *p = &tmp[0];
T *q = &data[0];
while(*q)
*q++ = *p++;
//free old mem
delete[] tmp;
} else
throw int(99); ///throw Exception("array is empty",__FILE__,__LINE__);
}
//data
private:
//data
uint size;
T *data;
protected:
//data
}; //class DynamicArray#include <iostream>
using namespace std;
#include "DynamicArray.h"
int main() {
DynamicArray<float> arr;
int i;
for(i=0; i<50; i++) {
cout << "appending " << i << endl;
arr.Append((float)i);
}
return 0;
}it crashes about halfway. the allocation seems very inconsistent hope you can help me out! many thanks! ![]() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
The main problem I picked up at first glance is in your loops of the form;
while (*p)
*q++ = *p++;There may be other problems as well; I stopped after encountering only one problem. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
ah of course! pretty obvious!
thanks for the pointer Grumpy! i will fix this now - bit busy at work at the moment, but when i get a chance ill fix and get back! :banana: |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Hint: use something like memcpy.
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
hey,
well i finally got a chance to work on this, its all sorted now! wow, such a stupid thing to do! thanks for the help guys! much appreciated! ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem processing file into a char array | csrocker101 | C++ | 1 | May 8th, 2007 11:50 PM |
| relation between array and pointer | n00b | C++ | 6 | Oct 12th, 2006 3:38 PM |
| string array problem | MADTech | C++ | 8 | Jul 27th, 2005 6:19 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |
| Variable array problem | Hintshigen | C | 6 | Apr 10th, 2005 2:35 PM |