Copy vector<char> into char*

6k views Asked by At

I'm just studying C and C++ programming.

I've searched and can't seem to find an answer that has a decent response. Of course using <string> is much easier but for this task I am REQUIRED to use only clib <string.h> functions; I'm also not allowed to use C++11 functions.

I have the 2 variables below, and want to move the contents of buffer into c.

vector<char> buffer;
char* c = "";

How can I do this easily?


I have this so far but it obviously doesn't work, otherwise I wouldn't be here.

for (int b = 0; b < buffer.size(); b++)
{
    c += &buffer[b];
}
3

There are 3 answers

0
Peter On BEST ANSWER

The simplest way I can think of is;

 std::vector<char> buffer;
   // some code that places data into buffer
 char *c = new char[buffer.size()];
 std::copy(buffer.begin(), buffer.end(), c);
    // use c
 delete [] c;

std::copy() is available in the standard header <algorithm>.

This assumes the code that places data into buffer explicitly takes care of inserting any trailing characters with value zero ('\0') into the buffer. Without that, subsequent usage of c cannot assume the presence of the '\0' terminator.

If you want to ensure a trailing '\0' is present in c even if buffer does not contain one, then one approach is;

 std::vector<char> buffer;
   // some code that places data into buffer
 char *c = new char[buffer.size() + 1];    // additional room for a trailing '\0'
 std::copy(buffer.begin(), buffer.end(), c);
 c[buffer.size()] = '\0';
    // use c
 delete [] c;

One could also be sneaky and use another vector;

 std::vector<char> container;
   // some code that places data into buffer
 std::vector<char> v(container);   // v is a copy of container
 v.push_back('\0');    // if we need to ensure a trailing '\0'
 char *c = &v[0]

    // use c like a normal array of char

As long as the code that uses c does not do anything that will resize v, the usage of c in this case is exactly equivalent to the preceding examples. This has an advantage that v will be released when it passes out of scope (no need to remember to delete anything) but a potential disadvantage that c cannot be used after that point (since it will be a dangling pointer).

0
Nivetha On

You can do it in this way:

vector<char> buffer;
//I am assuming that buffer has some data
char *c = new char[buffer.size()+1];

for( int i=0; i<buffer.size(); i++ )
   c[i] = buffer[i];
c[i] = '\0';

buffer.clear();
7
Sergey Kalinichenko On

First, allocate space for the data by assigning c = new char[buffer.size()];

Then use memcpy to copy the data: memcpy(c, buffer.data(), buffer.size())

Your for loop would work in place of memcpy, too.

Also note that if vector<char> stays in place all the time when you use char*, and you are allowed to change the content of the vector, you could simply use the data behind the vector with a simple assignment, like this:

char *c = buffer.data();

I'm noticing some weird behavior when I create my char* of the given size is that it creates it bigger with some random "hereýýýý««««««««" values after my word

It looks like you do need a null-terminated C string after all. In this case you need to allocate one extra character at the end, and set it to zero:

char *c = new char[buffer.size()+1];
memcpy(c, buffer.data(), buffer.size());
c[buffer.size()] = 0;