vendredi 24 avril 2015

Why Pointer contains some trash?

I have the following code snippet:

size_t size = 5;
std::vector<char> container(size, 'd');

std::copy(container.begin(), container.begin() + size,
    std::ostream_iterator<char>(std::cout, " ")); // d d d d d

auto ptr = containter.data();
//ptr == dddddTRASHTRASH why??

char* str_ = new char[size + 1];
memcpy(str_, container.data, size * sizeof(char));

std::cout << str_ << std::endl; // dddddTRASHTRASHTRASH!!!!

I don't understand, why my pointer contains not only d. How to create pointer with 5 symbols of d with RAII?

Aucun commentaire:

Enregistrer un commentaire