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

This is a discussion on Need help with using reserve for vector of objects in C++ within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am still learning C++ and trying to use reserve for vector in my project. In fact I ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    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;
    ROB.reserve(10000);
    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.

    Mira.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Post the smallest and simplest program that demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Location
    New Zealand
    Posts
    6,307
    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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21