|
 |
|
 |
06-20-2004, 11:23 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
|
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
^_^;
|
|
|
06-20-2004, 07:22 PM
|
#2 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
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.
|
|
|
06-20-2004, 10:42 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
|
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.
|
|
|
06-21-2004, 02:32 AM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Did you get this code from a tutorial?
__________________
|
|
|
06-24-2004, 06:41 AM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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 08:12 AM.
|
|
|
06-24-2004, 06:50 AM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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 11:14 AM.
|
|
|
07-06-2004, 12:24 PM
|
#7 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
|
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?
|
|
|
07-07-2004, 02:48 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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?
Try it if you like a challenge  .
__________________
Last edited by Valmont; 07-08-2004 at 04:41 PM.
|
|
|
07-08-2004, 12:33 PM
|
#9 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
|
Thanks
|
|
|
10-01-2004, 12:55 PM
|
#10 (permalink)
|
|
Registered User
Join Date: Oct 2004
Location: Euless, TX, USA
Posts: 1
|
あいがとうございます 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
|
|
|
10-05-2004, 07:00 AM
|
#11 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I wonder... have you figured it out yet? 
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 07:46 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|