stdvector

How does std::vector copy objects to its internal storage

三世轮回 提交于 2019-12-24 02:27:08
问题 I have the following problem: void add(){ cRow Row(); Row.add("Column", "Value"); std::vector<cRow> mRows; mRows.push_back(Row); } cRow::cRow(): mCol(NULL), mVal(NULL) { } cRow::add(const char* Col, const char* Val){ mCol = strdup(Col); mVal = strdup(Val); } cRow::~cRow(){ free(mCol); free(mVal); } After adding the local variable Row to the vector, the destructor is called for that Row and the strings are freed. Obviously, the pointers to the strings of the stored row in the vector are now

C++ slows over time reading 70,000 files

ε祈祈猫儿з 提交于 2019-12-24 02:06:05
问题 I have a program which needs to analyze 100,000 files spread over multiple filesystems. After processing around 3000 files it starts to slow down. I ran it through gprof, but since the slow down doesn't kick in until 30-60 seconds into the analysis, I don't think it tells me much. How would I track down the cause? top doesn't show high CPU and the process memory does not increase over time, so I/O? At top level, we have: scanner.init(); // build a std::vector<std::string> of pathnames.

How can I tell lint to track a custodial pointer to a vector?

扶醉桌前 提交于 2019-12-24 00:18:47
问题 I have some code that loops and news up some pointers and stores them in a vector: std::vector<InputBox*> m_octets; ... InputBox* octet = new InputBox(rect, title, touch_num); m_octets.push_back(octet); In the class destructor I for_each over m_octets and invoke the destructor for each pointer. I think this is all good. It all compiles and the unit tests pass. The problem is Gimpel's PC-lint doesn't like it. It sees that `octet' is a custodial pointer that has not been freed (Warning 429). I

push_back to a Vector

本秂侑毒 提交于 2019-12-23 18:45:34
问题 I have a weird problem. I have a vector that I would like to push objects on to like so: vector<DEMData>* dems = new vector<DEMData>(); DEMData* demData = new DEMData(); // Build DEMDATA dems->push_back(*demData); There will be a few hundred DEMData objects in the vector. The problem is when this code is finished, all items are equal to the last item "pushed back" to the vector? Why are the other objects being overridden in the vector? edit: The DemData class is simple, just a data structure

Why is vector::push_back of local variable not move optimized? [duplicate]

我的未来我决定 提交于 2019-12-23 09:49:52
问题 This question already has answers here : Can compiler generate std::move for a last use of lvalue automatically? (3 answers) Do compilers automatically use move semantics when a movable object is used for the last time? (1 answer) Closed 2 years ago . In C++11 you can use std::vector::push_back in combination with std::move to avoid copies while inserting elements into a vector. Is there a section in the standard that forbids compilers to use std::move automatically with local variables that

Why is this function producing incorrect values? [duplicate]

空扰寡人 提交于 2019-12-23 09:37:17
问题 This question already has answers here : Why is the std::accumulate function showing the wrong sum of a vector<double>? (4 answers) Closed last year . I have a simple function template to calculate the average value of a container: template<typename T> T array_average( std::vector<T>& values ) { if( std::is_arithmetic<T>::value ) { if( !values.empty() ) { if( values.size() == 1 ) { return values[0]; } else { return (static_cast<T>( std::accumulate( values.begin(), values.end(), 0 ) ) / static

Use std::vector::data after reserve

倖福魔咒の 提交于 2019-12-23 07:03:20
问题 I have a std::vector on which I call reserve with a large value. Afterwards I retrieve data() . Since iterating data is then crashing I am wondering whether this is even allowed. Is reserve forced to update data to the allocated memory range? 回答1: The guarantee of reserve is that subsequent insertions do not reallocate, and thus do not cause invalidation. That's it. There are no further guarantees. 回答2: Is reserve forced to update data to the allocated memory range? No. The standard only

Why does push_back() cause crash within malloc()'ed data?

萝らか妹 提交于 2019-12-23 05:08:49
问题 Why does this crash? I did find out malloc() doesnt call constructors, so I called them myself manually, but it still crashes, I do not understand why. PS. I know std::vector and new[] exists. Do not tell me to use vectors/new[] as an answer. struct MyStruct { vector<int> list; }; void make_crash(){ MyStruct *array = (MyStruct *)malloc(100*sizeof(MyStruct)); MyStruct element; // initialize element here since malloc() doesnt do it. array[0] = element; // copy, everything should be alright?

Subtraction and Intersection of two vectors of pointers in C++

久未见 提交于 2019-12-23 02:48:12
问题 I've two vectors having pointers to my custom class object. The pointers in these two vectors don't point to the same object, but the values stored in the objects are same. My custom class structure is: Class Item { string ItemId; string ItemDescription; float ItemPrice; } The first vector( V1 ) is having n entries and the second vector( V2 ) is having m entries (n>m) . I've to perform two operations: Get a vector which has common objects in both V1 and V2 . By common, I mean to say that the

Smart way to construct class member std::vector<std::unique_ptr<AClass> >

我与影子孤独终老i 提交于 2019-12-23 02:03:52
问题 This question combines unique_ptr as class member and move semantics fail to compile with clang and C++ std::vector in constructor. My goal is to construct a wrapper struct V_wrapper{ std::vector<std::unique_ptr<AClass> > vec; V_wrapper(std::vector<std::unique_ptr<AClass> > v) : vec{std::move(v)} {} }; Unfortunately this code does not compile, because the compiler (clang Apple LLVM version 4.2) attempts to copy construct the vector v which is not supported. On the other hand, if I design an