| | |
vector trouble
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
Probably, you can check it out pretty easy.
Be careful of the word size. size is frequently used to indicate the actual number of elements currently in the vector as opposed to the number of elements for which space is allocated for vector use, which might be called capacity or size.
Be careful of the word size. size is frequently used to indicate the actual number of elements currently in the vector as opposed to the number of elements for which space is allocated for vector use, which might be called capacity or size.
Klatu Barada Nikto
Fortunately you CAN'T invent a "class of no designated size" in C++. For every class its objects have fixed and defined size (remember operator sizeof).
If you mean a class with dynamically allocated members then it's a problem of this class constructor, not a vector container. That's why class constructors were invented.
If you mean a class with dynamically allocated members then it's a problem of this class constructor, not a vector container. That's why class constructors were invented.
If you are using std::vector class then probable causes are:
1. You reserve vector size then try to access its elements, but reserve member function does not construct vector elements. You need resize (not reserve) vector if you want to access new elements immediatly.
2. Your class has bad default constructor (constructor without arguments). For example:
New Bad objects created after vector<Bad>::resize(k) are true bad. There are garbage values of pDynArr member in all 10 vector elements. The destructor of troubles calls Bad destructor for every 10 elements, the last one call delete [] pDynArr for undefined pointer.
Follow MosaicFuneral's advice
...
1. You reserve vector size then try to access its elements, but reserve member function does not construct vector elements. You need resize (not reserve) vector if you want to access new elements immediatly.
2. Your class has bad default constructor (constructor without arguments). For example:
C++ Syntax (Toggle Plain Text)
class Bad { Bad() {} // Forgot to initialize pDynArr with 0. Bad(const char* p, int n) { pDynArr = new char[n]; } ~Bad() { delete [] pDynArr; private: char* pDynArr; }; ... std::vector<Bad> troubles; ... troubles.resize(10); ...
Follow MosaicFuneral's advice
... Last edited by ArkM; Nov 19th, 2008 at 2:13 am.
![]() |
Similar Threads
- GUI, matrix and vector libraries (C++)
- help:stl vector of user defined objects (C)
- Trouble with Pointers and Arrays (C)
Other Threads in the C++ Forum
- Previous Thread: sub proceedure/function Qs
- Next Thread: Multi Client server
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






