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.
- Type:
void * - Can hold address of any variable
- Must be converted to proper type before use
- Useful in functions that accept unknown data types
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.
- Not assigned to any valid address
- Dangerous – may crash program
- Always initialize pointers before use
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.
- Pointer exists, but memory is invalid
- Happens after free()
- Also happens when a local variable returns
- Fix → Set pointer to NULL after free()
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
- Memory freed using
free() - Local variables destroyed after function returns
- Reassigning pointer without freeing old memory
Why These Pointers Are Dangerous?
- Program crashes
- Security vulnerabilities
- Overwriting other data
- Unexpected results
- Memory corruption
Safe Pointer Practices
- Always initialize pointers
- Assign NULL after freeing memory
- Check pointer before dereferencing
- Use malloc/calloc responsibly
- Avoid returning address of local variables
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
- What is a void pointer?
- How does a wild pointer occur?
- Explain dangling pointer with example.
- Why should we set pointer to NULL after free()?
- 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