Causes of Dangling Pointer in C PDF
Causes of Dangling Pointer in C PDF
Causes of Dangling Pointer in C PDF
Subscribe
Home About C Programs Write for Us Java C++ JavaS cript MCQ Civil S ervices Easy Tips
Dangling Pointer in C 1. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. 2. In Short Pointer Pointing to Non-Existing Memory Location is called Dangling Pointer. There are different ways where Pointer acts as Dangling Pointer. Way 1 :Using Free / De allocating Memory
# i n c l u d e < s t d l i b . h > { c h a r* p t r=m a l l o c ( C o n s t a n t _ V a l u e ) ; . . . . . . . . . . . . . . . . . . . . .
f r e e( p t r ) ; }
/ *p t rn o wb e c o m e sad a n g l i n gp o i n t e r* /
1. 2. 3. 4.
Character Pointer is Declared in the first Step. After some statements we have deallocated memory which is previously allocated. As soon as Memory is Deallocated Pointer is now Dangling. Problem : If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.
After Deallocating memory Initialize Pointer to NULL so that pointer is now no longer dangling . Initializing Pointer Variable to NULL Value means Now Pointer is not pointing to anywhere Way 2 :Out of Scope
# i n c l u d e < s t d l i b . h > v o i dm a i n ( ) { c h a r* p t r=N U L L ; . . . . . . . . . . { c h a rc h ; p t r=& c h ; } . . . . . / *d pi sn o wad a n g l i n gp o i n t e r* / }
1. Character Pointer is Declared in the first Step. 2. Pointer Variable ptr is pointing to Character Variable ch declared in the inner block . 3. As character variable is non-visible in Outer Block , then Pointer is Still Pointing to Same Invalid memory location in Outer block , then Pointer becomes Dangling Way 3 : Function Call
i n t*f u n c(v o i d)
{ i n tn u m=1 4 ; / *. . .* / r e t u r n& n u m ; }
Attempts to read from the pointer may still return the correct value (1234) for a while after calling func, but any functions called thereafter will overwrite the stack storage allocated for num with other values and the pointer would no longer work correctly. If a pointer to num must be returned, num must have scope beyond the function it might be declared as static.
Previous sub topic Next sub topic Exercise : Basic Concept of Pointer What is Pointer ? Pointer Address Operator Memory Organization Memory Requirement Size of Pointer variable Pointer Operator Pointer Declaration Initialization of Pointer Dereferencing Pointer Void Pointers Dereferencing Void Pointer Size of Void Pointer Arithmetic Operations Incrementing Pointer Variable
Pointer Addition Pointer Arithmatics Subtracting Integer Value From Pointer Subtracting Pointers Comparison of Pointers Rules - Pointer Operation Illegal Arithmetic Operations Precedence of ' * ' and ' & ' Meaning of (*++ptr) Meaning of (++*ptr) Difference between *ptr++ and ++*ptr Are ++*ptr and (*ptr)++ are same ? Double Pointer Pointer to Pointer Pointer to Constant Objects Constant Pointers Pointer to Array Pointer to Array of String Function Pointer #1 Function Pointer #2 Pointer to Array of function Size of Const Pointer Accessing Integer using char pointer What is Null Pointer ? char *a Vs char a[] Reading Complex Pointer Common Pointer Mistakes Causes of Dangling Pointer in C Application of Pointer Question 1 Question 2 Question 3 Question 4 Pointer in C
C Programming 1. History of C 2. Overview of C 3. Language Type 4. Comments 5. Variables 6. Constants 7. Data Types 8. Operators 9. Expressions 10. If-else 11. Loops 12. Array Index 13. Functions 14. Pointer 15. Storage Classes 16. Strings 17. Preprocessor 18. Bitwise Operator 19. Structure 20. Unions 21. File Handling Advance Topics
Chapter 1 : Data Structure Using C Chapter 2 : 200+ C Programs Chapter 3 : 300+ MCQs on C Subscribe Us
Submit
We're on
Follow
+222
Recent in C Programming Typedef Defining Type Expressions in C Constants in C Comments in C Types of Languages in C File Handling in C Union in C Structure in C Bitwise Operator in C Preprocessor in C
2009-2013 Programming Tutorials. The content is copyrighted to Pritesh Taral and may not be reproduced on other websites.