Jump to content

  • Log In with Google      Sign In   
  • Create Account


Passing a std::vector to a function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Tispe   Members   -  Reputation: 978

Like
0Likes
Like

Posted 16 February 2013 - 04:14 PM

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

 



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7745

Like
3Likes
Like

Posted 16 February 2013 - 04:39 PM

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, 16 February 2013 - 04:40 PM.


#3 Tispe   Members   -  Reputation: 978

Like
0Likes
Like

Posted 16 February 2013 - 04:53 PM

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


#4 Yrjö P.   Crossbones+   -  Reputation: 1412

Like
0Likes
Like

Posted 16 February 2013 - 04:55 PM

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.

#5 Yrjö P.   Crossbones+   -  Reputation: 1412

Like
0Likes
Like

Posted 16 February 2013 - 05:11 PM

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.

#6 iMalc   Crossbones+   -  Reputation: 2250

Like
0Likes
Like

Posted 16 February 2013 - 05:39 PM

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

#7 Tispe   Members   -  Reputation: 978

Like
0Likes
Like

Posted 16 February 2013 - 06:00 PM

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


#8 SiCrane   Moderators   -  Reputation: 9337

Like
2Likes
Like

Posted 16 February 2013 - 06:41 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS