Copy std :: vector <char> contents to char * buffer?

I have std :: vector. I want to copy the contents of a vector to a char * buffer of a certain size.

Is there a safe way to do this?

Can I do it?

memcpy(buffer, _v.begin(), buffer_size); 

or that?

 std::copy(_v.begin(), _v.end(), buffer); // throws a warning (unsafe) 

or that?

 for (int i = 0; i < _v.size(); i++) { *buffer = _v[i]; buffer++; } 

Thanks..

+7
source share
5 answers
 std::copy(_v.begin(), _v.end(), buffer); 

This is the preferred way to do this in C ++. It is safe to copy this path if buffer is large enough.

+18
source

If you just need a char* , then you can do this:

 char *buffer=&v[0];//v is guaranteed to be a contiguous block of memory. //use buffer 

Note that the changing data pointed to by buffer also changes vector content!

Or, if you need a copy, v.size() memory of size v.size() bytes and use std::copy :

  char *buffer = new char[v.size()]; std::copy(v.begin(), v.end(), buffer); 

Do not forget delete []buffer; after you finish, otherwise you will lose your memory.

But then why do you need a problem that requires you to manage your memory ... especially when you can do better, for example:

 auto copy = v; // that simpler way to make copies!! // and then use copy as new buffer. // no need to manually delete anything. :-) 

Hope this helps.

+11
source

The safest way to copy vector<char> to the char * buffer is to copy it to another vector, and then use this vector internal buffer:

 std::vector<char> copy = _v; char * buffer = &copy[0]; 

Of course, you can also access the _v buffer if you really don't need to copy the data. Also, beware that the pointer will not be valid if the vector is modified.

If you need to copy it to a specific buffer, you need to know that the buffer is large enough before copying; no array constraint checks. After you have checked the size, your second method is best. (The first only works if vector::iterator is a pointer that is not guaranteed, although you can change the second argument to &_v[0] to work. The third does the same, but is more complicated and probably should be fixed so that it does not change buffer ).

+4
source

Well, you want to assign *buffer to case 3, but that should work. The first will almost certainly not work.

EDIT: I am standing corrected for No. 2.

0
source
 static std::vector<unsigned char> read_binary_file (const std::string filename) { // binary mode is only for switching off newline translation std::ifstream file(filename, std::ios::binary); file.unsetf(std::ios::skipws); std::streampos file_size; file.seekg(0, std::ios::end); file_size = file.tellg(); file.seekg(0, std::ios::beg); std::vector<unsigned char> vec(file_size); vec.insert(vec.begin(), std::istream_iterator<unsigned char>(file), std::istream_iterator<unsigned char>()); return (vec); } 

and then:

 auto vec = read_binary_file(filename); auto src = (char*) new char[vec.size()]; std::copy(vec.begin(), vec.end(), src); 

but don't forget to remove [] src later

0
source

Source: https://habr.com/ru/post/901006/


All Articles