Home » Ask & Discuss » Board Exams - CBSE, ICSE, State Boards » Computer Science « Back to Discussion



Computer Science

New kid on the Block

Joined: 5 May 2010
Post: 25
19 May 2010 21:18:16 IST
0 People liked this
4
723 View Post
What is a dangling pointer?
None

What is a dangling pointer?


Share this article on:

Comments (4)

Deevita Agarwal's Avatar

Scorching goIITian

Joined: 6 Dec 2009
Posts: 290
20 May 2010 20:45:25 IST
1 people liked this

Dangling pointers are 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.

bladeX -rise of a new sun's Avatar

Blazing goIITian

Joined: 12 Dec 2007
Posts: 1249
20 May 2010 20:59:13 IST
1 people liked this

A dangling pointer is a cause a many problem in C language specially segmentation fault error. Ex:

int* p;

{

int q;

p=&q;

}

/* in this place you cannot access c as now the memory space containing the value of c is now no longer in use but p still points to the same place and so we say that p is a dangling pointer.That is p is pointing to a place which has been reallocated for some other purposes.*/

Another example which i can think of is :

int *p,*q;

p=(int*)malloc(sizeof(int));

q=p; /*p and q points to the same chunk of memory*/

free(p);/* now we free p so p points to NULL but q points to the same place so q is a dangling pointer*/

In Language like Java there is the concept of garbage collection etc. which tackles this if you are a C learner than always free memory after use.

Suresh Kumar's Avatar

Forum Expert
Joined: 24 Aug 2009
Posts: 1342
20 May 2010 21:46:16 IST
0 people liked this

 hello dear

A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning  addresses of the automatic variables from a function or using the address of the memory block after it is freed. The following code snippet shows this: 
class Sample 

public: 
int *ptr; 
Sample(int i) 

ptr = new int(i); 


~Sample() 

delete ptr; 

void PrintVal() 

cout << ?The value is ? << *ptr; 

}; 

void SomeFunc(Sample x) 

cout << ?Say i am in someFunc ? << endl; 



int main() 

Sample s1 = 10; 
SomeFunc(s1); 
s1.PrintVal(); 


In the above example when PrintVal() function is called it is called by the pointer that has been freed by the destructor in SomeFunc.


New kid on the Block

Joined: 11 Jul 2011
Posts: 3
11 Jul 2011 10:54:43 IST
0 people liked this

Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. These are special cases of memory safty violations.

Cegonsoft




Quick Reply


Reply

Some HTML allowed.
Keep your comments above the belt or risk having them deleted.
Signup for a avatar to have your pictures show up by your comment
If Members see a thread that violates the Posting Rules, bring it to the attention of the Moderator Team
Free Sign Up!

Preparing for IIT-JEE ?

Arihant Revision Package for IIT JEE - Books, Practice Tests + Rank Predictor


@ INR 1,995/-

For Quick Info

Name

Mobile No.

Find Posts by Topics

Physics.

Topics

Mathematics.

Chemistry.

Biology

Parents

Board

Fun Zone

Sponsored Ads