Dangling pointer

This is a discussion on Dangling pointer within the C Programming forums, part of the General Programming Boards category; Why does program runs....it should not since ptr is a dangling pointer Code: #include<stdio.h> int main( ) { int *ptr ...

  1. #1
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162

    Dangling pointer

    Why does program runs....it should not since ptr is a dangling pointer
    Code:
    #include<stdio.h>
    
    int main( ) 
    
    { 
    
    int *ptr ; 
    
    int *f( ) ; 
    
    ptr = f( ) ; 
    
    printf ( "%d", *ptr ) ; 
    
    return 0;
    } 
    
     
    
    int *f( )
    
    {
    
    int a = 10 ;
    
    return ( &a ) ;
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Undefined behavior unfortunately does include what one might expect to happen if the program were correct.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    variable a was allocated on the stack and function f() is returning a valid address to some location on the stack. The problem is that main() thinks (assumes) the address still points to a valid integer called a. The address is valid, but the contents of that address may or may not change -- and changes are pretty good that the contents of that address will change if there are any other functions calls in main() between f() and printf().

  4. #4
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    I tried adding a function between f() and printf()...now the contents of *ptr is different(Garbage Value)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    So what's your question?
    How you were lucky in the first example
    Or how you were unlucky in the 2nd example

    Dave's answer is spot on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    try this code out:
    Code:
    #include<stdio.h>
    void abc(void);
    int main( ) 
    
    { 
    
        int *ptr ; 
    
        int *f( ) ; 
    
        ptr = f( ) ; 
    
        abc();
        printf ( "%d", *ptr ) ; 
    
        return 0;
    } 
    
     
    
    int *f( )
    
    {
    
    int a = 10 ;
    
    return ( &a ) ;
    
    }
    
    void abc(){
        int a = 25;
        int b = 35;
    }
    output when i run it is:

    agarwaga@lc1:/u/agarwaga/c> gcc a6.c
    a6.c: In function `f':
    a6.c:27: warning: function returns address of local variable
    agarwaga@lc1:/u/agarwaga/c> ./a.out
    25agarwaga@lc1:/u/agarwaga/c>

    Automatic variables are made on the stack. Pointer is a dangling pointer after the function call is over. 25 is output in the program above since the call to abc function also makes an automatic variable which is in the same address as the earlier variable(in the previous function call) was .

    You can also see the compiler gives a warning also that you should not return the address of a local variable. Such a use is very bad programming practice and should never be done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21