M. Rock
M. Rock

Reputation: 123

Store more data in iov in C++

I am writing a C++ program (see below). My goal is to store data in iov struct. I have allocated buffer of fixed length in constructor. Every time that buffer gets filled, I want to transfer data in iov and allocated new buffer of fixed length. Finally when done with data processing, I want to return iov struct. My intension here is to store all these data into iov so that if it's required in future, I can send data easily. I have written sample code. But it seems it's not working. I got an "Bus error: 10". Can someone help me?

Sample code:

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

Upvotes: 0

Views: 1518

Answers (2)

radato
radato

Reputation: 930

I took your code, which didn't exactly use anything with the iovec, and I modified it a little.

I am not sure why developers prefer buffers of char* instead of std::string or why use a pointer which should be allocated and then deleted instead of using a std::vector

I also added a function which uses the iovec. It is called void MyClass::print_data(). It prints all the data in the vector 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;
}

compile it like so: g++ test.cpp

Upvotes: 0

Stephan Lechner
Stephan Lechner

Reputation: 35164

Without understanding the intent of your program in detail, it is very clear that you forgot to reserve memory for the iov-objects themselfes. For example, in your constructor you write iov[0].iov_base = buf, yet iov has not been allocated before.

To overcome this, somewhere in your code, before the first access to iov, you should write something like iov = calloc(100,sizeof(struct iovev)) or a c++ equivalent using new[].

Consider the following program:

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
}

Upvotes: 1

Related Questions