Vector Assignment Crashing

App crashing when using push_back method for a vector of smart pointers

When you move from a shared_ptr the moved from variable is invalid (in this case null).

You call data_ptr->get_data() before pushing, which is fine. But after you push (via move) calling data_ptr->get_data() a second time will use the (now moved-from) smart pointer - hence your crash.

Growing a vector in a iterator based loop crashes in destructing with munmap_chunk(): invalid pointer, why?

When you resize a std::vector, iterators that are pointing into it might get invalidated. So you need to reset it after resizing:

if (*it == 5 && std::next(it) == exampleVector.end()) {
exampleVector.resize(exampleVector.size() + 1);
it = std::next(exampleVector.begin(), exampleVector.size() - 2);
// ...
}

Obviously, indexes are just ints, so there is no issue about them becoming invalid, so your second example works fine.

Range-for loops are just pretty syntax for an iterator based loop, so yes, it would have the same issue as the first version.

crash when using stl vector at instead of operator[]

I hate answering my own questions but this seems to be a situation where it is required. As it says in the update, I downloaded and installed the latest intel c++ compiler and recompiled from scratch which seems to have fixed the problem. I've also rebuilt the entire project from scratch using the 11.0.074 compiler to rule out a corruption of one of the binaries; even a clean build results in the crash!

I'm going to follow this up on the Intel forums, thanks to everyone who put in some time and effort to this problem.

Program crashes when trying to create and access 2-d vector

The problem is, when using vector you have to assign memory for the number of elements you want to store. (Well, using arrays too but that's sort of implicit when creating one statically) You just can't go ahead and say you want to store something in a box when there is no box.

There are two ways I can think of to make space for those elements. One is using the constructor as in:

std::vector<int> myVector(100); // This makes room for a 100 elements

And using the function for that purpose.

std::vector<int> myVector;
myVector.reserve(100); // This makes room for a 100 elements after the declaration

So from there, I think you can figure out what's next. You just need to make space for everything.

I personally don't use Vector that much because I was never required to (Had to make my own dynamic lists in school and basically used that as my "Vector" library), but I definitely should use it when needed because it offers a lot of advantages.

Just as a notice, one thing I do know is that you are using the unsafe way of accessing data in your vector. Unsafe because it can't throw any exceptions at you to catch if it happens to do a restricted access.

std::vector<int> myVector(10);
myVector.at(0) = 0; // What I mean is that function, you can jut wrap it inside a try-catch to make it safer.
myVector[0] = 0; // Instead of this, of course.

Of course, then there are iterators and all that stuff which should be safer, but you can look further on that later.



Related Topics



Leave a reply



Submit