Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 3rd, 2006, 3:19 PM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
arrays

Is there any way to resize an array (of the same name)? Or do I "have to use a linked list"?

In java I could just do this.

 Piece tetPiece[] = new Piece [1];

 tetPiece = new Piece [x];
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 3rd, 2006, 3:26 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Arrays in C/C++ are just contiguous collections of data. The are not individual objects in the sense that they are in Java. If you want to adjust your array size, allocate it dynamically, then you may do so. If it can't be upsized, a larger area will be allocated and the contents moved. A linked list is not necessaryily a good substitute for an array. Since you're using C++, investigate the STL (vectors, deques, etc.), as these are classes designed to do what you probably need to do. Resizing and all that good hooey is taken care of for you.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 3rd, 2006, 3:38 PM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by DaWei
Arrays in C/C++ are just contiguous collections of data. The are not individual objects in the sense that they are in Java. If you want to adjust your array size, allocate it dynamically, then you may do so. If it can't be upsized, a larger area will be allocated and the contents moved. A linked list is not necessaryily a good substitute for an array. Since you're using C++, investigate the STL (vectors, deques, etc.), as these are classes designed to do what you probably need to do. Resizing and all that good hooey is taken care of for you.
Okay.. Thanks
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 3rd, 2006, 5:30 PM   #4
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>In java I could just do this.
That doesn't resize the array, it just points your reference to a completely different array. Tell me, what happens to the data in your old array now that you no longer have a reference to it? :p So I guess you can't just do that, or you'll end up writing really bad code.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Jun 3rd, 2006, 6:13 PM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,107
Rep Power: 5 lectricpharaoh will become famous soon enough
Before you pick which you want to go with, you should carefully consider how your data will be accessed, as different data structures are optimized for different sets of operations. For example, adding to (the end of) a linked list is generally done in constant time, but adding or removing elements at arbitrary positions is done in linear time, and sorting is abysmally slow. If you're not familiar with the term 'big O notation', look it up; it's a way of measuring the approximate mathematical complexity of different algorithms with respect to given operations.
Quote:
Originally Posted by Narue
That doesn't resize the array, it just points your reference to a completely different array. Tell me, what happens to the data in your old array now that you no longer have a reference to it?
Presumably, the Java GC will get around to cleaning it up at some point, after an arbitrarily long period of time has passed. I guess I'm not the only one who is frustrated at Java's lack of destructors.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jun 3rd, 2006, 6:14 PM   #6
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by lectricpharaoh
If you're not familiar with the term 'big O notation', look it up.
Yeah I'm very familiar with it, I just had a test on that topic 2 weeks ago.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 3rd, 2006, 6:30 PM   #7
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
Quote:
Presumably, the Java GC will get around to cleaning it up at some point, after an arbitrarily long period of time has passed. I guess I'm not the only one who is frustrated at Java's lack of destructors.
I think narue was commenting on the fact that the code posted dosent resize the array, rather it just creates a new one, losing any information contained in the orignal.
Animatronic is offline   Reply With Quote
Old Jun 4th, 2006, 2:08 AM   #8
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
i'd check out vectors.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja 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




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

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