Sign in to follow this  
Tispe

Passing a std::vector to a function

Recommended Posts

Tispe    1468

Hi

 

I am wondering how I should pass vectors to a function that will operate on them. Should I just pass them as is or by &reference? I read somewhere that std::vector is a pointer itself and such does not need to be referenced, is that true? Are there any pro/cons for these methods?

 

Do I need to use the std::vector::at method for both cases?

 

//proto
void AddVertex(std::vector<VERTEX>* pNewVertexBuffer, std::vector<short>* pNewIndexBuffer, VERTEX* pNewVertex);
std::vector<VERTEX> NewVertexBuffer;
std::vector<short> NewIndexBuffer;

VERTEX NewVertex = GetVertex(.........);

//Check for duplicates and adds to vertex and index arrays
AddVertex(&NewVertexBuffer, &NewIndexBuffer, &NewVertex);

 

OR

 

//proto
void AddVertex(std::vector<VERTEX> NewVertexBuffer, std::vector<short> NewIndexBuffer, VERTEX* pNewVertex);
std::vector<VERTEX> NewVertexBuffer;
std::vector<short> NewIndexBuffer;

VERTEX NewVertex = GetVertex(.........);

//Check for duplicates and adds to vertex and index arrays
AddVertex(NewVertexBuffer, NewIndexBuffer, &NewVertex);

 

Share this post


Link to post
Share on other sites
Brother Bob    10344

Your first is pass by pointer, not pass by reference. Pass by reference would be this:

 

void AddVertex(std::vector<VERTEX> &pNewVertexBuffer, ...); // prototype
 
AddVertex(NewVertexBuffer, ...); // call

Your second is pass by value, so an entire copy of the vector is made, and the function AddVertex will operate on its own local copy.

 

You want pass by reference in this case over pass by pointer, and most certainly not pass by value.

Edited by Brother Bob

Share this post


Link to post
Share on other sites
Tispe    1468

So the correct code would look like this?

 

//proto
void AddVertex(std::vector<VERTEX>& pNewVertexBuffer, std::vector<short>& pNewIndexBuffer, VERTEX* pNewVertex);
std::vector<VERTEX> NewVertexBuffer;
std::vector<short> NewIndexBuffer;

VERTEX NewVertex = GetVertex(.........);

//Check for duplicates and adds to vertex and index arrays
AddVertex(NewVertexBuffer, NewIndexBuffer, &NewVertex);

Share this post


Link to post
Share on other sites
What Bob said.

Good C++ style requires using references when you can, and pointers only when actually necessary. It's much easier to screw up with pointers, you have to write more syntax, and they are not more efficient than references.

Share this post


Link to post
Share on other sites
Yes, that is correct as far as the vector usage goes. I do wonder why you are passing NewVertex's address to AddVertex. Since NewVertexBuffer contains VERTEXes, AddVertex now has to construct a new VERTEX before it can stick it in the vector.

All-capital "VERTEX" looks atrocious, too.

Share this post


Link to post
Share on other sites
iMalc    2466
Generally you pass by pointer if:
1. The argument can be NULL, or
2. It is going to addressed like an array, or
3. For a legacy API or other such functions that cannot be changed.
Otherwise you pass by reference.

Share this post


Link to post
Share on other sites
Tispe    1468

Yes, that is correct as far as the vector usage goes. I do wonder why you are passing NewVertex's address to AddVertex. Since NewVertexBuffer contains VERTEXes, AddVertex now has to construct a new VERTEX before it can stick it in the vector.

All-capital "VERTEX" looks atrocious, too.

 

Well, i passed by pointer to do a memcmp in the fucntion.

void LOADER::AddVertex(std::vector<VERTEX>& NewVertexBuffer, std::vector<short>& NewIndexBuffer, VERTEX* pNewVertex)
{
	//Check if vertex is already in the new vertex buffer
	bool dupe = false;
	short DupeIndex;

	for(size_t i=0;i<NewVertexBuffer.size();i++)
	{
		if( memcmp(&NewVertexBuffer[i], pNewVertex, sizeof(VERTEX)) == 0 )
		{
			dupe = true;
			DupeIndex = (short)i;
			break;
		}	
	}

	if(dupe)
	{
		NewIndexBuffer.push_back(DupeIndex);
	}
	else
	{
		NewIndexBuffer.push_back((short)NewVertexBuffer.size());
		NewVertexBuffer.push_back(*pNewVertex);
	}
}

Share this post


Link to post
Share on other sites
SiCrane    11839
You can still get the address of an object that is passed by reference with the unary & operator. However it might make more sense to define an operator== overload instead.

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