Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical

    Extra Credit IQOD #2A

    Ok since we're on a roll i'm posting an open ended question here:

    Consider the following:
    Code:
    void MyClass::DoSomething()
    {
       //Some code here
       delete this;
    }
    What are your thoughts on this function? why?

  2. The Following 2 Users Say Thank You to B1ackAnge1 For This Useful Post:

    Hell_Demon (09-18-2009),zeco (09-18-2009)

  3. #2
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by B1ackAnge1 View Post
    Ok since we're on a roll i'm posting an open ended question here:

    Consider the following:
    Code:
    void MyClass::DoSomething()
    {
       //Some code here
       delete this;
    }
    What are your thoughts on this function? why?
    slightly less confused:
    I forgot 'this' means it is pointing to itself(the memory where the class is allocated after using new)
    therefor this function would be a cleanup function amiright?

    MyClass *testing = new MyClass; //allocate new memory for our class to put its shizzle in
    //perform some random crap untill we no longer need it
    //done performing random crap so lets clean up
    testing->DoSomething(); //delete the allocated memory


    edit:
    ok wtf...
    your post as it was before is in my quote, and now it says HI ARUN2 and HI ARUN1 o__O but it does not say 'last edited by' or anything this is scary :P

    edit2:
    HI ARUN1 dissappeared(it was at the end of the post) and the other one is still there
    Last edited by Hell_Demon; 09-18-2009 at 02:54 PM.

  4. #3
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    huh? I did NOT put that in...

  5. #4
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by B1ackAnge1 View Post
    huh? I did NOT put that in...
    weird, i found some more posts containing HI ARUN2 but 1 is rare ;o
    btw was my answer correct?

  6. #5
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    hmm you sorta follow what's going on but is that good or bad?
    say you had a 3rd party DLL with that method in it that you have to call . Would you know it did that? Should it do that? why/why not?

    Let me show something that would be a typical scenario..
    Code:
    MyClass* pClass = new MyClass;
    ...Do Stuff on pClass
    pClass->DoSomething();
    ...Do Stuff on pClass
    delete pClass;
    What do you think about it now? Do you see any potential problems?
    Last edited by B1ackAnge1; 09-18-2009 at 03:12 PM.

  7. #6
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by B1ackAnge1 View Post
    hmm you sorta follow what's going on but is that good or bad?
    say you had a 3rd party DLL with that method in it that you have to call . Would you know it did that? Should it do that? why/why not?
    /me changed status into confused again.

  8. #7
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    Updated my post above yours with a sample that should more clearly show the issue

  9. #8
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    dosomething deletes the allocated memory therefor making the pointer point to NULL, after that you delete it again which will result into a problem im afraid.

    edit: doing stuff after dosomething will also result in problems since its no longer there.

  10. #9
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    Correct - other valid answers would have been:
    Find the guy who wrote that and beat'm

    So this is something you NEVER want to do and is VERY BAD practice

  11. #10
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by B1ackAnge1 View Post
    Correct - other valid answers would have been:
    Find the guy who wrote that and beat'm

    So this is something you NEVER want to do and is VERY BAD practice
    woot i win another cookie :d yay

  12. #11
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    Shoudl start keeping track of scores

  13. #12
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by B1ackAnge1 View Post
    Shoudl start keeping track of scores
    what does the winner(me ofcourse :P) get? ^^
    Lifetime VIP on cookies i hope or a job as imontheothersideoftheworldcoder

  14. #13
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    It looks like a Deconstructor because it deletes the pointer to the object that it was called on. So in essence it deletes the object? I'm guessing you can't have two deconstructors in an object. What would happen?

    Yeh I'm seeing the little Hi Arun thing too o_O its weird ...
    Last edited by why06; 09-18-2009 at 03:39 PM.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  15. #14
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by why06 View Post
    It looks like a Deconstructor because it deletes the pointer to the object that it was called on. So in essence it deletes the object? I'm guessing you can't have two deconstructors in an object. What would happen?

    Yeh I'm seeing the little Hi Arun thing too o_O its weird ...
    make your text white... NOW!

  16. #15
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    Lets see:
    <!--The function seems to do stuff, as indicated by the comment, and then delete the object that the function was called with.-->

    By the way, i already read the question about why it's bad you posted to HD, and my answer is this is bad because the unsuspecting user might try to delete his pointer to the class that he dynamically allocated after he has already used that method. Which would be bad because you should never try to delete the same allocated memory twice.

    Btw, BlackAngel, These supremely rock. Not only is this something for the C++ board community to do, but it is also telling us things that are expected of us to know, if we ever want a C++ programming job. Even if i get it completely wrong, that is good because i end up learning even more.
    Last edited by zeco; 09-18-2009 at 04:21 PM.

  17. The Following 2 Users Say Thank You to zeco For This Useful Post:

    B1ackAnge1 (09-18-2009),Hell_Demon (09-19-2009)

Page 1 of 2 12 LastLast

Similar Threads

  1. Extra Credit [FUCK MY TEACHER]
    By GOD in forum General
    Replies: 2
    Last Post: 10-30-2008, 04:37 AM
  2. Some Extra Adresses!
    By ltkort213 in forum WarRock - International Hacks
    Replies: 17
    Last Post: 10-08-2007, 08:47 AM
  3. Credit FOr uploaded files...
    By Ov3nCleaner in forum Suggestions, Requests & General Help
    Replies: 6
    Last Post: 02-10-2006, 01:41 PM

Tags for this Thread