在winsock中将向量重用为数组的更有效方法?

发布于 2024-07-14 20:50:14 字数 488 浏览 5 评论 0原文

我目前使用向量作为 c 样式数组通过 Winsock 发送和接收数据。

我有一个 std::vector,我将其用作我的“字节数组”。

问题是,我使用两个向量,一个用于每次发送,一个用于每个接收,但我所做的似乎效率相当低。

示例:

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 只需调用发送适当的次数并确保一切正常运行。

反正。 除非我在每次使用之前将向量归零,否则我会遇到重叠的错误。 有没有更有效的方法让我做我正在做的事情? 因为每次调用时将整个缓冲区清零似乎效率极低。

谢谢。

I'm currently using vectors as c-style arrays to send and recieve data through Winsock.

I have a std::vector and I'm using that as my 'byte array'.

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长发绾君心 2024-07-21 20:50:14

您可以使用 m_SendBuffer.clear()

否则 end() 方法将不知道缓冲区的实际大小是多少。

调用clear()并不是一个非常昂贵的方法。 除非你正在处理 486 之类的东西,否则它不应该影响你的表演

you can use m_SendBuffer.clear()

otherwise the end() method would not know what is the real size of the buffer.

clear() is not a very expensive method to call. Unless you're working on some 486 or something it shouldn't affect your performances

飘逸的'云 2024-07-21 20:50:14

似乎其他发帖者关注的是清除缓冲区的成本或缓冲区的大小。 然而,对于您正在做的事情,您实际上并不需要清除整个缓冲区或将其归零,或者知道其大小。 “内容重叠的错误”是 SendData 的问题,您尚未发布其代码。 据推测,SendData 不知道需要发送多少缓冲区,除非其中的数据以零结尾。 如果该假设正确,您所要做的就是正确地以零终止数据。

std::copy(EndBody.begin(),EndBody.end(),m_SendBuffer.begin());
m_SendBuffer[EndBody.size()] = 0;
SendData();

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.

std::copy(EndBody.begin(),EndBody.end(),m_SendBuffer.begin());
m_SendBuffer[EndBody.size()] = 0;
SendData();
泪之魂 2024-07-21 20:50:14

调用clear 是否意味着向量的新大小为0? 如果OP将向量用作一大块内存,那么他们必须在清除后调用调整大小,以确保有适当的空间可用于调用发送和接收。

调用clear然后调整向量的大小与仅用零填充它大致相同,不是吗?

向量::clear

向量::调整大小

填充

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.

Calling clear then resize on the vector would be around the same as just filling it with zeros would it not?

vector::clear

vector::resize

fill

回忆追雨的时光 2024-07-21 20:50:14

据我了解STL 文档,调用clear只是将.end()值设置为与.begin()相同,并将大小设置为零,这是即时的。

它不会改变分配的内存量或内存所在的位置(任何迭代器显然都是无效的,但数据往往会徘徊!)。 正如您已经发现的那样,.capacity() 不会改变,存储在那里的数据也不会改变。 如果您总是使用 .begin() .end() 和 STL 迭代器来访问该区域,那么这并不重要。

不要忘记,类的方法变量不会被初始化,除非您将它们包含在初始化列表中。 添加 m_SendBuffer(BUFSIZE,0) 可能会解决问题。

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文