Void, Wild & Dangling Pointers

Pointers are powerful, but incorrect use can cause serious problems like crashes, memory corruption, or unpredictable behavior. Three commonly misunderstood pointer types are Void Pointers, Wild Pointers, and Dangling Pointers.

1. Void Pointer

A void pointer (generic pointer) can store the address of any data type. But it cannot be dereferenced unless typecasted.

Example – Void Pointer

#include <stdio.h>

int main(){

    int a = 10;
    void *ptr;

    ptr = &a;

    printf("%d", *(int*)ptr);

    return 0;
}

2. Wild Pointer

A wild pointer is an uninitialized pointer that points to random memory, leading to undefined behavior.

Example – Wild Pointer

#include <stdio.h>

int main(){

    int *p;   // wild pointer

    // *p = 5;  // ❌ Dangerous! Unknown location

    return 0;
}

3. Dangling Pointer

A dangling pointer occurs when the memory it points to is freed or goes out of scope.

Example – Dangling Pointer After free()

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

int main(){

    int *p = (int*)malloc(sizeof(int));
    *p = 50;

    free(p);   // memory freed
    p = NULL;  // avoid dangling pointer

    return 0;
}

Causes of Dangling Pointers

Why These Pointers Are Dangerous?

Safe Pointer Practices

Wild, Void & Dangling Pointers — 10 Examples

1. Wild Pointer (Uninitialized Pointer)

#include <stdio.h>

int main() {

    int *p;   // wild pointer
    // *p = 10;  // dangerous

    printf("Wild pointer created (uninitialized).");

    return 0;
}

2. Assigning Address to Wild Pointer (Fix)

#include <stdio.h>

int main() {

    int x = 50;
    int *p;

    p = &x;  // now safe

    printf("%d", *p);

    return 0;
}

3. Dangling Pointer After Free()

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

int main(){

    int *p = malloc(sizeof(int));
    *p = 100;

    free(p);   // memory released

    // p is now a dangling pointer
    printf("Pointer is dangling now.");

    return 0;
}

4. Fix Dangling Pointer Using NULL

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

int main(){

    int *p = malloc(sizeof(int));
    free(p);

    p = NULL;   // fix

    if(p == NULL)
        printf("Pointer safely set to NULL");

    return 0;
}

5. Dangling Pointer After Returning Local Variable

#include <stdio.h>

int* test(){
    int x = 10;   // local
    return &x;    // returns dangling pointer
}

int main(){
    int *p = test();
    printf("Returned a dangling pointer.");
    return 0;
}

6. Void Pointer Storing Any Data Type

#include <stdio.h>

int main(){

    int a = 10;
    float b = 5.5;

    void *p;

    p = &a;
    printf("Int = %d\n", *(int*)p);

    p = &b;
    printf("Float = %.1f", *(float*)p);

    return 0;
}

7. Void Pointer Cannot Be Dereferenced Directly

#include <stdio.h>

int main(){

    int x = 10;
    void *p = &x;

    // printf("%d", *p); // error
    printf("%d", *(int*)p);   // correct

    return 0;
}

8. Converting Void Pointer to Char Pointer

#include <stdio.h>

int main(){

    char c = 'A';
    void *vp = &c;

    printf("Character = %c", *(char*)vp);

    return 0;
}

9. Multiple Dangling Pointers Pointing to Same Memory

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

int main(){

    int *p = malloc(sizeof(int));
    int *q = p;

    free(p);

    // both p and q are dangling
    printf("Two dangling pointers created.");

    return 0;
}

10. Avoid Dangling Pointer by Reallocating

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

int main(){

    int *p = malloc(sizeof(int));
    free(p);

    p = malloc(sizeof(int));  // allocated again

    *p = 20;
    printf("%d", *p);

    return 0;
}

Practice Questions

  1. What is a void pointer?
  2. How does a wild pointer occur?
  3. Explain dangling pointer with example.
  4. Why should we set pointer to NULL after free()?
  5. List three safe pointer practices.

Practice Task

Create a program that: ✔ Uses a void pointer to print an int, float, and char ✔ Demonstrates a dangling pointer ✔ Fixes it safely using NULL