Thread: lifespan of an object and a class + the object's identity

  1. #1
    My c++ skilz are 1336! :(
    Join Date
    Feb 2005
    Posts
    2

    lifespan of an object and a class + the object's identity

    I am new to making programs, although I can get around windows quite well. I am having a little bit of trouble trying to find out the lifespan of an object and a class. I looked on http://www.cprogramming.com/ , and I spent a couple of hours searching for this on google. While I was doing this search, I came across some websites that used "object's identity" quite a lot. Does anyone know what this means, or could you give me a website that explains the above?


    Thanks,

    SA_Max71
    Last edited by SA_Max71; 02-25-2005 at 12:02 AM. Reason: I forgot to add a few things...

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    lifespan of an object and a class.
    An object goes out of scope and is destroyed when the block it is declared in ends....unless you use the new operator to create the object. If you use the new operator, the object remains 'alive' after the block ends, and the object can be freely accessed until you use delete to 'kill' it.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    To expand:
    Variables have lifespans and scope. Lifespan tells you how long the object is in existance. Scope tells you where you can access the variable using it's name.

    Global / Filescope variables: They have a lifespan of the entire run of the program and a scope of the file in which it was declared ( using extern or by defining it )

    Local Variables: Local variables come into existance when they care declared and are destroyed when they go out of scope. Their scope is the block ( think { } ) they were declared in and any sub blocks. Going out of scope means that you leave the } (for the most part).

    Modifiers: By declaring a local variable static you change the lifetime to the entire run of the program. You do not however change it's scope.

    Dynamically allocated objects: A variable that is created using new exists from the moment the new was invoked until it's destroyed using delete. Since this object has no name it also has no scope. Any pointer that knows the object's type and location can reference the object.

    Note about global and static locals: These variables are created before main() is invoked and are destroyed after main exits.

  4. #4
    My c++ skilz are 1336! :(
    Join Date
    Feb 2005
    Posts
    2

    Unhappy Lets see if I understand you correctly....

    Variables have lifespans and scope. Lifespan tells you how long the object is in existance. Scope tells you where you can access the variable using it's name.
    So let’s see if I understand you correctly. The lifespan would be how long it takes for you to make your bike pedals to make a full turn (or revolution). The scope would be your brain; your brain tells your feet where the bike pedals are and how to use them.

    Dynamically allocated objects: A variable that is created using new exists from the moment the new was invoked until it's destroyed using delete. Since this object has no name it also has no scope. Any pointer that knows the object's type and location can reference the object.
    So lets say "new" is invoked. If this command was a bike, then someone has given you a brand spanking new bike. Then the delete command is used. At the same time the delete command is used, someone has stolen your bike and trashed it. (I don't know much about the delete command, but I think I have seen some threads on this, so I can read on what it does.)


    In case you were wondering, yes, I am new to programming in any language.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Post

    Quote Originally Posted by SA_Max71
    So let’s see if I understand you correctly. The lifespan would be how long it takes for you to make your bike pedals to make a full turn (or revolution). The scope would be your brain; your brain tells your feet where the bike pedals are and how to use them.
    Not quite. The lifespan would be how long it takes before you chain breaks. A "static" chain would last as long as the big does while a default chain would break every time you got off the bike.

    So lets say "new" is invoked. If this command was a bike, then someone has given you a brand spanking new bike. Then the delete command is used. At the same time the delete command is used, someone has stolen your bike and trashed it. (I don't know much about the delete command, but I think I have seen some threads on this, so I can read on what it does.)
    More like, when you new a bike someone loans your family a bike. As long as you know where the bike is you can get to it. When you delete the bike the person takes the bike back.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    I don't get the bike analogies, but 'delete' frees up the memory occupied by an object so that it is subject to reuse by your program. For example:
    Code:
    int* p = new int(10);
    cout<<*p<<endl;
    delete p;
    After using delete, the memory used to store the int is freed up. When your program ends, the memory it used will be automatically freed up anyway, so delete isn't strictly necessary all the time. If the next line in the code above was:

    return 0;

    then the delete is redundant. However it's good practice to always delete any memory you obtain using new.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by 7stud
    Hi,

    I don't get the bike analogies, but 'delete' frees up the memory occupied by an object so that it is subject to reuse by your program. For example:
    Code:
    int* p = new int(10);
    cout<<*p<<endl;
    delete p;
    After using delete, the memory used to store the int is freed up. When your program ends, the memory it used will be automatically freed up anyway, so delete isn't strictly necessary all the time. If the next line in the code above was:

    return 0;

    then the delete is redundant. However it's good practice to always delete any memory you obtain using new.
    Never EVER trust the OS to free allocated memory when the program exits.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Never EVER trust the OS to free allocated memory when the program exits.

    um... all allocated memory (using new) belongs to the process - not to the system.
    thus it should be freed by the system when the process is destroyed. unless its an bug.
    or am i mistaken?

    anyway its bad style not freeing used memory. it also makes code less reusable.
    assume you have a function that doesnt free up 1kb of memory - since you think: "well this function is run only once in my prog - so why bother freeing up 1kb?"

    later you want to embed that function in another project - which calls the function 1.000.000 times... then youve used up 100Mb of memory for nothing!!!
    signature under construction

Popular pages Recent additions subscribe to a feed