Thread: life time of variable ?

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    90

    life time of variable ?

    Hello Everyone

    I am new to this forum. I am sorry if this question is already discussed on forum but I really don't understand clearly life time of variable.

    Code:
    #include <stdio.h>
    int main(void)
     {  // start scope1 
       int i = 2;
      for (int  j = 0; j < 5; ++j)
    
    
          { //start scope2
             
             // j can be only accessible within scope 2
             
             printf("Hello \n");
          }// End scope2
          
      printf("%d", i);  // I can accessible within scope 1 and scope 2
    
    
      return 0;
    }//  End scope1
    I don't understand how variable create and destroy regrades memory ?
    Last edited by Player777; 12-01-2019 at 12:07 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    int main(void)
    {  // start scope1 
      int i = 2;  // i begins at this ;
      for (int  j = 0;/* j begins at this ;*/ j < 5; ++j)
         { //start scope2
             // j can be only accessible within scope 2
             printf("Hello \n");
          }// End scope2
      printf("%d", i);  // I can accessible within scope 1 and scope 2
    
      return 0;
    }//  End scope1
    Variables begin their semantic existence at the ; following their declaration, and end at the closing brace of the scope block they're contained in.

    > I don't understand how variable create and destroy regrades memory ?
    Nor is there really any need to - at least in the short term.

    The compiler is free to turn your code into this.
    Code:
    #include <stdio.h>
    int main(void)
    {
      int j;
      int i = 2;
      for (j = 0; j < 5; ++j)
        {
          printf("Hello \n");
        }
      printf("%d", i);  // I can accessible within scope 1 and scope 2
      return 0;
    }
    Now j has a physical lifetime which is the same as i.
    But the compiler will enforce the rules to make sure you can only access j when you're allowed to.

    Yes, if you're doing some really low level debugging, it might be useful to know that is the case.
    But that knowledge is really only 'true' for that particular program compiled with one specific compiler.

    You certainly can't assume it's true for all compilers, and it's definitely not something you would assume in the code by trying to write something sneaky to access variables outside of their declared scope.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by Salem View Post

    Variables begin their semantic existence at the ; following their declaration, and end at the closing brace of the scope block they're contained in..
    It means local variable is created when the function first starts, and stays alive until the function exits.

    Global variable is created when the program first starts, and stays alive until the program exits.

    We can access local variable withing scope where as we can access global variable inside or outside the program.

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    > It means local variable is created when the function first starts, and stays alive until the function exits.

    Not exactly. Local scope variables are not created when the function starts. They are created when "you" declare them. If you have a variable declared in the last line in your function, it'd be created when the compiler reaches that line, and not when your function first starts. And yes, it stays alive until the end of the function i.e. its lifetime is the scope of that function. This is typically the case for statically allocated variables. When you learn about dynamic allocation, you'll learn that the lifetime of a dynamically allocated variable is decided by you.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Zeus_ View Post
    This is typically the case for statically allocated variables.
    Err, that's not exactly true either. A static object has a lifetime that extends beyond the lifetime of the function. E.g.

    Code:
    #include <stdio.h>
    
    int *fn (void)
    {
         static int foo = 0;
         foo++;
         return &foo;
    }
    
    int main (void)
    {
        int *bar;
        int i;
    
        for (i = 0; i < 10; i++) {
             bar = fn();
             printf("object of type int at 'address' %p is %d\n", bar, *bar);
        }
        return 0;
    }
    The lifetime of foo (in the function called fn()) exists beyond the function (in fact its lifetime is the same as the lifetime of the program as a whole). Perhaps you have misunderstood what "statically allocated" means?

  6. #6
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    My bad, I meant to say stack allocated. Yeah, static variables have the lifetime same as that of the entire program. I'll edit my post above for the typo error.

    Edit: I don't have edit permissions for messages so I can't edit the old message. What I meant was this:

    "This is typically the case for stack allocated variables."
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  7. #7
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    My bad, I meant to say stack allocated. Yeah, static variables have the lifetime same as that of the entire program. I'll edit my post above for the typo error.

    Edit: I don't have edit permissions for messages so I can't edit the old message. What I meant was this:

    "This is typically the case for stack allocated variables."
    Ignore this. This is just me being stupid. @Hodor, you're completely right. With the exception of static objects, it would be right saying "typically the case for statically allocated variables". I was on the bus when I replied to your message and got "statically allocated" i.e. allocation at compile time mixed with static variables. It's a mess - what I was thinking - so you probably don't wanna know lol but thanks anyway for pointing that out.

    Also, why can I not just be able to edit older messages...
    Last edited by Zeus_; 12-01-2019 at 08:51 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 06:29 PM
  2. scoped lock and object life time
    By pheres in forum C++ Programming
    Replies: 0
    Last Post: 01-22-2008, 02:25 AM
  3. Variable life cycle
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 12-27-2007, 08:28 AM
  4. system.map create/modify during linux life-time
    By pavmarc in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2005, 09:59 PM
  5. A lesson in life kids: Bed time is for your own good!
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-06-2003, 02:27 PM

Tags for this Thread