Site Stability Read more... ×

Public Group

arrays in functions

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

Recommended Posts

Say I want to declare an array inside a function but I don't know what size it will be. The size has to be a constant but even if I pass a constant to the function it doesn't work. I assume this is because it wants to know at compile time what size to reserve for this array before the function is ever called? // assuming size was declared as a const int in main void someFunction( int size ) { int array[size]; } Any tips/tricks to either make this work or tell me the right way I should be handling things like this?

Share on other sites
std::vector is your freind

Vectors are resizeable in run time..they are fairly easy to figure out and use also.

Share on other sites
Thanks, I'll look into those. Been doing exercises at the end of my books chapter on arrays. Was working out a bucket sort function where I was trying to pass the array size to create the bucket array. Ended up having to use static numbers in the code.

Share on other sites
Here's some code that demonstrates some really basic ways to work with a std::vector (but definitely read the article about vectors linked above).

#include <vector>#include <iostream>using std::cout;using std::endl;using std::vector;int main(){    vector<int> foo;    // add some elements to foo    foo.push_back(5);    foo.push_back(17);    foo.push_back(3);    // output how many elements are in the vector (how big it is)    cout << "Vector has: " << foo.size() << " elements" << endl;    // now print the values stored in the vector    // actually, it's better to use an iterator to go through the vector    // but i've done it this way to make it more understandable    for (int i = 0; i < foo.size(); ++i)        cout << "Element " << i << ": " << foo << endl;    // output a blank line to make things more readable    cout << endl;    // now remove the last element from the vector    foo.pop_back();    cout << "Vector now has: " << foo.size() << " elements" << endl;        for (int i = 0; i < foo.size(); ++i)        cout << "Element " << i << ": " << foo << endl;    return 0;}

The output will look like this:
Quote:
 Vector has: 3 elementsElement 0: 5Element 1: 17Element 2: 3Vector now has: 2 elementsElement 0: 5Element 1: 17

Thanks Will. :)

Share on other sites
You'll need to dynamically allocate your arrays if you want to pass in a dynamic value for size (const or not). Dynamic creation doesn't need to know the size at compile time.

void someFunction( int size )
{
int* array = new int[size];
// ... do stuff
delete [] array;
}

Don't forget to clean up after yourself also. Note that the [] is required when deleting if you new'd the object with an [].

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 15
• 21
• 26
• 11