在 C++ 的 iov 中存储更多数据

Store more data in iov in C++

本文关键字:存储 多数据 iov C++      更新时间:2023-10-16

我正在编写一个C++程序(见下文)。我的目标是将数据存储在 iov 结构中。我已经在构造函数中分配了固定长度的缓冲区。每次缓冲区被填满时,我都想在 iov 中传输数据并分配固定长度的新缓冲区。最后,当完成数据处理时,我想返回 iov 结构。我在这里的意图是将所有这些数据存储到 iov 中,以便将来需要时,我可以轻松发送数据。我已经编写了示例代码。但它似乎不起作用。我收到"总线错误:10"。有人可以帮助我吗?

示例代码:

#include <iostream>
#include <string>
#include <sys/uio.h>
#include <cstdlib>
using namespace std;
#define MAX_LEN 1000
#define MIN_LEN    20
class MyClass
{   
public:
MyClass();
~MyClass();
void fillData(std::string &data);
private:
struct iovec     *iov;
unsigned int     count;
unsigned int  len;
char *buf;
unsigned int total_len;
unsigned int tmp_len;
};  
MyClass::MyClass()
{   
cout << "Inside constructor" << endl;
total_len = MIN_LEN;
buf = (char *)malloc(MAX_LEN);
if (buf == NULL) {
cout << "Error: can’t allocate buf" << endl;
exit(EXIT_FAILURE);
} 
}   

MyClass::~MyClass()
{   
free(buf);
}   
void MyClass::fillData(std::string &data)
{   
unsigned int d_len, tmp_len, offset;
d_len = data.size();
const char* t = data.c_str();
total_len += d_len;
tmp_len += d_len;
if (total_len > MAX_LEN) {
/* Allocate memory and assign to iov */
tmp_len = d_len;
}

memcpy(buf + offset, t,  d_len);
/* Adjust offset */
}
int main()
{
MyClass my_obj;
int i;
std::string str = "Hey, welcome to my first class!";
for (i = 0; i < 10; i++) {
my_obj.fillData(str);
}
return 0;
}

如果不详细了解程序的意图,很明显您忘记为iov对象保留内存。 例如,在构造函数中,您编写iov[0].iov_base = buf,但以前没有分配iov

为了克服这个问题,在代码中的某个地方,在第一次访问iov之前,您应该使用new[]编写类似iov = calloc(100,sizeof(struct iovev))或 c++ 等效项的东西。

请考虑以下程序:

struct myStruct {
char *buf;
int len;
};
int main() {
struct myStruct *myStructPtr;
myStructPtr->buf = "Herbert";  // Illegal, since myStructPtr is not initialized; So even if "Herbert" is valid, there is no place to store the pointer to literal "Herbert".
myStructPtr[0].buf = "Herbert"; // Illegal, since myStructPtr is not initialized
// but:
struct myStruct *myStructObj = new (struct myStruct);
myStructObj->buf = "Herbert"; // OK, because myStructObj can store the pointer to literal "Herbert"
myStructObj->buf = "Something else"; // OK; myStructObj can hold a pointer, so just let it point to a different portion of memory. No need for an extra "new (struct myStruct)" here
}

我拿了你的代码,它并没有完全使用任何iovec,我稍微修改了一下。

我不确定为什么开发人员更喜欢char*缓冲区而不是std::string或者为什么要使用应该分配然后删除的指针,而不是使用std::vector

我还添加了一个使用 iovec 的函数。它被称为void MyClass::print_data()。它打印矢量中的所有数据iovecs

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <unistd.h>
#include <sys/uio.h>
using namespace std;
class MyClass
{
vector<struct iovec> iovs;
vector<string> bufs;
public:
MyClass();
~MyClass();
void fill_data(const string &data);
void print_data();
};
MyClass::MyClass()
{
cout << "Inside constructor" << endl;
}

MyClass::~MyClass()
{
}
void MyClass::fill_data(const string &data)
{
stringstream stream;
stream << setw(2) << setfill(' ') << (this->bufs.size() + 1) << ". "
<< data << endl;
this->bufs.push_back(stream.str());
iovec iov = {&(bufs.back()[0]), bufs.back().size()};
this->iovs.push_back(iov);
}
void MyClass::print_data() {
writev(STDOUT_FILENO, iovs.data(), iovs.size());
}
int main() {
MyClass my_obj;
string str = "Hey, welcome to my first class!";
for (int i = 0; i < 10; ++i) {
my_obj.fill_data(str);
}
my_obj.print_data();
return 0;
}

像这样编译它:g++ test.cpp