3

Auto and static variable have scope limited to the block in which they are defined. Since Auto variables are defined in the stack, if the function exits, stack is destroyed and memory for the auto variable is released. But I read somewhere "However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables reside." Is this correct?

Also, static variables are defined in the data section so it retains its existence till end of program. The scope is within the block in which it is defined. Is there any way through which we can access static variable from any other function? Also, Is there any way we can access static variable from any other file?

5
  • can you give an example of an auto variable that you'd like to access outside the function it was defined in? Commented Mar 27, 2019 at 5:53
  • And what do you need this for? There might be a better way to achieve whatever you are trying to do Commented Mar 27, 2019 at 5:54
  • 8
    Scope and lifetime are different things. You can use pointers to access objects outside their scope, but not outside their lifetime. Commented Mar 27, 2019 at 5:56
  • I just was made aware that I assumed that you are not interested in the case of variables being accessed from functions which are called from within scope and during life-time. Can you clarify? Commented Mar 27, 2019 at 7:09
  • 1
    @Akib: You accepted a bad answer that does not discuss how the address of an object may be passed to other functions during its lifetime. rici’s answer is better. Commented Mar 27, 2019 at 7:48

3 Answers 3

5

Here's a very simple example:

void print_msg(const char* msg) {
  printf("The message is: %s\n", msg);
}

int main(void) {
  char m[] = "Hello, world!";
  print_msg(m);
}

Here, m is an automatic variable, which is not in scope in print_msg. But print_msg clearly has access to its value.

Don't confuse "scope" with "lifetime". The scope of a variable is that part of the program where the variable's name is visible (and thus can be used). The lifetime of a value is the period during program execution in which a value exists. Scope is about program text; it relates to compilation. Lifetime is about program execution.

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

Comments

2

As you said, static variables exist through out the life cycle of the program i.e memory allocated to them is not destroyed as long as the program is running. So, to access such a variable out side its scope, we can pass around the pointer to that memory location via pointer. A small example to show the same

#include <stdio.h>
#include <stdlib.h>

int* func()
{
        static int a = 0;
        a++;
        printf("a in func = %d\n", a);
        return &a;
}

int main()
{
        int *p;
        p = func();
        printf("a in main from ptr : %d\n", *p);
        *p++;
        p = func();
        return 0;
}

As you can see in the example, func() returns the pointer to the static variable it has declared, and any one who wishes to access the variable a, can use that pointer. NOTE: we can only do this because static variable's life is through out the program. Now irrespective of the static variable being in a different function or a different file, as long as you can some how get hold of the pointer to that static variable, you can use it.

Now coming to the case of auto variable.

What happens if you run the above program changing a from static to auto? you will see that while compiling a warning warning: function returns address of local variable [-Wreturn-local-addr] is thrown and when executing, we get a segmentation fault. What causes this is that the auto variable exists only in its scope, i.e as long as the function func() is being executed, the variable a has memory allocated for itself. As soon as the function exits, the memory allocated for variable a is freed and so the value pointed to by pointer p is at some unallocated memory location (resulting in segmentation fault).

12 Comments

About the segfault thing: you wish! On pretty much any implementation the memory for the stack is actually always allocated as a single big chunk (barring automatic expansion through guard pages), and stack allocation/deallocation is just moving the top-of-the-stack pointer. For this reason, even if you read/write above the currently top of the stack, no access violation is triggered - you've simply written in a logically non allocated, but "physically" valid location, so either you are overwriting some unrelated local variable, or you are just writing on unused data.
Then again, if your implementation writes canary values on unused portions of the stack or is performing extra checks at each memory access (think AddressSanitizer builds) it may detect the problem; but in an optimized build, generally use-outside-of-scope is either silent or results in silent corruption.
If you run it under gdb you'll see it's dereferencing a NULL pointer. That's just a favor gcc does to you - it detects the problem at compile time (indeed, it prints a warning about it), and essentially transforms it to a return NULL to aid debugging (it's allowed to change whatever because you are dereferencing an invalid pointer, which is undefined behavior, so anything is allowed). Try to pass the address of the local out of the function through more convoluted means (e.g. a global volatile value) and you'll see that it all appears to work flawlessly.
@MatteoItalia +1 TIL
@yashC: The question does not ask about accessing an object after its lifetime ends. In this answer, it is shown that an object with automatic storage duration should not be accessed, as by the example program, after its lifetime. But that is not what the question asks about. Instead of showing an example about a situation that was not asked about for a behavior that one cannot properly use, the answer should show an example that was asked about that one can properly use. The fact is that it is perfectly proper to access an automatically allocated object by address during its lifetime…
|
0

Note, as comments rightly point out, I am making an assumption here, the assumption that the simplest case of calling another function is not what the question is about. This assumption was not (yet) confirmend or rejected by OP. This case is discussed e.g. in the answer by rici.

The existence of auto variables is not only exist to "within" their scope (simplified: only code between the same enclosing {} can use their identifier), they are also restricted to "during" their "chronological scope" i.e. their lifetime (simplified after starting the execution of the code in the function and finishing its execution). It is possible to access the memory location of a variable via a pointer, which was set to their address (which is only possible within their scope, because accessing via their identifier is necessary) as long as it is done during their lifetime, yes.

But how would that pointer be found from anywhere else?
Maybe by being written (from inside their scope and during their lifetime) to a global variable.

But which "other" code should then use that value? (remember I am putting the call of functions at the side here)
This requires multithreading/multitasking/multiwhatevering. Lets say there is an interrupt service routine doing it. It would have to see the same address space as the variables scope, i.e. no memory management units getting in the way with some virtual memory magic. This is not true for many multiwhatevering implementations, but admittedly for a few of them, so lets continue.

This imagined ISR would have to ensure that it only accesses the auto variable while it actually exists (i.e. during its lifetime), otherwise it would pretty much access what is effectively a meaningless random memory location. And this assumes that the ISR is actually allowed/able to access that memory. Even without MMUs, there are implementations which can/will have execeptions.
This introduces the need for synchronisation mechanisms, e.g. semaphores.

So in certain environments it would be possible, but completley pointless (global variables are still involved), expensive, hard to understand and next to impossible to port. (remember I am putting call of a function aside here)

Similar for static variables.

In the case of function local static variables, they would at least reliably exist, but accessing them would still need the pointer value to be somehow transported out of their scope. For static variables that could actually be done via the return value of the function as demonstrated in the answer by yashC.

In the case of "static" variables understood as file scope restricted variables, the pointer still would have to be transported out of the file scope.
This would merely defeat what is probably the point of a file scope restricted variable. But I could imagine some kind of access privilege scheme, as in "Here is the key to the vault. Handle with care."

As mentioned at the start of this answer, I am putting the call of other functions aside. Yes, the easiest way to leave the scope of a function is to call another one. If that other function has a pointer parameter, it can use it to read-access and write-access the auto variable of the calling function. That is the normal case of call-by-reference parameters as supported by C.
Calling a function also provides another, even simpler way of read-accessing the value of an auto variable of the calling function, though not write-accessing and not actually accessing the autovariable itself, only using its value. That way is the trivial mechanism of a call-by-value parameter, it does not even require a pointer. Both ways (call-by-reference parameter and call-by-value parameter) conveniently guarantee that the value does not change during the execution of the called function. (this time I am putting the multi-threaded case aside, because that is discussed in the main part of this answer).

9 Comments

It doesn't require multithreading; there is a much simpler case. You only need to pass the address of a local variable to a called function. It could be any function which takes a C-string (or an array for that matter). Or a function which returns extra values through call-by-reference.
@rici I would consider code being called from within scope to be within scope, at least for the idea of this quesiton. At least I think that the question is meant like that. Good input though. I will try to have my assumption clarified.
That is not what scope means. Scope is not the same as lifetime.
@rici Yes I know and it is exactly what I elaborate in this answer.
Re “The existence of auto variables is not only exist to "within" their scope”: This is incorrect; the existence of objects with automatic storage duration is not limited to within their scope. Consider an object O, its identifier I, and code C in which I is not in scope. It is possible for O to exist and be accessed by C. This is demonstrated in rici’s answer.
|

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.