C++ Allocate dynamic array inside a function

12,098

If I have understood correctly you did not declare an array before calling the function. It seems that you declared a pointer to int instead of an array. Otherwise if you indeed declared an array then you may not change its size and allocate memory in the function.

There are at least three approaches to do the task. The first one looks like

int *f()
{
    size_t n = 10;

    int *p = new int[n];

    return p;
}

And the functionn is called like

int *p = f();

The other approach is to declare a parameter of the function as having type of pointer to pointer to int. For example

void f( int **p )
{
    size_t n = 10;

    *p = new int[n];
}

and the function can be called like

int *p = nullptr;

f( &p );

The third approach is to use reference to pointer as the function parameter. For example

void f( int * &p )
{
    size_t n = 10;

    p = new int[n];
}

and the function is called like

int *p = nullptr;

f( p );

The better way is to use standard class std::vector<int> instead of a pointer. For example

void f( std::vector<int> &v )
{
   size_t n = 10;

   v.resize( n );
}

and the function can be called like

std::vector<int> v;

f( v );

Also you could use a smart pointer like std::unique_ptr

Share:
12,098
kevin labille
Author by

kevin labille

Updated on June 04, 2022

Comments

  • kevin labille
    kevin labille almost 2 years

    so I need to allocate an array of int inside a function. The array is declared before calling the function (I need to use that array outside the function) and the size is determined inside the function. Is it possible ? I have been trying a lot of thing but nothing worked so far.

    Thanks for your help guys ! Here is some code :

    void fillArray(int *array)
    {
      int size = ...//calculate size here
      allocate(array, size);
      //....
    }
    
    void alloc(int * &p, int size)
    {
      p = new int[size];
    }
    
    int main()
    {
      //do stuff here
      int *array = NULL;
      fillArray(array);
      // do stuff with the filled array
     }
    
  • kevin labille
    kevin labille over 9 years
    It's almost what I need. I think I did not explain my issue pretty well though. So, I have that arrya of int inside my main function. Then I need to call a function A() that take into parameter that empty array, this function will calculate the size and will then allocate and fill the array. Afterward, I can use my filled array in my main function
  • Vlad from Moscow
    Vlad from Moscow over 9 years
    @kevin labille As I wrote in my post you may not define an empty array. You may define a pointer and then in a function allocate memory for an array and assign its address to the pointer as I showed in my post.
  • kevin labille
    kevin labille over 9 years
    I did try the first and second solution but it did not work. How should I pass the array to my function that is supposed to fill it ? Right now I have declared the array in the main, then I pass it to my function and then I call your 2nd method to allocate it, inside that function
  • Vlad from Moscow
    Vlad from Moscow over 9 years
    @kevin labille I wrote already that you may not declare an array and reallocate it in a function. See in my post how the function is called.
  • kevin labille
    kevin labille over 9 years
    Okay the third approach worked :) My function fillArray was declared as void fillArray(int *array) instead of void fillArray(int* &array) thank you Vlad