Wild pointer and dangling pointer demo Dry Run in C

Wild pointer and dangling pointer demo is an interactive C dry run visualizer from the Pointersintro programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Wild pointer and dangling pointer demo Program Code

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

int* createDanglingPointer() {
    int local = 42;
    return &local;  // Returns address of local variable (BAD!)
}

int main() {
    // 1. WILD POINTER - uninitialized pointer
    int *wildPtr;  // Contains garbage address
    printf("Wild pointer value (garbage): %p\n", wildPtr);
    // *wildPtr = 10;  // CRASH! Never dereference wild pointer
    
    // 2. DANGLING POINTER - pointer to freed memory
    int *danglingPtr = malloc(sizeof(int));
    *danglingPtr = 100;
    printf("Before free: %d\n", *danglingPtr);
    free(danglingPtr);
    // danglingPtr still holds the address, but memory is freed
    printf("After free - dangling pointer address: %p\n", danglingPtr);
    // *danglingPtr = 200;  // CRASH! Undefined behavior
    
    // 3. Dangling pointer from returning local address
    int *funcPtr = createDanglingPointer();
    printf("Dangling from function: %p\n", funcPtr);
    // *funcPtr causes undefined behavior
    
    // 4. Correct pattern - set to NULL after free
    int *goodPtr = malloc(sizeof(int));
    *goodPtr = 50;
    free(goodPtr);
    goodPtr = NULL;  // Now it's a null pointer, safe to check
    
    if(goodPtr == NULL) {
        printf("goodPtr is correctly set to NULL\n");
    }
    
    return 0;
}

View the complete Pointersintro programs page.

Program Console Wild pointer and dangling pointer demo Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.