2

I need to declare a 2D array within a function and call the function repeatedly but the array should be declared only once at the beginning. How can I do this? I'm new to this. thanks in advance

0

5 Answers 5

3

Static Variables inside Functions

Static variables when used inside function are initialized only once, and then they hold there value even through function calls.

These static variables are stored on static storage area, not in stack.

consider the following code:

#include <iostream>
#include <string>

void counter()
{
    static int count[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    static int index = 0;

    std::cout << count[index / 3][index % 3];
    index++;
}

int main()
{
    for(int i=0; i < 9; i++)
    {
        counter();
    }
}

Output:

123456789
Sign up to request clarification or add additional context in comments.

2 Comments

The output is not like that, there is no 0 in the array. it seems you made it up :-) Anyways, I like your answer.
You're right, probably a typo ;) Thanks for the correction
2
void func1()
{
    static int myArra[10][20];
}

Comments

1

As Razack mentioned. That is the first way. and the second way is using std::array so you can accomplish like this.

#include <array>
void fun(){
    static std::array<std::array<int, 5>,5> matrix;
}

Comments

1

In C++ you can use a std::array of std::arrays to create a 2D array:

#include <array>

std::array<std::array<int, 3>, 3> arr = { {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}} };

This is a 3 x 3 2D array with each element initialised to 0. The syntax for accessing an element is the same as a C-style 2D array: arr[row][col].

It could be declared static within your function, but it could also be declared within an anonymous namespace at the top of your .cpp file like this:

namespace
{
   std::array<std::array<int, 3>, 3> arr = { {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}} };
}

This is generally better practice than static variables. The array is initialised only once before the main thread starts and only functions within your translation unit (your .cpp file) have access to it.

4 Comments

Actually, it's not recommended to use complex global scope objects as they may cause problems down the road. google.github.io/styleguide/…
@TigerYu But this isn't a global static variable. It's scope is limited to the file. It has no visibility outside of it. That's what an anonymous namespace does.
According to the google style guide, "Objects with static storage duration live from the point of their initialization until the end of the program. Such objects appear as variables at namespace scope ("global variables"), as static data members of classes, or as function-local variables that are declared with the static specifier. "
@TigerYu But this has internal linkage. It's not global. Have a look at this: google.github.io/styleguide/… : ["Use of internal linkage in .cc files is encouraged for all code that does not need to be referenced elsewhere. Do not use internal linkage in .h files."
0
void process(int ele, int index) {
    static std::vector<std::vector<int>> xx_vec = {{1,2,3}, {11,12}, {}};
    // example:
    for (int i = 0; i < xx_vec.size(); i++) {
        // add
        if (index == i) {
            xx_vec[i].push_back(ele);
        }
        // read
        for (int j = 0; j < xx_vec[i].size(); j++) {
            std::cout << "xx_vec" << "place:" << i << "," << j << ":" << xx_vec[i][j] << std::endl;
        }
    }
}

Comments

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.