Jump to content

  • Log In with Google      Sign In   
  • Create Account

SteveHatcher

Member Since 27 Nov 2013
Offline Last Active Yesterday, 09:39 PM

Posts I've Made

In Topic: Inserting element into vector during iteration causes crash

20 September 2016 - 08:21 PM

 

It does do exactly that. But you also call resize(10), which changes the vector to have 10 elements, and since you only had 10 capacity, it's full up again.

 

Ahh okay, but when I remove the resize(10) I still get the crash, so I am a bit confused now...

 

 

 

I thought this sort of thing would be quite common in C++ so am surprised at this behavior!

 
Documentation of std::vector will point out that element insertion invalidates iterators.

 

It looks like this vector is a primary owner for these objects. If you can ensure that the vector outlives anything that may refer to its contents then you could just use std::vector<Test1> and then take references to elements or (preferably) simply refer to them directly as needed. If you're thinking about polymorphism then that won't work though.

 

 

The actual situation in my game needs polymorphism, thanks though!

 

Thanks everyone for your input I am learning a lot!


In Topic: Inserting element into vector during iteration causes crash

20 September 2016 - 05:29 AM

 

1.  If you will only add to the vector, use indices instead of iterators:

for (int i = m_test.size()-1; i >= 0; i--)
        {
            auto newPtr = make_shared<Test1>();
            m_test[i]->update();
            pushOn(newPtr);
            cout << "if you read this it passed" << endl;
        }

2. Yes.

 

 

Ahh okay thanks.

 

Now I am wondering if adding them this way vs. other methods is most recommended.

 

Thanks!

 


In Topic: Inserting element into vector during iteration causes crash

20 September 2016 - 05:22 AM

Your constructor reserves enough space for 10 elements, then actually creates 10 elements. So as soon as you add one more, that is element 11 - and you don't have space for that, so it resizes, and you get exactly the same problem as before.

 

If you know you will never have more than 10 elements, the reserve() call alone should be enough.

If you can't be sure about that, then you need to add elements outside the loop, or consider a different data structure, or restart the loop any time you add something.

 

Ahh thanks! That is an excellent explanation. I *thought* that reserve sets up the vector such that it is ready to take the 'reserved' number of items before it must resize itself, but it still fills them up from 0, 1, 2 etc...

 

I will have a look at your suggested workarounds, I thought this sort of thing would be quite common in C++ so am surprised at this behavior!

 


In Topic: Inserting element into vector during iteration causes crash

20 September 2016 - 05:09 AM

Hi vstrakh,

 

Thanks for your reply, I am slowly learning c++'s ways....

 

So I added this in my constructor:

	Test(){
		m_test.reserve(10);
		m_test.resize(10);
		cout << "struct Test constructor" << endl;
	}

I am still getting the crash though...

 

Thanks for any help.


In Topic: Transitioning game states when using a stack of states

07 September 2016 - 06:46 PM

As for the rest, here's what I mean:

 
Does that help?

 

Yes! Thanks! That's amazing. I couldn't quite figure out how to apply the demos of this sort of stuff I have read to my game. Much appreciated!

 

You might be over complicating things....

 

 

State has a specific meaning, ...

 

 

Yeah.. I see what you mean guys, Thanks. I think I need to separate the idea of GameState and "Screen or UI State". Since In my game I will be wanting these 'overlays. As Kylotan says though It is a fine line and I am having trouble deciding what constitutes as a "GameState" and "ScreenState".
 


PARTNERS