Dangling Pointer Demo

Dangling Pointer Demo

Hard C Pointers 29 views
Explanation Complexity

Problem Statement

Return the address of the local variable in the function. Initialize it in the function. What's the problem? Then indicate the correct reason (dynamic allocation or static variable).

Input Format

A function that declares a local variable and returns its address.

Output Format

Explanation of the problem and the correct alternatives.

Example

Function returns address of a local variable.
Undefined behavior / garbage value

Constraints

• Local variables have block scope

• Memory is stack-allocated

Concept Explanation

Returning the address of a local variable from a function causes a serious problem.

Step-by-Step Explanation

1.Function ke andar local variable declare hota hai.

2.Uska address return kiya jata hai.

3.Function return hote hi local variable ka memory khatam ho jata hai.

4.Returned address invalid ho jata hai.

5.Access karne par undefined behavior hota hai.

Concept Explanation

Returning the address of a local variable from a function causes a serious problem.

Step-by-Step Explanation

1.Function ke andar local variable declare hota hai.

2.Uska address return kiya jata hai.

3.Function return hote hi local variable ka memory khatam ho jata hai.

4.Returned address invalid ho jata hai.

5.Access karne par undefined behavior hota hai.

Input / Output Format

Input Format
A function that declares a local variable and returns its address.
Output Format
Explanation of the problem and the correct alternatives.
Constraints
• Local variables have block scope

• Memory is stack-allocated

Examples

Input:
Function returns address of a local variable.
Output:
Undefined behavior / garbage value

Example Solution (Public)

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

/* Incorrect: returning address of local variable */
int* wrongFunction() {
    int localVar = 100;       // local variable on stack
    return &localVar;         // ❌ Dangerous!
}

/* Correct: using static variable */
int* correctStatic() {
    static int staticVar = 200; // static variable persists after function ends
    return &staticVar;          // ✅ Safe
}

/* Correct: using dynamic allocation */
int* correctDynamic() {
    int *ptr = (int *)malloc(sizeof(int)); // allocated on heap
    if (ptr != NULL)
        *ptr = 300;
    return ptr; // ✅ Safe, must free later
}

int main() {
    int *p1 = wrongFunction();
    printf("Wrong function returned: %d (undefined behavior!)n", *p1);

    int *p2 = correctStatic();
    printf("Static variable returned: %dn", *p2);

    int *p3 = correctDynamic();
    printf("Dynamically allocated returned: %dn", *p3);

    free(p3); // free heap memory

    return 0;
}

Official Solution Code

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

/* Incorrect: returning address of local variable */
int* wrongFunction() {
    int localVar = 100;       // local variable on stack
    return &localVar;         // ❌ Dangerous!
}

/* Correct: using static variable */
int* correctStatic() {
    static int staticVar = 200; // static variable persists after function ends
    return &staticVar;          // ✅ Safe
}

/* Correct: using dynamic allocation */
int* correctDynamic() {
    int *ptr = (int *)malloc(sizeof(int)); // allocated on heap
    if (ptr != NULL)
        *ptr = 300;
    return ptr; // ✅ Safe, must free later
}

int main() {
    int *p1 = wrongFunction();
    printf("Wrong function returned: %d (undefined behavior!)n", *p1);

    int *p2 = correctStatic();
    printf("Static variable returned: %dn", *p2);

    int *p3 = correctDynamic();
    printf("Dynamically allocated returned: %dn", *p3);

    free(p3); // free heap memory

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.