std::vectors and winsocket

Is it safe to call a vector inside a winsocket..

Example:

1
2
3
4
5
6
7
8
9
typedef std::vector<unsigned char> bufferprt;

// check buffer data size
	size_t bufSize = bufferprt.size();
	if ( bufSize < 0 )
		return;

//sending the vector and the size thru SEND()
send( sckSocket,(char*)&bufferprt, bufferprt.size(), 0 );


Am i safe to call this type of code if not would i have to create a array to old the vector data then pass it to the send fucntion?
Last edited on
Should be fine as long as u dont modify the vector itself (ie: if u reallocate its contents the address of &bufferprt[0] will be invalid)

U can however, modify the content of the vector.

Also, be careful that u'r not trying to send an empty vector.
Well before hand im creating a vector with data into it then modiftying it thru other functions. Then running a send function that passes these modifcations.

Now what about Recv would it be ok to do the same?

1
2
3
4
5
6
7
8
9
typedef std::vector<unsigned char> recvbufferprt;

unsigned recvd = 0;

recvd = recv( sckSocket,(char*)&recvbufferprt, recvbufferprt.size(), 0 );
if (socket is zero)
{
     // disconnect the socket here
}
Last edited on
Yes of course. The same principles apply :)
std::vector<unsigned char> has the same memory layout as unsigned char from C. There shouldn't be an issue.
Topic archived. No new replies allowed.