1

I’d like to easily initialize an array inside the function:

    int arr[]{1, 2, 3};

It simplifies calculating the size of the array:

    int n  = sizeof(arr) / sizeof(*arr);

However, I need this array to be preserved and be accessible outside the function.

How to do this?

For the start, I have this:


    #include <iostream>

    using namespace std;

    int n{0};
    int* g_arr{nullptr};

    void f()
    {
        int arr[]{1, 2, 3};

        n = sizeof(arr) / sizeof(*arr);
        g_arr = arr; // you can't use g_arr value outside this function, since arr will be freed.
    }

    void b()
    {
        cout << n;

        for (int i = 0; i < n; ++i)
        {
            cout << g_arr[i] << endl; // this is still wrong
        }
    }

    int main()
    {
        f();
        b();
    }
5
  • You could have int g_arr[3]; at file scope and assign the values in f Commented Feb 23, 2020 at 9:26
  • @M.M Not in my case. I need it inside the function. Commented Feb 23, 2020 at 9:28
  • In the code you have posted there is no need for arr inside the function. In fact it ceases to exist when f returns. If you have other requirements then you will need to detail them Commented Feb 23, 2020 at 9:30
  • @M.M Well, I need what I need. It would be very verbose to explain why. It leads to embeded programming and sparing the SRAM using PROGMEM. Commented Feb 23, 2020 at 9:36
  • 1
    Well you will need to try. The local array in the question stops existing when the function returns. If you're trying to say you create the array by some other means then obviously that is relevant to the question. Otherwise we just end up with an endless sequence of people suggesting answers and you saying "Oh that won't work for me" Commented Feb 23, 2020 at 9:40

2 Answers 2

2

Make arr[] static:

 void f()
 {
    static int arr[]{1, 2, 3};

    n = sizeof(arr) / sizeof(*arr);
    g_arr = arr;
 }
Sign up to request clarification or add additional context in comments.

Comments

1

A possible solution is to copy the stack allocated array:

#include <iostream>
#include <cstring>

int* g_arr = nullptr;
std::size_t n = 0;

void f() {
  int arr[]{1, 2, 3};

  n = sizeof(arr) / sizeof(*arr);

  delete[] g_arr;
  g_arr = new int[n];
  std::memcpy(g_arr, arr, sizeof(arr));
}

void b() {
  std::cout << n << '\n';

  for (std::size_t i = 0; i != n; ++i) {
    std::cout << g_arr[i] << '\n';
  }
}

int main() {
  f();
  b();
}

If you can, use a vector:

#include <iostream>
#include <vector>

std::vector<int> g_arr{};

void f() {
  g_arr = {1, 2, 3};  
}

void b() {
  std::cout << g_arr.size() << '\n';

  for (int const i : g_arr) {
    std::cout << i << '\n';
  }
}

int main() {
  f();
  b();
}

6 Comments

Unfortunately, I can't use vector. Templates library is not available in my environment and it wastes lots of SRAM.
Can you dynamically allocate arr?
I can. However I'll lose the conveniences of using []{…}.
@zhekaus considering []{...} is very important for you, I updated the answer
In the first case you can use unique_ptr<int[]> rather than a raw owning pointer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.