EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Dangling Pointers in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Dangling Pointers in C

Dangling Pointers in C

Introduction to Dangling Pointers in C

The C Dangling pointer is a type of pointer that actually points to a specific memory location that is to be free or deleted. There are some different ways where the pointer now acts as a dangling pointer. Most of the times there are only 3 different types/ways where the pointer will act as one of the dangling pointers. They are De-allocation of memory, Function Call, and Variable goes out of the scope. These dangling pointers are used when the memory management and pointers is having most of the common bugs. It usually appears/occurs at the object destruction time whenever the object deleted or de-allocated from memory without modifying pointer value. In this topic, we are going to learn about Dangling Pointers in C.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

free(a1)

How Dangling Pointers Works in C?

The dangling pointers are similar pointer just like the normal pointer but it works by taking consideration of a de-allocated object/deleted object. It is nothing but a pointer which actually going to point a specific memory location that is actually deleted and it is called a dangling pointer.

The dangling pointer’s errors can only be avoided just by initializing the pointer to one NULL value. If we try assigning the NULL value to a specific pointer, then that pointer will not at all point to the needed deallocated memory. Assigning the NULL value to the specific pointer helps the pointer not pointing to any specific memory location.

For de-allocating memory of the C dangling pointer concept, free() function is used with a single parameter just to make a pointer into a dangling pointer. This is how the dangling pointer will be created with free() function in the C coding language. There is also another way of creating a dangling pointer. It is variable go out of the scope way of creating a dangling pointer concept.

The Dangling Pointers works just by pointing to the specific memory location which actually contains either some programming code or some code of the operating system. If we accept some value to the specific pointer then it will overwrite the value of the program code.

Examples of Dangling Pointers in C

Here are the following examples mention below:

Example #1

This is the example of the de-allocation of the memory of the C Programming Language by the specific ptr causes. At first, standard libraries or C language is included with the #include method/function. Then int main() is created to write C Code. The *ptr is created to get a pointer variable which is with the help of the malloc() function. The malloc() function usually returns the void value, so here int * is used to convert the void pointer to an int pointer. Then free() function with the parameter “ptr1” is used in order to make the pointer as a dangling pointer. So when the compiler completed the execution, the compiler will run the code perfectly but there will be no output because nothing is mentioned too in print to show as output in the command prompt.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Syntax:

#include <stdlib.h>
#include <stdio.h>
int main()
{
int *ptr1 = (int *)malloc(sizeof(int));
free(ptr1);
ptr1 = NULL;
}

Output:

Dangling Pointers in C output 1

Example #2

This is the example of implementing the Function call way or representing the dangling pointer. Here one of the pointers which point to the local variable becomes into a dangling pointer when the local variable is not at all static. Here the pointer didn’t become into dangling pointer because the x1 is not defined as a static term. Here at first a pointer fun1() is created with a normal int variable with value 5. Then main() is created for entering the C code. Then pointer p1 is created to call the fun1(). Then after this, the pointer p will not point to a specific point, it points to the one which is not at all a valid one anymore. Then printf is used to print. But here there will be a warning when the c code runs in the c compiler. Check out the output so that you will know. Here in this example, the normal pointer doesn’t even become into a dangling pointer.

Syntax:

#include<stdio.h>
int *fun1()
{
int x1 = 5
return &x1;
}
int main()
{
int *p1 = fun1();
fflush(stdin);
printf("%d", *p1);
return 0;
}

Output:

Dangling Pointers in C output 2

Example #3

This is also an example of the function call which is similar to the above example. At first, usually #include is used for including the standard library. Then a function fun11() is created and included a static int variable “x11” with a value “5”. Then the main() function is used along with the pointer variable “P11” to include the pointer function fun11(). Then fflush() function is used. The fflush function is mostly used for output streams. Fflush(stdin) is a type of undefined behavior. Then printf() function is used to print the pointer variable which is nothing but the x11 variable value.

Syntax:

#include<stdio.h>
int *fun11()
{
static int x11 = 5;
return &x11;
}
int main()
{
int *p11 = fun11();
fflush(stdin);
printf("%d", *p11);
return 0;
}

Output:

output 3

Example #4

This is the example of implementing the variable which goes out of the scope. Here the variable will go out of the scope then the pointer pointing to the variable becomes into a dangling pointer. Here at first, we are declaring the pointer variable str1. Then inside we are declaring a character variable. Now the str1 variable contains the variable “a1”s address. Then control will come out of inner scope. Here a1 variable will no longer available. So str1 will point to a specific deallocated memory. It means the str1 pointer will become into a dangling pointer but A1 is an undeclared variable.

Syntax:

#include<stdio.h>
int main()
{
char *str1;
{
char a1 = ?A1?;
str1 = &a1;
}
printf("%s", *str1);
}

Output:

output 4

Conclusion

I hope you understand what is the definition of C Dangling/Wild Pointers along with its syntax and explanation, how the dangling pointers work in C Programming Language along with various examples of implementing better and so easily.

Recommended Articles

This is a guide to Dangling Pointers in C. Here we discuss how Dangling Pointers Works in C along with programming examples to understand better. You may also have a look at the following articles to learn more –

  1. Patterns in C Programming
  2. C Literals
  3. C Programming Matrix Multiplication
  4. C Programming Interview Questions

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training Course Learn More