I'm currently using vectors as c-style arrays to send and recieve data through Winsock.
我目前正在使用矢量作为c风格的数组来通过Winsock发送和接收数据。
I have a std::vector and I'm using that as my 'byte array'.
我有一个std :: vector,我正在使用它作为我的'字节数组'。
The problem is, I'm using two vectors, one for each send, and one for each recv, but what I'm doing seems to be fairly inefficient.
问题是,我正在使用两个向量,每个发送一个,每个recv一个,但我正在做的似乎是相当低效的。
Example:
std::string EndBody("\r\n.\r\n");
std::fill(m_SendBuffer.begin(),m_SendBuffer.end(),0);
std::copy(EndBody.begin(),EndBody.end(),m_SendBuffer.begin());
SendData();
SendData just calls send the appropriate amount of times and ensures everything works as it should.
SendData只是调用发送适当的次数并确保一切正常工作。
Anyway. Unless I zero out the vector before each use I get errors with stuff overlapping. Is there a more efficient way for me to do what I'm doing? Because it seems that zeroing out the entire buffer on each call is horribly inefficient.
无论如何。除非我在每次使用之前将矢量归零,否则会出现重叠错误的错误。有没有更有效的方式让我做我正在做的事情?因为似乎在每次调用时将整个缓冲区清零是非常低效的。
Thanks.
4 个解决方案
#1
you can use m_SendBuffer.clear()
你可以使用m_SendBuffer.clear()
otherwise the end() method would not know what is the real size of the buffer.
否则end()方法不知道缓冲区的实际大小是多少。
clear() is not a very expensive method to call. Unless you're working on some 486 or something it shouldn't affect your performances
clear()不是一种非常昂贵的调用方法。除非你正在处理某些486或其他东西,否则它不会影响你的表现
#2
Seems like the other posters are focusing on the cost of clearing the buffer, or the size of the buffer. Yet you don't really need to clear or zero out the whole buffer, or know its size, for what you're doing. The 'errors with stuff overlapping' is a problem with SendData, that you've not posted the code for. Presumably SendData doesn't know how much of the buffer it needs to send unless the data within it is zero-terminated. if that assumption is correct, all you have to do is zero-terminate the data correctly.
似乎其他海报正在关注清除缓冲区的成本或缓冲区的大小。然而,您并不需要清除或清除整个缓冲区,或者知道它的大小,因为您正在做的事情。 '错误与东西重叠'是SendData的一个问题,你没有发布代码。据推测,SendData不知道需要发送多少缓冲区,除非其中的数据是零终止的。如果这个假设是正确的,那么你所要做的就是正确地对数据进行零终止。
std::copy(EndBody.begin(),EndBody.end(),m_SendBuffer.begin());
m_SendBuffer[EndBody.size()] = 0;
SendData();
#3
Wouldn't calling clear mean the vector gets a new size of 0? If the OP is using the vector as a large chunk of memory then they'd have to then call resize after clear to ensure the appropriate space is available for calls to send and recv.
不会调用清楚意味着向量获得0的新大小?如果OP使用向量作为大块内存,那么他们必须在清除之后调用resize以确保调用send和recv的适当空间。
Calling clear then resize on the vector would be around the same as just filling it with zeros would it not?
调用clear然后调整大小就会大致相同,只是用零填充它不会吗?
#4
As far as I understand the STL docs, calling clear simply sets the .end() value to be the same as .begin() and sets size to zero,which is instant.
据我所知STL文档,调用clear只是将.end()值设置为与.begin()相同,并将size设置为零,这是即时的。
It doesn't change the amount of memory allocated or where the memory is (any iterator will obviously be invalid, but the data tends to linger!). The .capacity() doesn't change and neither does the data stored there, as you have already discovered. If you are always using .begin() .end() and STL iterators to access the area this won't matter.
它不会改变分配的内存量或内存的位置(任何迭代器显然都是无效的,但数据往往会延续!)。正如您已经发现的那样,.capacity()不会更改,也不会存储数据。如果你总是使用.begin()。end()和STL迭代器访问该区域,这无关紧要。
Don't forget, method variables of a class aren't initialised unless you include them in your initialisation list. Adding m_SendBuffer(BUFSIZE,0)
there might do the trick.
不要忘记,除非将它们包含在初始化列表中,否则不会初始化类的方法变量。添加m_SendBuffer(BUFSIZE,0)可能会有所作为。
#1
you can use m_SendBuffer.clear()
你可以使用m_SendBuffer.clear()
otherwise the end() method would not know what is the real size of the buffer.
否则end()方法不知道缓冲区的实际大小是多少。
clear() is not a very expensive method to call. Unless you're working on some 486 or something it shouldn't affect your performances
clear()不是一种非常昂贵的调用方法。除非你正在处理某些486或其他东西,否则它不会影响你的表现
#2
Seems like the other posters are focusing on the cost of clearing the buffer, or the size of the buffer. Yet you don't really need to clear or zero out the whole buffer, or know its size, for what you're doing. The 'errors with stuff overlapping' is a problem with SendData, that you've not posted the code for. Presumably SendData doesn't know how much of the buffer it needs to send unless the data within it is zero-terminated. if that assumption is correct, all you have to do is zero-terminate the data correctly.
似乎其他海报正在关注清除缓冲区的成本或缓冲区的大小。然而,您并不需要清除或清除整个缓冲区,或者知道它的大小,因为您正在做的事情。 '错误与东西重叠'是SendData的一个问题,你没有发布代码。据推测,SendData不知道需要发送多少缓冲区,除非其中的数据是零终止的。如果这个假设是正确的,那么你所要做的就是正确地对数据进行零终止。
std::copy(EndBody.begin(),EndBody.end(),m_SendBuffer.begin());
m_SendBuffer[EndBody.size()] = 0;
SendData();
#3
Wouldn't calling clear mean the vector gets a new size of 0? If the OP is using the vector as a large chunk of memory then they'd have to then call resize after clear to ensure the appropriate space is available for calls to send and recv.
不会调用清楚意味着向量获得0的新大小?如果OP使用向量作为大块内存,那么他们必须在清除之后调用resize以确保调用send和recv的适当空间。
Calling clear then resize on the vector would be around the same as just filling it with zeros would it not?
调用clear然后调整大小就会大致相同,只是用零填充它不会吗?
#4
As far as I understand the STL docs, calling clear simply sets the .end() value to be the same as .begin() and sets size to zero,which is instant.
据我所知STL文档,调用clear只是将.end()值设置为与.begin()相同,并将size设置为零,这是即时的。
It doesn't change the amount of memory allocated or where the memory is (any iterator will obviously be invalid, but the data tends to linger!). The .capacity() doesn't change and neither does the data stored there, as you have already discovered. If you are always using .begin() .end() and STL iterators to access the area this won't matter.
它不会改变分配的内存量或内存的位置(任何迭代器显然都是无效的,但数据往往会延续!)。正如您已经发现的那样,.capacity()不会更改,也不会存储数据。如果你总是使用.begin()。end()和STL迭代器访问该区域,这无关紧要。
Don't forget, method variables of a class aren't initialised unless you include them in your initialisation list. Adding m_SendBuffer(BUFSIZE,0)
there might do the trick.
不要忘记,除非将它们包含在初始化列表中,否则不会初始化类的方法变量。添加m_SendBuffer(BUFSIZE,0)可能会有所作为。