Question regarding scope and lifetime of variable

Discussion in 'C Programming' started by somenath, Dec 24, 2007.

  1. somenath

    somenath Guest

    Hi All ,

    I have one question regarding scope and lifetime of variable.

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
    int *intp = NULL;
    char *sptr = NULL;
    {
    int i =5;
    char *s = "hello";
    intp = &i;

    sptr = s;

    }
    printf("value of i = %d\n",*intp);
    printf("value of sptr = %s \n",sptr);

    return 0;
    }
    Out put of the program

    value of i = 5
    value of sptr = hello
    The variable i, and s has block scope .But as s points to string
    literal life time of the s is through out the program .
    My question is as i has only block scope so once it goes out of scope
    address of i becoming meaning less. So is it guaranteed that first
    printf statement will always print 5 ?
    Regards,
    Somenath
     
    somenath, Dec 24, 2007
    #1
    1. Advertising

  2. somenath

    Ian Collins Guest

    somenath wrote:
    > Hi All ,
    >
    > I have one question regarding scope and lifetime of variable.
    >
    > #include <stdio.h>
    >
    > int main(int argc, char *argv[])
    > {
    > int *intp = NULL;
    > char *sptr = NULL;
    > {
    > int i =5;
    > char *s = "hello";
    > intp = &i;
    >
    > sptr = s;
    >
    > }
    > printf("value of i = %d\n",*intp);
    > printf("value of sptr = %s \n",sptr);
    >
    > return 0;
    > }
    > Out put of the program
    >
    > value of i = 5
    > value of sptr = hello
    > The variable i, and s has block scope .But as s points to string
    > literal life time of the s is through out the program .
    > My question is as i has only block scope so once it goes out of scope
    > address of i becoming meaning less. So is it guaranteed that first
    > printf statement will always print 5 ?


    It prints whatever is at the location is points to contains. The result
    is undefined if the pointer points to a variable that is out of scope.

    Your example only works because there isn't any intervening code between
    the end of the block scope and the printf.

    --
    Ian Collins.
     
    Ian Collins, Dec 24, 2007
    #2
    1. Advertising

  3. somenath

    somenath Guest

    Re: Question regarding scope and lifetime of variable

    On Dec 24, 10:54 am, Ian Collins <> wrote:
    > somenath wrote:
    > > Hi All ,

    >
    > > I have one question regarding scope and lifetime of variable.

    >
    > > #include <stdio.h>

    >
    > > int main(int argc, char *argv[])
    > > {
    > >     int *intp  = NULL;
    > >     char *sptr = NULL;
    > >     {
    > >    int i =5;
    > >    char *s = "hello";
    > >    intp = &i;

    >
    > >    sptr  = s;

    >
    > >     }
    > >     printf("value of i     = %d\n",*intp);
    > >     printf("value of sptr  = %s \n",sptr);

    >
    > >     return 0;
    > > }
    > > Out put of the program

    >
    > > value of i     = 5
    > > value of sptr  = hello
    > > The variable  i, and s   has block scope .But  as s points to string
    > > literal life time of the s is through out the program .
    > > My question is as i has only block scope so  once it goes out of scope
    > > address of i becoming meaning less. So is it guaranteed that  first
    > > printf statement will always print 5 ?

    >
    > It prints whatever is at the location is points to contains.  The result
    > is undefined if the pointer points to a variable that is out of scope.
    >
    > Your example only works because there isn't any intervening code between
    > the end of the block scope and the printf.


    I would like to get bit more clarity .Please look at the updated
    program bellow.

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
    int *intp = NULL;
    char *sptr = NULL;
    {
    int i =5;
    char *s = "hello";
    intp = &i;

    sptr = s;

    }
    {
    int y= 20;
    printf("\n Value of y = %d \n",y);
    }
    printf("value of i = %d\n",*intp);
    printf("value of sptr = %s \n",sptr);


    return 0;
    }
    So are you indicating that *intp will not print 5 ?
     
    somenath, Dec 24, 2007
    #3
  4. Re: Question regarding scope and lifetime of variable

    On Sun, 23 Dec 2007 22:06:52 -0800, somenath wrote:
    > On Dec 24, 10:54 am, Ian Collins <> wrote:
    >> somenath wrote:
    >> > Hi All ,

    >>
    >> > I have one question regarding scope and lifetime of variable.

    >>
    >> > #include <stdio.h>

    >>
    >> > int main(int argc, char *argv[])
    >> > {
    >> >     int *intp  = NULL;
    >> >     char *sptr = NULL;
    >> >     {
    >> >    int i =5;
    >> >    char *s = "hello";
    >> >    intp = &i;

    >>
    >> >    sptr  = s;

    >>
    >> >     }
    >> >     printf("value of i     = %d\n",*intp); printf("value of sptr  =
    >> >     %s \n",sptr);

    >>
    >> >     return 0;
    >> > }
    >> > Out put of the program

    >>
    >> > value of i     = 5
    >> > value of sptr  = hello
    >> > The variable  i, and s   has block scope .But  as s points to string
    >> > literal life time of the s is through out the program . My question
    >> > is as i has only block scope so  once it goes out of scope address of
    >> > i becoming meaning less. So is it guaranteed that  first printf
    >> > statement will always print 5 ?

    >>
    >> It prints whatever is at the location is points to contains.  The
    >> result is undefined if the pointer points to a variable that is out of
    >> scope.
    >>
    >> Your example only works because there isn't any intervening code
    >> between the end of the block scope and the printf.

    >
    > I would like to get bit more clarity .Please look at the updated program
    > bellow.
    >
    > [...]
    > So are you indicating that *intp will not print 5 ?


    No, he's saying printing *intp may or may not print 5, in both the
    original code, and your changed code. "The result is undefined". There's
    an error in your program. This error can't usually be detected at compile
    time, and for that reason doesn't cause your compiler to emit an error
    message, but the code is still broken.
     
    Harald van Dijk, Dec 24, 2007
    #4
  5. somenath

    Richard Bos Guest

    somenath <> wrote:

    > #include <stdio.h>
    >
    > int main(int argc, char *argv[])
    > {
    > int *intp = NULL;
    > char *sptr = NULL;
    > {
    > int i =5;
    > char *s = "hello";
    > intp = &i;
    >
    > sptr = s;
    >
    > }
    > printf("value of i = %d\n",*intp);
    > printf("value of sptr = %s \n",sptr);
    >
    > return 0;
    > }
    > Out put of the program
    >
    > value of i = 5
    > value of sptr = hello
    > The variable i, and s has block scope .But as s points to string
    > literal life time of the s is through out the program .


    Correct.

    > My question is as i has only block scope so once it goes out of scope
    > address of i becoming meaning less. So is it guaranteed that first
    > printf statement will always print 5 ?


    No. It has undefined behaviour. If it _does_ print a value, the most
    likely is obviously 5, but it would be risky to rely on that. For
    example, it's also quite allowed to crash with a "stack top violation"
    error.

    Richard
     
    Richard Bos, Dec 24, 2007
    #5
  6. somenath

    Eric Sosman Guest

    Ian Collins wrote:
    > somenath wrote:
    >> Hi All ,
    >>
    >> I have one question regarding scope and lifetime of variable.
    >>
    >> #include <stdio.h>
    >>
    >> int main(int argc, char *argv[])
    >> {
    >> int *intp = NULL;
    >> char *sptr = NULL;
    >> {
    >> int i =5;
    >> char *s = "hello";
    >> intp = &i;
    >>
    >> sptr = s;
    >>
    >> }
    >> printf("value of i = %d\n",*intp);
    >> printf("value of sptr = %s \n",sptr);
    >>
    >> return 0;
    >> }
    >> Out put of the program
    >>
    >> value of i = 5
    >> value of sptr = hello
    >> The variable i, and s has block scope .But as s points to string
    >> literal life time of the s is through out the program .
    >> My question is as i has only block scope so once it goes out of scope
    >> address of i becoming meaning less. So is it guaranteed that first
    >> printf statement will always print 5 ?

    >
    > It prints whatever is at the location is points to contains. The result
    > is undefined if the pointer points to a variable that is out of scope.


    No, that's not the problem. It is perfectly legal to
    use a pointer to an out-of-scope variable, and in fact it
    is quite usual to do so. "Scope" refers to the visibility
    of the variable's identifier, not to the variable's existence.

    The undefined behavior arises when the pointed-to variable's
    lifetime ("storage duration") has ended. In somenath's example
    this occurs at the same point in the source where the identifier
    goes out of scope, and the fact that the two things happen at
    the same point seems to have led to some confusion about which
    causes what. They are, however, distinct notions -- and since
    the question was specifically about the two notions it is as
    well to clear up whatever confusion arises.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 24, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bucrepus

    Variable Lifetime

    bucrepus, Apr 14, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    959
    bruce barker
    Apr 14, 2004
  2. Jason Heyes
    Replies:
    12
    Views:
    557
    Branimir Maksimovic
    Nov 24, 2004
  3. Jason Heyes
    Replies:
    6
    Views:
    295
    Jason Heyes
    Nov 24, 2004
  4. David Filmer
    Replies:
    19
    Views:
    267
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    208
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page