Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare an array inside a function according to the size of a parameter?

Tags:

c++

arrays

vector

Here is what I have tried:

int fun1(vector<int> s)
{ 
    const int n = s.size();
    int arr[n]; //<----want to declare an array of length s.size()
}

But this tells me that n is not a constant expression, so I cannot use it to declare the array size. But if I try:

int fun1(vector<int> s)
{ 
    const int n = 10;
    int arr[n]; //<-----this works
}

then it's fine. Even if I make the vector s of type const, it still won't recognize the size as a constant expression. How do I go about this?

like image 497
Henry Avatar asked Oct 22 '13 20:10

Henry


People also ask

How do you declare an array with variable size?

Variably changed types must be declared at either block scope or function prototype scope. Variable length arrays is a feature where we can allocate an auto array (on stack) of variable size. It can be used in a typedef statement. C supports variable sized arrays from C99 standard.

How do you declare an array inside a function?

The general syntax for declaring a two-dimensional array involves specifying the data type, array name, row index, and column index: DataType ArrayName [RowSize] [ColumnSize]; In this code, we declare a two-dimensional array Student[10][5] that can hold 10 arrays of Student[5] .

How do you pass the size of an array to a function?

The first one would be called like func1(foo, foo + CAPACITY) : passing in a pointer to the start of the array, and a pointer to just beyond the last element. The second would be called like func2(foo, CAPACITY) : passing in a pointer to the start of the array and the array size.

Which is the correct way to declare an array size?

Declaring Arrays:typeName variableName[size]; This declares an array with the specified size, named variableName, of type typeName. The array is indexed from 0 to size-1. The size (in brackets) must be an integer literal or a constant variable.


3 Answers

Declaring array by int arr[N]; the size N must be determined in compile-time (except for some compiler extensions which allow you to define them in run-time too). By the way, you can make it:

std::unique_ptr<int[]> arr (new int [n]);

// ... or ...

std::vector<int> arr(n);
like image 160
masoud Avatar answered Oct 19 '22 13:10

masoud


When you declare an array like this

int arr[n];

the compiler will allocate memory on the stack for it. In this case the C++ standard requires that n is known at compile time, i.e. it must be const.

The answer to your question is to get the memory from the heap at run time like this:

int* arr = new int[n];

In this case the memory is allocated at run time and so the value of n doesn't need to be known until run time. If you use this approach don't forget to free up the memory when you're done with:

delete [] arr;

However, as the comments suggest, using a std::vector<int> would almost certainly be a better approach. Unless you've got a good reason not to, I'd go with that.

like image 2
eoinmullan Avatar answered Oct 19 '22 13:10

eoinmullan


For this, C++ has std::vector<int>(n), which retains most of the semantics of a traditional C array but also adds a lot of benefits (dynamic allocation being one, resizing is another, algorithm support is yet another one). Even when your underlying code requires a C array, you can still use the vector and pass an address of the first element down (they are guaranteed to be contiguous).

Typically, std::vector uses heap for underlying storage, so on one hand you are better protected from stack overflows (pun intended), on the other hand, your code now uses dynamic allocation.

like image 1
Alexander L. Belikoff Avatar answered Oct 19 '22 11:10

Alexander L. Belikoff