Thread: Need help with using reserve for vector of objects in C++

  1. #1
    Registered User
    Join Date
    Jul 2009

    Need help with using reserve for vector of objects in C++

    Hello everyone,

    I am still learning C++ and trying to use reserve for vector in my project. In fact I am just trying to solve the memory problem and so I am guessing that I can possibly use reserve.

    I have vector that has elements of type class. each element is class. I have another vector that is member of that class and the elements of this another vector are pointer to other elements of first vector (pointer to class). Currently I have both of this vectors grows dynamically and don't have any size declared to it.

    It doesn't work rightnow. It works for small input but for big input, it has std:bad_alloc. I am not sure what is wrong, but I think, I can reserve big enough size for my outer (first) vector, so that in any case it will not need to reallocate as I am using pointer to the elements and so if it is reallocating, it will invalidate the addresses. I am not sure if I am right here but I have spend so much time but still doesn't work and so I think I can try to reserve either inner or outer vector.

    I have my vectors defined like this,

    std::vector<ROB_entry> ROB;
    typedef std::vector<ROB_entry*> dependency_list_t;

    With above use of reserve, I have an error like this,

    error: ISO C++ forbids declaration of ‘ROB’ with no type
    smt.h:77: error: expected ‘;’ before ‘.’ token

    If anybody knows what is wrong with my implementation of reserve, Please let me know. Any help will be greatly appreciated.

    Thank You.


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Post the smallest and simplest program that demonstrates the error.
    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

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Unless you can prove there is a good reason for using a second vector of pointers rather than indexes to the first vector, then My thoughts are that you should be using indexes instead.
    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. Objects as attributes of other objects.
    By kbro3 in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2009, 03:46 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM