Welcome! Log In Create A New Profile

Advanced

C++ question [SOLVED!!!]

Posted by Arikado 
C++ question [SOLVED!!!]
June 25, 2009 08:30PM
Is there any way I can declare an array, and than change the size of it during runtime? For example:

int MaxNumber = 1;
int NumberArray[MaxNumber];

...

int main(){

...

std::cin >> MaxNumber;

...

NumberArray[4] = 16;

}

I'm getting tons of compilation errors trying to do something similar like the above code.



Edited 2 time(s). Last edit at 06/26/2009 09:32PM by Arikado.
Re: C++ question
June 25, 2009 09:29PM
As far as I know, no.

I think the following is the reason:

When an array is declared. The variable you declared it to holds a memory adress (example: 45FF19) and memory is allocated from that adress to "the adress + sizeof(int)", if the array is of type int.
If you, after declaring the array, declare something else, it might allocate memory direclty after the end of the allocated memory from the array.
If you then would redeclare the size of the array. The data after the end of the allocated memory for the array would be overwritten.

You could make your own linked list to solve this problem (A linked list consists, as you perhaps know, of one object containing 2 member variables; one the holds the actual data and one the holds a reference to another identical object. The objects have methods for finding the data at a specific index and adding and removing data objects).
Re: C++ question
June 25, 2009 09:30PM
You can use dynamic arrays, they can change sizes.

Or

Maybe linked lists may suit your needs.

Dynamic Arrays --> CPlusPlus

Linked Lists (I'm not too sure about these, for a fact a barely know anything about them)

Good Luck,

Legend28469
Re: C++ question
June 25, 2009 09:59PM
Thanks for both of your replies. I'll look into both methods and post here when/if I find a solution. My current solution (just so I can test what I'm writing) is to use a maximum value, but obviously that makes some cases where there are way more members of the array than I need. (If I neglected to mention, this array that I need to change the size of in run time is part of a class)

Update: Dynamic Memory lets me declare an arrays size at run time, but it seems it won't let me change the size of what I've already declared unfortunately.



Edited 2 time(s). Last edit at 06/25/2009 10:03PM by Arikado.
Re: C++ question
June 25, 2009 10:08PM
Your right, you'd have to do a bit of recoding if you want to use dynamic arrays. I guess you'll be using linked lists.
Re: C++ question
June 25, 2009 10:29PM
Well, if you could change an arrays size at runtime, after is declared and stored in memory, it can't be guaranteed that enough memory is avaiable directly after the end of the array.
I beleive that is the reason for not allowing it in C++.
(There may be ways do to that in Virtual Memory)
(We adress memory on the Wii directly, don't we?)

It is hard to recommend anything w/o knwoing whta you need it for, but I guess Linked Lists (as Legend recommended) are the best multipurpose answer, unless you want the items to be stored as sequential as possible?

Otherwise you could also use the last element in an array as a pointer to the next array
Re: C++ question
June 26, 2009 02:20PM
K, so I'm going for linked lists with an OOP approach then. Thanks for everyone's help :) Also, as a last shot in case I don't need to, heres the header of my class that needs a resizeable array:

//This file prototypes a simple Sprite class for ALLEGRO

         class Sprite{
               
               public:
               //Constructor
               Sprite();
               //Destructor
               ~Sprite();
               //Class Functions
               void SetBITMAP(BITMAP *bitmaptoset, int width, int height);//Sets a single-framed BITMAP to represent a Sprite
               void SetBITMAP(BITMAP *bitmaptoset, int width, int height, int columns, int framenumber);//Sets a multi-framed BITMAP to represent a Sprite
               void Draw(BITMAP *backbuffer);//Draws the BITMAP set to the Sprite
               void Draw(BITMAP *backbuffer, int transform);//Same as the first Draw, except the integer param specifies a transformation(1-Horiz, 2-Vert)
               void JumpFrame(int frame);//Change to a different frame
               void NextFrame();//Sets to the next frame of the assigned BITMAP
               void PrevFrame();//Sets to the previous fram of the assigned BITMAP
               bool CollidesWith(Sprite *spritetocollidewith);//Checks to see if one sprite collides with another
               bool CollidesWith(int Mouse_X, int Mouse_Y, int Mouse_Width, int Mouse_Height);//Checks to see if a Sprite collides with the mouse
               //Callbacks
               int GetWidth();
               int GetHeight();
               //Variables
               int X;//X value on a 2D plane
               int Y;//Y value on a 2D plane
               
               protected:
               int SpriteMaxFrame;
              BITMAP *SpriteBITMAP[SpriteMaxFrame];//BITMAP assigned to Sprite <--Works perfectly when I have a number replace "SpriteMaxFrame"
               int SpriteWidth;//Width value of each frame in a BITMAP
               int SpriteHeight;//Height value of each frame in a BITMAP
               int SpriteFrame;//What frame the Sprite is on
               
         };

Edit: Also, I can post its .cpp partner if any thinks they need to see it. (Again, the class works perfectly when I have a number like 5 or something replace "SpriteMaxFrame")



Edited 2 time(s). Last edit at 06/26/2009 02:23PM by Arikado.
Re: C++ question
June 26, 2009 09:31PM
The Linked List works.

What just ensued before this post was the hardest, longest, 4 hours of my life writing this linked list. I can't even begin to describe all of the difficulties I ran into, and the design problems. The compilation errors galore. Then, when it was finally compiling, all it did was crash my program and I spent forever staring at cryptic compiler output to fix the problem. (however, the hex compiler output dev-c++ lets you look at was extremely helpful)

Tomorrow, after some code cleanup, I'm going to release the Sprite Class code, linked list code included, on my web site ( [arikadosblog.blogspot.com] )

Now if you'll excuse me, I'm going to go spend the rest of the day at the beach or something. Or anywhere else that's far, far away from my computer.

Thanks again to all of the support I received in this topic. You all just helped create the most powerful, working, ALLEGRO Sprite class in the world :)



Edited 1 time(s). Last edit at 06/26/2009 09:31PM by Arikado.
Re: C++ question [SOLVED!!!]
June 27, 2009 07:01PM
You are welcome!!

I just have one question: Why didn't you post in the coding section?
Re: C++ question [SOLVED!!!]
June 27, 2009 07:08PM
Because the coding section is for "General Wii Programming Questions" My question had nothing to do with programming on the Wii but instead, more with C++ in general. I really wish the offtopic section would be used for non-Wii related intensive computer programming/hacking/homebrewing more often than the ubiquitous rants/happy-something-you-didnt-care-about-day/help-me-with-something-simple-that-has-nothing-to-do-with-the-wii topics. (The admins used to delete/junk topics like those, but I'm not that mean)
Re: C++ question [SOLVED!!!]
June 27, 2009 07:14PM
OK! Good to know :)
Re: C++ question [SOLVED!!!]
July 07, 2009 06:56PM
Three other possibilities exist that I would want to see here if I didn't know them, and found this page by googling, so here goes.

(1)Change your array into a pointer to an array then new a bigger one, copy the old elements into it, delete the old one, and fix the pointer. What you're doing here is building part of std::vector, so
(2) just using std::vector is probably a better plan.
(3)There is also a std::list which is a double linked list. (both forward and reverse iteration work.)

The Standard Template Library (STL) is your friend until optimization time, and even then lots of the time.



Edited 1 time(s). Last edit at 07/07/2009 06:58PM by segers_j.
Re: C++ question [SOLVED!!!]
July 12, 2009 07:03PM
Yeah, you should really get used to using the C++ standard library. It's the way to do things. Using std::vector would have saved you all those 4 hours:

std::vector< int > resizableArray; // array of ints but who knows how many ints
resizeableArray.push_back( 5 ); // increase size of the array by one, by adding the element 5
doSomethingWith( resizeableArray[ 0 ] ); // do something with 5

And there's functions for erasing, sorting etc... as well. And it's likely faster & more stable than anything you (or me) could write (no offense meant, just that the guys who did it are very clever people) Basically, you should always use std::vector for this sort of thing unless you have a very good reason not to



Edited 2 time(s). Last edit at 07/12/2009 07:08PM by seiken.
Re: C++ question [SOLVED!!!]
July 13, 2009 08:02PM
Yes, it's true; My knowledge of the C++ standard library is laughably limited. To that end, I've probably coded a ton of things I didn't need to at one point or another. If you could suggest some reading on the subject, I'd really appreciate it.

Also, quick question, is the C++ standard library platform independent?
Re: C++ question [SOLVED!!!]
July 13, 2009 08:10PM
Yes (and no).
The C++ and C standard library functions should be avaibale on all platforms / toolchains
But of course nothing is forcing anybody to actually port them.



Edited 1 time(s). Last edit at 07/13/2009 08:12PM by daniel_c_w.
Re: C++ question [SOLVED!!!]
July 13, 2009 10:06PM
Ah, I understand now.

I certainly don't want people who use my sprite class to have to hunt down a proper C++ Standard Library to be able to compile and run my platform independent programs on their OS. I think I'll keep my linked list.
Re: C++ question [SOLVED!!!]
July 13, 2009 10:24PM
Quote
Arikado
I certainly don't want people who use my sprite class to have to hunt down a proper C++ Standard Library to be able to compile and run my platform independent programs on their OS. I think I'll keep my linked list.

Well, vector should be avaiable on the Wii (devkitPro / PPC), and is definitly part of all big linux and windows compiler suites.
And I bet, you would even have it on the iPhone.

But if your code works, there is no need to change it.
Re: C++ question [SOLVED!!!]
July 13, 2009 10:31PM
Quote
daniel_c_w
Quote
Arikado
I certainly don't want people who use my sprite class to have to hunt down a proper C++ Standard Library to be able to compile and run my platform independent programs on their OS. I think I'll keep my linked list.

Well, vector should be avaiable on the Wii (devkitPro / PPC), and is definitly part of all big linux and windows compiler suites.
And I bet, you would even have it on the iPhone.
Well, since my code is dependent on a platform independent library not (yet?) designed to work on some of those platforms, it's probably best to stay with my platform independencies.

Quote
daniel_c_w
But if your code works, there is no need to change it.
Since the code is also not messy, I completely agree.
Re: C++ question [SOLVED!!!]
July 15, 2009 06:28PM
For those interested: C FAQ on "dynamic" arrays.
[c-faq.com]
Sorry, only registered users may post in this forum.

Click here to login