Search code examples
c++vectormemset

memset on vector is making its size 0


I was trying to clear all data in a vector without changing its size using memset. But after memset operation size of vector becomes 0. Here is the exact code:

std::vector<T> buckets[2];
MAX_BUCKET_SIZE=16;

Constructor:

buckets[0].resize(MAX_BUCKET_SIZE);
std::cout << "Actual size0 " << buckets[0].size() << std::endl;
buckets[1].resize(MAX_BUCKET_SIZE);
std::cout << "Actual size1 " << buckets[1].size() << std::endl;

in a function:

std::cout << "Actual size2 0 " << buckets[0].size() << std::endl;
std::cout << "Actual size2 1 " << buckets[1].size() << std::endl;
...
...
while (...){
    delete_data(current_bucket);

    std::cout << "current bucket ** " << current_bucket << std::endl;
    std::cout << "Actual size3 0 " << buckets[0].size() << std::endl;
    std::cout << "Actual size3 1 " << buckets[1].size() << std::endl;
}

delete_data function:

memset(&buckets[bucket_id], 0, sizeof(buckets[bucket_id]));

output:

Actual size0 16
Actual size1 16
Actual size2 0 16
Actual size2 1 16
current bucket ** 1
Actual size3 0 16
Actual size3 1 0
current bucket ** 0
Actual size3 0 0
Actual size3 1 0

Solution

  • Not to mention memset on non-POD is undefined behavior, it looks like you've just "succeeded" memset-ing the vector's internal data - that could be pointer to implementation or pointers to start/end of the underlying array. You didn't modify the elements and you effectively lost them (and leaked a memory).

    You most probably wanted to operate on the underlying array (still, T has to be POD):

    memset(&buckets[bucket_id].front(), 0, buckets[bucked_id].size() * sizeof(T));
    

    But nobody does this in C++, because there are STL solutions, like std::fill:

    std::fill(buckets[bucket_id].begin, buckets[bucket_id].end(), T{});
    

    which will fill the array with copies of default-constructed T (which can be non-POD).

    Also note that sizeof(buckets[bucket_id]) gives you completely pointless number for this occasion.