Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-09-2004, 04:58 PM   #1 (permalink)
Mr.Anderson
Registered User
 
Mr.Anderson's Avatar
 
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
Mr.Anderson is on a distinguished road
Send a message via AIM to Mr.Anderson Send a message via Yahoo to Mr.Anderson
Unhappy Alright, now it's time to panic.

Well, Valmont, let it never be said you didn't warn me.

I tried using templates to create a dynamically resizable array of any datatype. I'm stumped... like a tree.

Here's what I have so far.

Code:
#ifndef dvar_h
#define dvar_h

//////////////////////////////////////////////////
/* dVar.h ------------------ Work on templates. */
//////////////////////////////////////////////////

template <class dataType>
class dVar
{
private:
	dataType internalData[1];

public:
	dVar( unsigned int arraySize );
	~dVar();

	dataType getData( unsigned int valueNumber );
	void setData( unsigned int valueNumber, dataType newValue );
};

//////////////////////////////////////////////////
/* ----------- Functions for dVar.h ----------- */
//////////////////////////////////////////////////

// Name: Initializer (Constructor)
// Function: Resize array and fill it with the value defaultValue.
template <class dataType>
dVar<dataType>::dVar( unsigned int arraySize )
{
// And the nightmare begins when I unquote the following line.
//	internalData = new dataType[ arraySize ];
}

// Name: Deconstructor
// Function: Guess.
template <class dataType>
dVar<dataType>::~dVar()
{
	delete[] internalData;
}

// variable.getData( NUMBER );
// Name: getData
// Function: Returns the value of the item in position NUMBER.
template <class dataType>
dataType dVar<dataType>::getData( unsigned int valueNumber )
{
	return internalData[ valueNumber ];
}

// variable.setData( NUMBER, VALUE );
// Name: setData
// Function: Set the value of the item in position NUMBER equal to VALUE.
template <class dataType>
void dVar<dataType>::setData( unsigned int valueNumber, dataType newValue )
{
	internalData[valueNumber] = newValue;
}

#endif
Alrighty, here's what's bugging me.

Although I can create and return any data type, I can't resize the array.

I don't know what I'm going to do for irregular and custom types. (structs and classes).

I googled to find more information, but I'm kinda' at a loss as to whether this is a battle I can win.

Should I abort, retry, or BEER?

EDIT: Dah, evils and badstuffs. Should I have posted this under the other thread? It seems a bit off topic for that one.

EDIT... again: Fixed it! See below.
Code:
dataType internalData[1];
Should have been
Code:
dataType * internalData;
But still, my other question remains, how exactly would I make this compatable with abnormal data types?

E.g.
Code:
struct worker
{
   char name[16];
   unsigned int age;
};

Last edited by Mr.Anderson; 07-09-2004 at 09:19 PM.
Mr.Anderson is offline   Reply With Quote
Old 07-10-2004, 04:44 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Here is a starter. It contains all the coding skills with pointers to solve your little riddle. Now, my CArray behaves more like a (weirdo) list/stack then like an array. So your first job is to make it behave like an array.

So here is what you need to do:
  • Overload the subscript operator []. Even better, overload the () operator, but that might be slightly too unconvenient yet. Try it with [] first. The code for overloading it could be quite simple to start with: return internalData[idx] ;
  • Overload the = (assignment) operator. We want to write something like: FloatArray[3] = 2.5 ;

Once again, I gave you this crappy code on purpose, only to demonstrate the basic coding skills. It contains all techniques you need to know for now. Good luck!
Code:
#include <iostream>

using namespace std;

template <typename T> class CArray
{
public:
	CArray( ) : m_intSize(1)
	{
		//Guarantee that the array has at least one spot vacant.
		internalData = new T[1];
	}
	~CArray()
	{
		delete[] internalData;
		internalData = 0;
	}

	T getData( unsigned int idx )
	{
		return internalData[idx];
	}
	void Push( T val )
	{
		ResizeOne();
		internalData[m_intSize-1] = val;
	}
private:
	void ResizeOne()
	{
		T* temp = new T[m_intSize+1];

		for(int i = 0; i < m_intSize; ++i)
			temp[i] = internalData[i];
		delete[] internalData;
		internalData = new T[m_intSize+1];
		for(int i = 0; i < m_intSize; ++i)
			internalData[i] = temp[i];
		
		delete[] temp;
		m_intSize++;
	}

private:
	T* internalData;
	int m_intSize;
};

struct SpecialStruct
{
	//Whatever.	
};

int main()
{
	CArray<float> FloatArray;
	FloatArray.Push(2.5);
	FloatArray.Push(5.0);
	FloatArray.Push(8.73f);

	for(int i = 3; i != 0 ; --i)
		cout<<FloatArray.getData(i)<<endl;

	//Works great too.
	SpecialStruct AS, BS, CS;
	CArray<SpecialStruct> SSArray;
	SSArray.Push(AS);
	SSArray.Push(BS);
	SSArray.Push(CS);

	return 0;
}
Now if you're clever you'll take all the time in the world and read (and activly participate with) my tutorial on operator overloading in the C++ tutorial section. You will get trained in pointer handling as well, and you will be trained to know what you are doing. After study, this task is going to be a piece of cake...
__________________
Valmont is offline   Reply With Quote
Old 07-10-2004, 06:55 PM   #3 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
improvements inline:

Code:
	void ResizeOne()
	{
		T* temp = new T[m_intSize+1];
		if(temp == NULL)
		{
			// unhandled memory allocation failure
		}
		for(int i = 0; i < m_intSize; ++i)
			temp[i] = internalData[i];
		delete[] internalData;
		/*
		// this is redundant
		internalData = new T[m_intSize+1];
		for(int i = 0; i < m_intSize; ++i)
			internalData[i] = temp[i];
		
		delete[] temp;
                */
		internalData = temp;
		m_intSize++;
	}
preallocating some extra space will make this
joe_bruin is offline   Reply With Quote
Old 07-10-2004, 10:05 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Yes, I realized it later. I don't know what I was thinking. But I don't care about memory allocation failures in this stage :). But yes, your code is obviously the way to go.
Code:
void ResizeOne()
	{
		T* temp = new T[m_intSize+1];
		for(int i = 0; i < m_intSize; ++i)
			temp[ i ] = internalData[ i ];
		delete[] internalData;
		internalData = temp;	
		m_intSize++;
	}
By the way.
You checked for null pointer failure but it is tedious to code
throw bad_alloc(); after every new. So I would never check for NULL these days.
But if one has an old compiler then one should throw an exception if it runs out of memory. Do this by forcing new to throw alloc_error();. Obviously you will have to implement such a thing as: set_new_handler(theNewHandler).
Although this method doesn't come with guarantees. But I don't know a better way wich pays of at my work (time).
__________________

Last edited by Valmont; 07-10-2004 at 11:17 PM.
Valmont is offline   Reply With Quote
Old 07-11-2004, 01:02 AM   #5 (permalink)
Mr.Anderson
Registered User
 
Mr.Anderson's Avatar
 
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
Mr.Anderson is on a distinguished road
Send a message via AIM to Mr.Anderson Send a message via Yahoo to Mr.Anderson
Thumbs up Thanks.

Ah, your overloading tutorial. I've floated through it many a sleepless night. I'll keep working on it. I have to work tomorrow (3:00 AM already?) so it might be a while before I return post. I'll be back soon. Thank you, thank you, thank you to the far ends of eternity for all the help.
Mr.Anderson is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using IsDate( ) to check for a Time value shs MS Technologies ( ASP, VB, C#, .NET ) 1 09-09-2004 01:03 PM
network time synchronisation in java bossebo Java 1 06-21-2004 06:17 PM
Adjusting date() function for time zones Epsilon PHP 1 02-27-2004 03:55 AM
Assign value to a variable depending on time AOD PHP 10 02-12-2003 04:06 PM


All times are GMT -8. The time now is 05:14 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting