Thread: allocating structs within STL vectors

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    7

    allocating structs within STL vectors

    Hi. I have a question related to memory allocation within STL vectors. I wonder if the way I am allocating memory is correct and if de-allocation is being handled.

    I am trying to use a vector of an object of my creation. There are pointers within the struct than need to be allocated. A simple example of what I'm trying to do follows:

    Code:
    struct my_struct
    {
    double *xyz;
    }
    
    vector<my_struct> my_vector;
    
    my_struct ms;
    ms.xyz = new double[xyz_size];
    
    my_vector.push_back(ms);
    Is this correct? I always thought you had to have a delete for every new. But in this case, I'm relying on the vector clean-up to deallocate the memory for double* xyz

    thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No, the vector only cleans up the memory it allocates. If you store dynamically allocated objects or objects that allocate memory dynamically, then freeing that is your responsibility.

    However, why does your stuct handle memory manually? Couldn't you simply make that double* a std::vector<double> too?

    If that is for some reason impossible, consider using constructors and destructors. If that struct had a destructor that would clean up after the instance, clearing up the vector would happen automatically.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    But in this case, I'm relying on the vector clean-up to deallocate the memory for double* xyz
    It does not do such a thing. It relies on my_struct to clean itself up, and my_struct just cleans up the pointers, not what they point to.

    The simple fix is to change xyz from a double* to a vector<double>. This time, the vector xyz can be relied on to deallocate the memory.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by aoiOnline View Post
    Hi. I have a question related to memory allocation within STL vectors. I wonder if the way I am allocating memory is correct and if de-allocation is being handled.

    I am trying to use a vector of an object of my creation. There are pointers within the struct than need to be allocated. A simple example of what I'm trying to do follows:

    Code:
    struct my_struct
    {
    double *xyz;
    }
    
    vector<my_struct> my_vector;
    
    my_struct ms;
    ms.xyz = new double[xyz_size];
    
    my_vector.push_back(ms);
    Is this correct? I always thought you had to have a delete for every new. But in this case, I'm relying on the vector clean-up to deallocate the memory for double* xyz

    thanks
    A vector (or any STL class) has no idea of what you're doing with your class, so it's certainly not going to delete any pointer it finds. If you allocate it, you need to delete it.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    Thanks very much guys for the instant answer. It's not impossible to use vector<double> but impractical in this case. Perhaps it's best to have a destructor as I think that would be the minimum code change.

    Thanks again.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You'll also need a copy constructor and overloaded assignment operator. Objects in vectors need to be copiable, and copying an object managing dynamic memory needs special care.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    Thanks. I hope I don't need an assignment operator for a double*. I'm rather sloppy when it comes to copy constructors and I try to pass variables as a reference as much as possible. Maybe it is best to use vector<double> after all.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I hope I don't need an assignment operator for a double*.
    You cannot define an assignment operator for the double*, but you should define one for my_struct if you do not turn the double* into a vector<double>.

    Maybe it is best to use vector<double> after all.
    Most likely yes. Otherwise you will be writing code for my_struct that probably duplicates the logic of a vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    Quote Originally Posted by laserlight View Post
    you should define one for my_struct if you do not turn the double* into a vector<double>.
    Gotcha. Thanks for helping me out and bearing with a fledgling programmer.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    A semi-related question about choosing between double* and vector<double>. I was under the impression that using vector<double> makes a code run slower than if double* is used. Is this true? (Using Microsoft Visual C++)

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by aoiOnline View Post
    A semi-related question about choosing between double* and vector<double>. I was under the impression that using vector<double> makes a code run slower than if double* is used. Is this true? (Using Microsoft Visual C++)
    Not necessarily. Read: Chapter 76. Use vector by default. Otherwise, choose an appropriate container

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The only place that xyz should be new'd is in the constructor, and it should be delete'd in the destructor. You are programming in a highly UN-object-oriented style here.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by aoiOnline View Post
    A semi-related question about choosing between double* and vector<double>. I was under the impression that using vector<double> makes a code run slower than if double* is used. Is this true? (Using Microsoft Visual C++)
    How the heck could adding a layer of indirection make code run FASTER? Yeah, a vector of double* is going to take up less SPACE than a vector of double, but the doubles themselves still exist somewhere so you're using MORE memory (you need values AND pointers to the values) and making it SLOWER to access them because you have to go through a pointer.

    And you get to futz with allocating and deallocating them. On the whole, a very dumb plan.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    Thanks for the replies.

    You are programming in a highly UN-object-oriented style here.
    I'm certain you are right. I'm not a programmer by training. I'm learning as I go.

    How the heck could adding a layer of indirection make code run FASTER? Yeah, a vector of double* is going to take up less SPACE than a vector of double, but the doubles themselves still exist somewhere so you're using MORE memory (you need values AND pointers to the values) and making it SLOWER to access them because you have to go through a pointer.
    Sorry for the confusion brewbuck. Either that or I don't understand your answer.

    I didn't mean a vector<double*> I meant using a vector<double> vs. double*

    I am writing a program that includes functions that must be lighting fast. I'm not knowledgeable in optimization techniques, so any up-front time savings help.

    Bottom line appears to be that I should use vector<double> instead of double*

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by aoiOnline View Post
    I am writing a program that includes functions that must be lighting fast. I'm not knowledgeable in optimization techniques, so any up-front time savings help.
    Up front time savings are a bad idea, because they can lead to hard to track bugs, particularly for the inexperienced.
    Last edited by King Mir; 12-05-2007 at 12:40 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL Vectors of Vectors
    By Beamu in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2008, 05:23 AM
  2. help using vectors with structs
    By Swordsalot in forum C++ Programming
    Replies: 15
    Last Post: 04-09-2008, 11:14 AM
  3. Array of Vectors amd other STL questions
    By kolistivra in forum C++ Programming
    Replies: 16
    Last Post: 04-12-2007, 09:11 AM
  4. STL Vectors
    By Da-Nuka in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2005, 08:35 PM
  5. STL list::sort() sorting structs
    By hk_mp5kpdw in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2002, 07:23 AM