0

When we create an array on the stack most compilers will want to know the size of the array which will be determined at compile time so generally we can't get a user to enter the size of the array from standard input,but what if we are calling a function and we pass that number entered from the user into a function to create an array?

isn't this function been called at compile time?

for example if the user enters 1 isn't this function being called at run time? Since the user will determine whether or not the function will be called.

or is this still compile time?

#include <iostream>

void someFunction(int number){

    int sampleArray[number];

    for(int i = 0; i < number; i++){

        sampleArray[i] = 0;
    }
    // I know what I'm doing is pointless but is it possible?
}

int main()
{
   int number = 0;
   int choice = 0;

   std::cout << "do you want to create an array? press 1 for yes" << std::endl;
   std::cin >> choice;
   if(choice == 1){

   std::cout << "enter size" << std::endl;
   std::cin >> number;

   someFunction(number);

   }
}

5 Answers5

2

What you are using is called a variable length array. It is not part of the C++ standard but some compilers support it as an extension.

To make your code standard compliant, don't use it. Use std::vector<int> instead.

std::vector<int> someFunction(int number){

    std::vector<int> sampleArray(number);

    // No need for this. sampleArray elements are zero initialized.
    /*
    for(int i = 0; i < number; i++){
        sampleArray[i] = 0;
    }
    */
    return sampleArray;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

number is not a constexpr value.

so, int sampleArray[number]; is still not valid C++ and is a VLA extension.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

In the case you're showing, in C (and most C++ compilers), the space for the array is reserved in the stack when the function is called.

Similarly you can do it manually using functions such as alloca().

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
0

What you are doing is creating a variable length array, which is usually a bad practice, if you want to create you array statically it is a better to give it a fixed length value, else if you're in dire need of a variable length array, you can use dynamic allocation to make your array, or you can use vectors from the STL.

0

I think it's more correctly to name that dynamic memory management. Also i really doubt that the example code you have given will work correctly. For an example:

int sampleArray[number]; 

Will also cause an error in many compilers, and eventually you will still get a warning in other compilers.

You don't have to use function in order to initialize an array with different size. You can simply make that with a pointer:

int size;
std::cin >> size;
int * array = new int[size];

This will work in any compiler i know of, also it won't cause warnings. But you will have to take in mind that you will have to delete the memory when you don't need the array anymore. Like when it's about to go out of scope.

delete []array;

Since C++ 11 you can use smart pointer. Those will take care of the array memory instead of you and will delete the memory.

int size;
std::cin >> size;
std::shared_ptr<int[]> array(new int[size]);

Same can be done with unique_ptr and so on. For many cases it will be good enough just to use std::vector for any int datatype storage.

  • Prefer `std::unique_ptr` to `std::shared_ptr` because you always want to go to the narrowest, most tightly regulated solution possible. For this task prefer `std::vector` to both. – user4581301 May 28 '18 at 22:13