1

In Linux, "static" variables are present in the BSS segment (in a code that is not running) and "local" variables are present in the "stack". This means that static variables local to a function are present in the BSS area. How is the book-keeping done to ensure that the scope is within the function itself?

1 Answer 1

3

Where a variable is stored has nothing to do with scope. It's usually the compiler itself that restricts scope (access to the variable). When you have a statement like:

static int xyzzy;

within a function, xyzzy will not be allowed to be accessed by code outside that function, regardless of the fact that it has static storage duration.

Any attempt to do so will be a compile-time error, not a run-time check.

In fact, you can affect the local static variable but with something like a buffer overflow (running into the storage area where it exists), not via its name (which the compiler will disallow).

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

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.