Jump to content
  • Advertisement
Sign in to follow this  
Zmurf

Array size defined with a variable.

This topic is 4694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I define the size of an array through a variable. Eg;
int size = 256;
string array[size];
except that doesn't work, so how can i do it?

Share this post


Link to post
Share on other sites
Advertisement
You need to dynamically create the array, that is:


int size = 256;
string *array = new string[size];

// ...
// cleanup
delete [] array;






[edit] But since you are using string (I assume STL), why not use vector as well (edit3: see Enigma's post for an example)?
[edit2] If size never changes, you can make it const and it will work in your original code.

jfl.

Share this post


Link to post
Share on other sites
std::vector< std::string > array(256) or boost::array< std::string, 256 > array.

EDIT: Sorry, boost::array won't work for a dynamic size. I was thinking there was a dynamic but non-resizeable array type in boost, but it appears that there isn't. Instead you can just use boost::shared_array< std::string > array(new std::string[size]) or boost::scoped_array< std::string > array(new std::string[size]).

Enigma

[Edited by - Enigma on October 9, 2005 6:27:51 PM]

Share this post


Link to post
Share on other sites
I think gcc allows it anyway. It's certainly not standard in C++ but it is in C99 (though I'm guessing from the string declaration you aren't using that).

Share this post


Link to post
Share on other sites
Also instead of using int whynot use unsigned int?


using namespace std ;
typedef unsigned int UINT ;

const UINT SIZE = 100 ;
vector< string > my_array( SIZE ) ;

Share this post


Link to post
Share on other sites
Declaring an array with a constant size (be it a literal or symbolic constant) isn't declaring an array with the size defined by a variable.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
C99 allows this, they're called "Variable Length Arrays" or VLAs for short.

This also brings up a difference between C and C++ that's perhaps significant in this context. Consider:
int main(void) {
const int size = 10;
int foo[size];
return 0;
}
In C99 foo is a VLA, in C++ foo is an array, and in C89 this is illegal.
const means "constant" in C++ and "read-only" in C.

Also, if you want a non-standard option, alloca() might be helpful depending on why you need a variable length array.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!