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 06-20-2004, 10:23 AM   #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
Wink Dynamic Arrays, the 'right' way.

I'm confused, as is not so uncommon a circumstance.
I've been working on building a dynamically resizable data structure
in C++. I've heard a number of people talk about malloc as a method to resize existing arrays, but I've never been able to get it to work. I did complete one application that used (ARRAY) = new (type) with a pretty good deal of success, but I'm wondering if there's a better way.
In a sense, I'm asking about malloc vs. new and why someone would use malloc when 'new' is available.

Comments? Criticism?

If need be, I can post some code from the other application I have.

[Ed.] D'oh, and only moments after posting.
Nevermind, I found a link in another tut.
http://www.cplusplus.com/doc/tutorial/tut3-4.html
^_^;
Mr.Anderson is offline   Reply With Quote
Old 06-20-2004, 06:22 PM   #2 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
If it is not a bother, could you email me or post your full code. No matter where it comes from, learning is still learning.
cheawick is offline   Reply With Quote
Old 06-20-2004, 09:42 PM   #3 (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 Okie dokey.

No problem. I hope this helps someone.


Code:
/////////////// test.cpp ///////////////
/// Use dVars.h and see if it works. ///
////////////////////////////////////////

#include <iostream.h>
#include "dVars.h"

int main( void )
{

	dInt num( 10 );

	num.setItem( 5, 10 );

	for(int x=0; x<10; x++)
		cout << num.getItem(x) << ' ';

	cout << endl;

	num.resize( 15 );

	num.setItem( 12, 42 );

	for(x=0; x<15; x++)
		cout << num.getItem(x) << ' ';

	cout << endl;

	return 0;
}
Code:
/////////////// dVars.cpp //////////////
// The header for a resizable array. ///
////////////////////////////////////////

#ifndef dVars_h
#define dVars_h

class dInt
{
public:
	dInt( int arraySize ); // Constructor
	~dInt( void ); // Deconstructor
	void resize( unsigned int newSize ); // Returns true if the resize is successful.
	int getItem( unsigned int itemNumber ); // Returns the value of the item number specified.
	bool setItem( unsigned int itemNumber, int itemValue ); // Sets the value
private:
	unsigned int size; // Hold the size of the vector.
	int *array; // A pointer to the array.
	int *temp; // A holder for the data when the array is resized.
};

#endif
And finally...

Code:
/////////////// dVars.cpp ///////////////

#include <iostream.h>
#include <stdlib.h>
#include "dVars.h"

dInt::dInt( int arraySize )
{
	if(arraySize > 0)
	{
		size = arraySize;
		array = new int[ size ];

		for(int x=0; x<size; x++)
			array[x] = 0;
	}
}

dInt::~dInt( void )
{
// TODO: Fix this.
//	delete array;
//	delete temp;
}

int dInt::getItem( unsigned int itemNumber )
{
	if(itemNumber <= size)
		return array[ itemNumber ];
	return 0;
}

bool dInt::setItem( unsigned int itemNumber, int itemValue )
{
	if(itemNumber <= size)
	{
		array[itemNumber] = itemValue;
		return 1;
	}
	else
	{
		return 0;
	}
}

void dInt::resize( unsigned int newSize )
{
	temp = new int[ size ];

	for(unsigned int x=0; x < size; x++)
	{
		temp[x] = 0;
		temp[x] = array[x];
	}

	array = new int[ newSize ];

	for(x=0; x <= newSize; x++)
		array[x] = 0;

	for(x=0; x < size; x++)
	{
		array[x] = temp[x];
	}

	temp = new int[0];
	size = newSize;

	return;
}
I'm guessing that I could fine tune this quite a bit.
I didn't do the clean up right, and there isn't enough commenting
in the source. I'm also uncertain as to whether or not I'd even need
to make the temporary array when I resize.

Feedback and constructive criticism are appreciated.
Mr.Anderson is offline   Reply With Quote
Old 06-21-2004, 01:32 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Did you get this code from a tutorial?
__________________
Valmont is offline   Reply With Quote
Old 06-24-2004, 05:41 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
#include "dVars.h"
#include <iostream>

using namespace std;

int main()
{
	dInt num( 10 );
	num.setItem(5, 33);
	for(int i = 0; i < 10 ; ++i)
		cout<<num.getItem(i)<<endl;

	cout<<endl;

	num.resize( 15 );
	num.setItem(14, 33);
	for(int i = 0; i < 15 ; ++i)
		cout<<num.getItem(i)<<endl;

	return 0;
}

/////////////// dVars.h //////////////
// The header for a resizable array. ///
////////////////////////////////////////

#ifndef dVars_h
#define dVars_h

class dInt
{
public:
	dInt( int arraySize ); 
	~dInt( );
public:
	void resize(  int newSize ); 
	int getItem(int i)
	{
		return array[i];
	}
	void dInt::setItem(  int pos, int val )
	{
		if(pos <= size)	
			array[pos] = val;
	}

private:
	int size; 
	int *array; 
};

#endif //dVars_h

/////////////// dVars.cpp ///////////////
WTF? WHY DO I GET ERROR WHEN I TRY TO POST THE REST OF THE CODE?
CONTINUES IN NEXT POST.
__________________

Last edited by Valmont; 06-24-2004 at 07:12 AM.
Valmont is offline   Reply With Quote
Old 06-24-2004, 05:50 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
/////////////// dVars.cpp ///////////////

#include "dVars.h"
#include <iostream>

using namespace std;

dInt::dInt( int arraySize )
{
	if(arraySize > 0)
	{
		size = arraySize;
		array = new int[ size ];
		for(int i = 0; i < size ; ++i)
			array[i] = i;
	}
}

dInt::~dInt()
{
	delete[] array;
}

void dInt::resize(  int newSize )
{
	int *temp = new int[ size ];
	for( int  x = 0; x < size; ++x)
		temp[x] = array[x];

	delete[] array;
	array = new int[ newSize ];

	for(int x = 0; x < newSize; ++x)
		array[x] = 0;

	for(int x = 0; x < size; ++x)
		array[x] = temp[x];
        delete[] temp;
	size = newSize;
}
1) You used <= instead of <.
2) Always use "delete[]" instead of "delete" when allocating with "new[ ]"
3) I localized the temporary.
__________________

Last edited by Valmont; 07-12-2004 at 10:14 AM.
Valmont is offline   Reply With Quote
Old 07-06-2004, 11:24 AM   #7 (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
Answers

(I know, I'm reviving an old post, sorry.)

Nope, didn't get it from a tutorial. Just found out about new and decided to give it a try.

Thanks for the corrections.

I'm wondering if there's any way to create a dynamic char or float without rewriting the code. E.g. pass the data type and have the program use that instead of int. Am I clear enough? I don't know.

dVar blarg( float, 2 );

dVar w00t( char, 6 );

dVar foobar( long, 3 );

Any ideas?
Mr.Anderson is offline   Reply With Quote
Old 07-07-2004, 01:48 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Templates mate. Templates.
Code:
template <typename T> class MyArrayClass
{
public:
	MyArrayClass( int arraySize ); 
private:
	int size; 
	T* array; 

}
And below is how you could define a setItem method:
Code:
void dInt::setItem(  int pos, T val )
	{
		if(pos <= size)	
			array[pos] = val;
	}
And so forth. But I warn you, setting up a decent abstract array is hard .
For example, what do you have to do to make this always work?
Code:
array[pos] = val;
Try it if you like a challenge .
__________________

Last edited by Valmont; 07-08-2004 at 03:41 PM.
Valmont is offline   Reply With Quote
Old 07-08-2004, 11:33 AM   #9 (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
Talking Thanks

Muchissimas gracias. This should be fun.


ありがとございます
Mr.Anderson is offline   Reply With Quote
Old 10-01-2004, 11:55 AM   #10 (permalink)
thecyphermage
Registered User
 
Join Date: Oct 2004
Location: Euless, TX, USA
Posts: 1
thecyphermage is on a distinguished road
Send a message via AIM to thecyphermage
あいがとうございます arigatoU gozaimasu

a lot of their words do that..

"domo" is actually doUmo.. freakin silent U's.. -.-

*goes back to coding*

oh yeah.. and katakana is typically used for foreign/borrowed words.. :p
thecyphermage is offline   Reply With Quote
Old 10-05-2004, 06:00 AM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I wonder... have you figured it out yet?
__________________
Valmont 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
combining arrays Engineer Standard C, C++ 11 03-12-2005 10:23 AM
EMERGENCY: Dynamic form processing DavH27 PHP 8 10-27-2004 07:52 PM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM
dynamic select menues sde HTML, XML, Javascript, AJAX 5 02-15-2003 09:05 AM


All times are GMT -8. The time now is 11:51 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