Can a local variable memory be accessed outside its scope in C/C++?

Many newbies ask this question, “can a local variable memory be accessed outside its scope in C/C++?”. So in this post, I will explain this question but before explaining it let’s understand the meaning of the local variable.

What is a local variable in C/C++?

A local variable is a variable that is given local scope. The local variable is declared within a function, block (within curly braces), or function argument.  The local variable could be static or non-static.

Consider the below example program, where a, x, y are non-static local variables and b is static local variables. The non-static local variable is created in stack memory. If you are not familiar with the C program memory layout, you can see my blog post “Memory Layout of C program“.

void foo(int x, int y)
{
    int a;
    static int b = 0;
}

 

You also must remember that the same variable name in the larger scope is overridden by local variable references in the function or block where it is defined. The following example shows how a local variable overrides the variable of the same name of larger scope.

#include <stdio.h>

int main()
{
    int data = 5;
    {
        //data is overriding here
        int data = 10;
        printf("value of data = %d\n",data);
    }

    return 0;
}

Output: value of data = 10

 

Can a local variable’s memory be accessed outside its scope in C/C++?

Now I believe you are familiar with the local variable, so now it’s time to explain this question. Because here we have seen two types of local variables show we will give the answer according to their types.

non-static local variable:

So if you are thinking to access non-static local variables outside their scope, then it is the biggest mistake of your life. Your code shows UB (undefined behavior).

Now you are thinking why I am saying this?

So let’s understand it with an example code. So consider the below example.

#include<stdio.h>
int* Fun()
{
    //non-static Local variable
    int data = 5;

    //Address of local variable
    return &data;
}

int main()
{
    int *ptr = Fun();

    printf("%d", *ptr);

    return 0;
}

Output: UB

 

Explanation:

In the above code, data is a non-static local variable. We know that the stack contains non-static local variables. A stack frame represents a function call and its parameter data in the case of a call stack. When the function ends the stack frame associated with this function is destroyed and memory is allocated to the data variable is also destroyed. It means there is no data variable in the memory.

Now you can think that here you are trying to access a memory that is not actually available for you. So the behavior of the code will be undefined.

 

Static local variable:

A local static variable has a static storage duration ( global lifetime ) and it is only visible within the block in which it is declared. Because a local static variable lives throughout the program, we can access it outside of its scope.

Consider the below example code.

#include<stdio.h>

int* Fun()
{
    //static Local variable
    static int data = 5;

    //Address of static local variable
    return &data;
}

int main()
{
    int *ptr = Fun();

    printf("%d", *ptr);

    return 0;
}

Output: 5

 

Recommended Articles for you: