Home > Mobile >  how to use static keyword in cpp
how to use static keyword in cpp

Time:12-08

Correct me if i'm wrong , i know that static keyword is used when we want to make the variable to be initialised only once and mostly used during recursion.I have never used static much before. Although I was solving a problem today where you have to add the numbers in a BST which are in between the given range.I wanted to try static keyword because i learned about it recently , so this was my code: `

class Solution {
public:
    int rangeSumBST(TreeNode* root, int low, int high) {
        static int sum=0;
        if(root==NULL)return sum;

        if(root->val>=low && root->val<=high)sum =root->val;

        if(root->left)rangeSumBST(root->left,low,high);
        if(root->right)rangeSumBST(root->right,low,high);

        return sum;
    }
};

This worked fine for the input :

root = [10,5,15,3,7,null,18], low = 7, high = 15

where the answer is 32.

But for the next test case:

root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10

The answer should be 23 but it shows 55. I tried to see what numbers were being added :

if(root->val>=low && root->val<=high)sum =root->val;cout<<root->val;

which gave output as: 10,7,6 which should add upto 23 but it gives 55.

My final solution was (without static) : `

class Solution {
public:
    int sum=0;
    int rangeSumBST(TreeNode* root, int low, int high) {
        if(root==NULL)return sum;

        if(root->val>=low && root->val<=high)sum =root->val;

        if(root->left)rangeSumBST(root->left,low,high);
        if(root->right)rangeSumBST(root->right,low,high);

        return sum;
    }
};

How to use static keyword?

CodePudding user response:

Recursion works because a functions context is created every time it is called. In that sense static is a sort of anti-recursion. So I'm having a hard time with the statement that static is mostly used during recursion. It's rarely used like this in my experience, and your example is a good example of why.

Here's your code rewritten so that it works, sum is a local variable, not static, not member, not global.

class Solution {
public:
    int rangeSumBST(TreeNode* root, int low, int high) {
        if (root == NULL)
            return 0;
        int sum = 0; // NOT static
        if (root->val >= low && root->val <= high)
           sum  = root->val;
        sum  = rangeSumBST(root->left, low, high);
        sum  = rangeSumBST(root->right, low, high);
        return sum;
    }
};

The important difference is that I use the return value of the recursive calls, instead of trying to return a value via some static, member or global variable. This way each recursive call is independent of each other, which is as it should be.

Static is commonly used for singletons, save it for that.

CodePudding user response:

Your understanding of static is deeply flawed. It's not about being initialized only once, and while I suppose you might use it during recursion, I never have.

Never.

A static variable means there is exactly one copy, and it is persistent. If your program enters that method now, and then enters it again an hour from now, it will have whatever value it had at the end of last run.

Furthermore, if you have a multi-threaded program, there is still only one copy, and both threads are using it.

Even if you have 10 Solution objects, there will be a single sum value.

The use for static is thus rare. The only time I ever use it is for the Singleton pattern, which is advanced for you and considered controversial.

So while it's important to know about it, at your level of programming, you probably aren't going to use it. You are better to make use of fields inside your class rather than static variables inside your methods.

They'll start to creep into your code when you begin multi-threaded programming, which you're probably not anywhere near.

  • Related