Thread: destucting objects - correct way

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    87

    destucting objects - correct way

    I'd like to confirm should I do what I'm doing and is correct/advised.

    I've vector of objects:

    Code:
    std::vector<Microbiome*> diversity;
    
          Microbiome* temp_object=new Microbiome(domain,phyla,clas,order,family,genus,species); //create object
    
    diversity.push_back(temp_object);
    I could have used emplace and avoided usage of new.
    I've a function to delete Microbiome objects for this vector. I've destructor seating back in Microbiome class.

    Code:
    void Excel::delete_microb_obj(const std::vector<Microbiome*>& temp_diversity ){
    
      std::string poison_string="-Z"; //poison string
      for(std::vector<Microbiome *>::const_iterator it = temp_diversity.begin(); it!=temp_diversity.end() ; ++it){
    
        (*it)->set_domain(poison_string);
        (*it)->set_phyla(poison_string);
        (*it)->set_clas(poison_string);
        (*it)->set_order(poison_string);
        (*it)->set_species(poison_string);
        (*it)->set_genus(poison_string);
        delete *it;
      }
    }
    Is this correct to poison object attributes and delete?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    My first question is why are you using a pointer in the vector in the first place. Why not just use a "normal" non-pointer instance?

    Code:
    std::vector<Microbiome> diversity;
    My second question is why are you const qualifying that parameter in the second snippet?

    Jim
    Last edited by jimblumberg; 08-31-2015 at 10:52 AM.

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I could have used emplace and avoided usage of new.
    You clearly don't understand emplace then, or new, if you're thinking this.

    The emplace or emplace_back member function is an in-place construction of an object in the container's memory. Normally, when you use push_back, you allocate a temporary object and then copy it back to the container. This is why emplace_back is faster, it does a direct construction of the object at a location in memory.

    You using new() is you requesting that the kernel give you a region of memory from the heap equal to the size of the allocation requested. If the memory request it successful, the object is then constructed with the parameters you passed.

    Again, why are you trying to create a vector of pointers to an object instead of a vector of objects?

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    Quote Originally Posted by jimblumberg View Post
    My first question is why are you using a pointer in the vector in the first place. Why not just use a "normal" non-pointer instance?

    [/code]std::vector<Microbiome> diversity;[/code]

    My second question is why are you const qualifying that parameter in the second snippet?

    Jim
    Hi Jim,
    Thank you for your reply.

    1- I played around with it, no specific reason.
    Like I said, I could have used emplace with constructors. Also, I want to check if Microbiome object is already present with a specific diversity.

    2- I do not want the ownership of vector object.

    I need more guidance with these usages, I'm doing some random stuff. Kindly bear with me.

    Thanks.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I need more guidance with these usages
    My suggestion is to stay away from pointers whenever possible and when you do require pointers prefer one of the "smart" pointers. One of the reasons for using a vector is to avoid messy dynamic memory allocations.

    Also, I want to check if Microbiome object is already present with a specific diversity.
    What does this have to do with using nasty dynamic memory allocation. You'll still need to iterate through the vector to determine if there is a duplicate.

    Like I said, I could have used emplace with constructors.
    This still doesn't explain why you want to use a pointer in your vector. You can use emplace_back() without the pointer.

    Jim

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    The emplace or emplace_back member function is an in-place construction of an object in the container's memory. Normally, when you use push_back, you allocate a temporary object and then copy it back to the container. This is why emplace_back is faster, it does a direct construction of the object at a location in memory.
    Correction: Move it into the container. If the object cannot be moved, then it falls back to copying the object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Oh really? Interesting. Man, I'm a little out of touch with my C++ container internals. In that case, emplace_back() just seems a little silly now. What would make an object non-movable?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    emplace_back() is even more efficient. You don't have to construct a temporary and move it. You just construct it directly in the container's memory.
    As for what would make an object non-movable is if the move constructor/operator are deleted, inaccessible or cannot be compiler generated. As an example of an object that's non-movable, look no further than our local C++ I/O streams.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There should be a flowchart for this...

    To decide which option to use for storing items in a vector:
    Are the object lifetimes greater than the vector itself and the objects lifetimes are entirely managed by something else? Yes => use vector of raw pointers (rare in practice)
    Do the objects in the vector need shared ownership with other code? Yes => use vector of shared_ptrs
    Does the object in the vector need to be used polymorphically? Yes => Use vector of unique_ptrs.
    Are the objects very expensive to copy-construct, or assign? Yes => Possibly use vector of unique_ptrs. (Even then, the copying slowness has to outweigh the loss of cache coherence)
    All answers No => Use objects directly in the vector. (most common)
    Last edited by iMalc; 09-05-2015 at 05:56 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Oh, missed one other case above...
    Does the class prevent copy-construction & assignment, and move variants of these? Yes => Use vector of unique_ptrs.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encapsulation and objects within objects
    By nair in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2011, 10:42 AM
  2. Replies: 1
    Last Post: 05-24-2011, 06:36 PM
  3. Assigning objects to objects
    By Swordsalot in forum C++ Programming
    Replies: 4
    Last Post: 07-26-2006, 03:47 AM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. Objects, or pointers to objects?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2001, 12:57 AM

Tags for this Thread