Passing a std::vector to a function

Started by
6 comments, last by SiCrane 11 years, 2 months ago

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);

Advertisement

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.

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);
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.
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.
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

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);
	}
}
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.

This topic is closed to new replies.

Advertisement