GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Tech FAQs  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 39 of 47    Print  
What is dangling pointer

  
Total Answers and Comments: 2 Last Update: October 30, 2009     Asked by: vijaymca 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sjappasab
 
pointers that do not point to a valid object of the appropriate type. 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. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer, as silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find, or cause segmentation faults (*NIX) or general protection faults (Windows).

Above answer was rated as good by the following members:
mohinuddinkhan
June 25, 2008 06:36:13   #1  
sjappasab Member Since: May 2008   Contribution: 1    

RE: What is dangling pointer
pointers that do not point to a valid object of the appropriate type. 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. As the system may reallocate the previously freed memory to another process if the original program then dereferences the (now) dangling pointer unpredictable behavior may result as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer as silent corruption of unrelated data may result leading to subtle bugs that can be extremely difficult to find or cause segmentation faults (*NIX) or general protection faults (Windows).
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
October 30, 2009 00:57:30   #2  
mohinuddinkhan Member Since: October 2009   Contribution: 2    

RE: What is dangling pointer
Let us understand through the following code snippet
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BLOCKSIZE 20;
int main()
{
char *ptr;
ptr (char *)malloc(BLOCKSIZE);
if(ptr! NULL)
strcpy(ptr "mohin khan");
printf("content s" ptr);
printf("naddress u" ptr);
free(ptr);
printf("nAfter free");
printf("ncontent s" ptr);
printf("naddress u" ptr);
}
output of the above code
content mohin khan
address 134520840 //(just a example)
After free
content
address 134520840 //same as the previous address

so we can conclude from the above code that
after free() also i keeps the address but content is deallocated.so now it is not pointing to any valid memory location.Hence ptr is now dangling pointer which probably having a address but not pointing to any valid memory location.
so it is good practice to assign NULL after freeing the allocated memory as below

ptr NULL;

in this way dangling problem will be solved.

 
Is this answer useful? Yes | No

 Related Questions

Latest Answer : Dangling pointer leads the memory leak problems. The following circumstances are memory leak ( dangling pointers ), not for good programming practice , try to avoid1) Try to avoid the empty object pointer creation2) Memory allocation without de allocation.3) ...
Read Answers (2) | Asked by : karibasi
Tags : Pointer

Latest Answer : Advantages:* Increasing the programming performance , Using pointers we can able to access the variables faster* Dynamic memory management * Data structure handling (Linkedlist, Stack ,Queue, Data dictionary,....)* File management using FILE pointer ...
Tags : Pointer

What is auto pointer ? What is the use of this ? 
Latest Answer : Auto pointer is nothing but, Proper memory management ( memory Allocation and deallocation).In C++ there is class template "auto_ptr" this is good example for auto pointer. Only we need to allocate a memory using the "auto_ptr" class ...
Tags : Pointer

Latest Answer : Let us understand through the following code snippet#include#include#include#define BLOCKSIZE 20;int main(){char *ptr;ptr=(char *)malloc(BLOCKSIZE);if(ptr!=NULL)strcpy(ptr,"mohin khan");printf("content=%s",ptr);printf("naddress=%u",ptr);free(ptr);printf("nAfter ...
Read Answers (2) | Asked by : vijaymca

How to reverse a linked list using only one pointer? 
Latest Answer : void revshow(){struct node *p;int temp[100],count=0,i;p=head;for(i=0;p!=NULL;i++,p=p->next)temp[i]=p->num;for(i=i-1,p=head;p!=NULL;i--,p=p->next)p->num=temp[i];} ...


 Sponsored Links

 
Related Articles

Concepts of Object-Oriented Programming

Object Oriented JavaScript In this chapter you ll learn about OOP Object Oriented Programming and how it relates to JavaScript As an ASP NET developer you probably have some experience working with objects and you may even be familiar with concepts such as inheritance However unless you re already a
 

C++ Void Pointer and Null Pointer

C Void Pointer and Null Pointer In this C tutorial you will learn about two interesting types of pointers; void pointers and Null Pointer These pointers will be discussed in conjunction with syntax usage and example mosgoogle center Pointer to Void General Syntax void pointer variable; Void is use
 

SQL Programming

SQL Programming Overview Anybody who has done something for a long time has probably wanted to change how things work at some point or another. A worker at a mill might have found a more efficient way of cutting logs, or a mathematics teacher might have had a hand in changing a school&rsquo;s al
 

WinRunner Programming Concepts

If you want to create WinRunner scripts that are highly efficient, there are important programming concepts that you will want to become familiar with. Understanding these concepts will provide you with a large number of key benefits. In addition to understanding these concepts, you must also learn
 

Programming Languages Certification

IT Certification programs have several options that will offer you the best knowledge.&nbsp; By learning everything that you need to know about information technology you will be able to open new doors to your career and personal business desires.&nbsp; IT Certification offers several vari
 

Neuro-linguistic Programming Methods

Neuro linguistic Programming Methods There are several methods used for performing Neuro linguistic Programming on an individual for obtaining insights into the psyche of the person in order to correct to modify certain patterns of behavior These techniques are also used for Neuro linguistic trainin
 

The Difficult Past of Neuro-linguistic programming

The Difficult Past of Neuro linguistic programming Neuro linguistic programming has had a rocky past with a number of lawsuits rivalry unsystematic development and intermittent progress During the 1980s the two founders separated after the lawsuit filed by Bandler Bandler went on to file several law
 

History of Neuro Linguistic Programming (NLP)

History of Neuro linguistic programming Neuro Linguistic Programming NLP is a system co founded by John Grinder and Richard Bandler for bringing about personal development in any individual by interpreting and translating the human perception experience beliefs and language It was developed in early
 

The Use of Access Specifiers In Object Oriented Programming

The Use of Access Specifiers In Object Oriented Programming In object oriented programming a variable will have a certain range This range can be defined as the place where it can be viewed In most cases there are two simple models that are used and these are lexically scoped and dynamically scoped
 

Class-based Object Oriented Programming

Class based Object Oriented Programming Class based object oriented programming is a style that gains inheritance by processing classes as objects The most prominent style of OOP is class based instead of object based With the class based OOP system objects are units that merge states identities and
 

About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape