Sign in to follow this  
derek7

the assignment error??

Recommended Posts

derek7    100
typedef std::vector<Material*> MaterialSet;

std::vector<Mesh::MaterialSet> MeshManager::materialSetList()
{
	std::vector<Mesh::MaterialSet> t;
	MeshMapIterator iter;
    MeshMapIterator iend = m_meshList.end();
	int i = 0;
	for(iter = m_meshList.begin(); iter != iend; ++iter)
	{
		Mesh::MaterialSet temp = iter->second->materialSet();
		t[0] = temp;
		i++;
	}
	return t;
}



runtime error happen at t[i] = temp; why?

Share this post


Link to post
Share on other sites
hh10k    589
You haven't set t's size. You can do this by using t.resize(count), or by using t.push_back(item) for each item if you don't know the final count. Using push_back() will cause t's internal array to be reallocated every so often (and waste some extra memory), so it's probably better to use resize() when you can.

Also, you code uses t[0], not t[i] :)

Share this post


Link to post
Share on other sites
Conner McCloud    1135
Quote:
Original post by hh10k
You haven't set t's size. You can do this by using t.resize(count), or by using t.push_back(item) for each item if you don't know the final count. Using push_back() will cause t's internal array to be reallocated every so often (and waste some extra memory), so it's probably better to use resize() when you can.

Alternatively, call t.reserve(count), and follow that with calls to push_back. That ensures no reallocations occur, and doesn't fill your vector with a bunch of useless default objects.

CM

Share this post


Link to post
Share on other sites
Conner McCloud    1135
Quote:
Original post by dbzprogrammer
Plus you're making a new temp every time you run through the loop. Might be a good idea to make it outside the for loop and then re-assign is every time.

That variable has no business outside the scope of the for loop, so it has no business being declared outside the scope of the for loop. That's exactly the sort of trivial optimization you can count on the compiler doing for you if it has any merit.

CM

Share this post


Link to post
Share on other sites
dbzprogrammer    100
Quote:
Original post by Conner McCloud
Quote:
Original post by dbzprogrammer
Plus you're making a new temp every time you run through the loop. Might be a good idea to make it outside the for loop and then re-assign is every time.

That variable has no business outside the scope of the for loop, so it has no business being declared outside the scope of the for loop. That's exactly the sort of trivial optimization you can count on the compiler doing for you if it has any merit.

CM


Heh... Good point! But I never trust a compiler to optomize for me. I just try to code as good as possible, and maybe I'm lucky enough to have missed something! ;)

Share this post


Link to post
Share on other sites
Conner McCloud    1135
Quote:
Original post by derek7
resize work.
But OFTEN I dont need to resize or reserve.forexample:
vector<int> v;
v[i] = 4; just OK. Why?

You're getting lucky. Maybe the vector is allocating a few bytes just in case. Maybe the vector's pointer holds junk data that happens to belong to you. The why doesn't matter. You always need to add elements to the vector before using [] to access those elements, whether the program runs that way or not.

CM

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this