开发者

What is the advantage of using vector<char> as input buffer over char array?

I am working on a network programming and I have seen people using vector as the input buffer for socket instead of char array.

I was wondering what开发者_运维百科 it the advantage of doing this.

Thanks in advance..


A vector<char> is essentially just a managed character array.

So you can write:

{
    vector<char> buf(4096);
    ...
    int result = recv(fd, &buf[received_so_far], buf.size() - received_so_far);
    ...
}

The vector "knows" its size, so you can use buf.size() everywhere and never have to worry about overrunning your buffer. You can also change the size in the declaration and have it take effect everywhere without any messy #defines.

This use of buf will allocate the underlying array on the heap, and it will free it automatically when buf goes out of scope, no matter how that happens (e.g. exceptions or early return). So you get the nice semantics of stack allocation while still keeping large objects on the heap.

You can use buf.swap() to "hand ownership" of the underlying character array to another vector<char> very efficiently. (This is a good idea for network traffic... Modern networks are fast. The last thing you want to do is to create yet another copy of every byte you receive from the network.) And you still do not have to worry about explicitly freeing the memory.

Those are the big advantages that come to mind off the top of my head for this particular application.


using vector is:

  • easy to grow-up space of memory.
  • easy to make copy of datas.
  • easy to free.


My main reason:

  • Its Exception Safe.
    • No matter what goes wrong the vector will not leak memory.


vector can be easily resized and allows convenient partial deletion of data - that's big advantage. In many cases having a fixed size array is inconvenient - for example you will read blocks of data and process received data in chunks smaller than received chunks. Then you want to know the actual amount of data left - that will be vector.size() and you won't have to store it in a separate variable and when you delete part of the data vector will move the remaining for you.


If you've ever programmed network applications in Java, you are likely to be familiar with ByteBuffer and IoBuffer. The two classes made buffer handling significantly easier with pretty easy and intuitive interface.

In the world of C++, very fortunately, with the standard vector class everything provided by this two class is what you can use. Since vector can dynamically grow, it's even more powerful than ByteBuffer and IoBuffer. Just write your code and you'll find everything you would expect in a byte buffer wrapper is just there.

Some obvious advantage:

  • Preallocate memory with vector::reserve
  • Remember buffer position with vector::size
  • Grow/clear buffer with vector::resize
  • Convert to C buffer with &your_vector[0]
  • Transfer buffer ownership with vector::swap
  • ... and more to be discovered.


One main difference between std::vector<char> buffer(SIZE) and std::array<char, SIZE> buffer is the placement of the SIZE argument: For vector, you only need to know the size at runtime, and you can even change the size, if needed. For array, SIZE is a template parameter, so you need to know it already at compile time and it stays fixed until you recompile. So, basically std::array is just a wrapper around C style arrays char buffer[SIZE]. While since the C++14 standard, C style arrays may be dynamically sized in C++, std::array is always statically sized. However, be aware, that C style arrays are usually allocated on the stack, and that stack space is limited in most implementations.

The next difference is, that std::vector performs value-initialization on its elements, which usually means setting them to zero (unless you override that with a custom constructor), while std::array and C style arrays leave their content uninitialized.

Therefore:

  • If you need a buffer of a known fixed size, if that size doesn't exceed a few kilobytes, AND if the contents of that buffer are going to be properly written by the application, use std::array or a C style array. This is the faster solution.
  • Otherwise, use std::vector. This is the safer solution.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜