Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 14th, 2007, 3:06 AM   #1
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
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!
rwm is offline   Reply With Quote
Old May 14th, 2007, 4:17 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
The main problem I picked up at first glance is in your loops of the form;
   while (*p)
      *q++ = *p++;
which relies on your arrays being terminated with an element of value zero. However, you do not ensure your arrays are terminated that way.

There may be other problems as well; I stopped after encountering only one problem.
grumpy is offline   Reply With Quote
Old May 14th, 2007, 4:55 AM   #3
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
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:
rwm is offline   Reply With Quote
Old May 14th, 2007, 6:20 AM   #4
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Hint: use something like memcpy.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old May 14th, 2007, 10:22 AM   #5
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
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!
rwm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC