pointer related problems dangling/wild pointers c++

C++
// a) The pointer pointing to local variable becomes 
// dangling when local variable is not static. 
// b) A pointer pointing to a memory location that has been deleted 
// (or freed) is called dangling pointer. There are three different 
// ways where Pointer acts as dangling pointer.
#include<stdio.h> 
  
int *fun() 
{ 
    // x is local variable and goes out of 
    // scope after an execution of fun() is 
    // over. 
    int x = 5; 
  
    return &x; 
} 
  
// Driver Code 
int main() 
{ 
    int *p = fun(); 
    fflush(stdin); 
  
    // p points to something which is not 
    // valid anymore 
    printf("%d", *p); 
    return 0; 
} 
// Output ==> A Garbage Address//Void pointer is a specific pointer type – void *
// – a pointer that points to some data location in storage,
// which doesn’t have any specific type. 
#include<stdlib.h> 
  
int main() 
{ 
    int x = 4; 
    float y = 5.5; 
      
    //A void pointer 
    void *ptr; 
    ptr = &x; 
  
    // (int*)ptr - does type casting of void  
    // *((int*)ptr) dereferences the typecasted  
    // void pointer variable. 
    printf("Integer variable is = %d", *( (int*) ptr) ); 
  
    // void pointer is now float 
    ptr = &y;  
    printf("\nFloat variable is= %f", *( (float*) ptr) ); 
  
    return 0; 
} 

Source

Also in C++: