Why doesn't vector::clear remove elements from

2019-01-11 12:47发布

When I use clear() on a std::vector, it is supposed to destroy all the elements in the vector, but instead it doesn't.

Sample code:

vector<double> temp1(4);
cout << temp1.size() << std::endl;
temp1.clear();
cout << temp1.size() << std::endl;

temp1[2] = 343.5; // I should get segmentation fault here ....

cout << "Printing..... " << temp1[2] << endl;
cout << temp1.size() << std::endl;

Now, I should have gotten segmentation fault while trying to access the cleared vector, but instead it fills in the value there (which according to me is very buggy)

Result looks as follows:

4
0
Printing..... 343.5
0

Is this normal? This is a very hard bug to spot, which basically killed my code for months.

10条回答
仙女界的扛把子
2楼-- · 2019-01-11 13:13

You have no right to get a segmentation fault. For that matter, a segmentation fault isn't even part of C++. Your program is removing all elements from the vector, and you're illegally accessing the container out of bounds. This is undefined behaviour, which means anything can happen. And indeed, something happened.

查看更多
疯言疯语
3楼-- · 2019-01-11 13:14

From cppreference:

void clear();

Removes all elements from the container. Invalidates any references, pointers, or iterators referring to contained elements. May invalidate any past-the-end iterators. Many implementations will not release allocated memory after a call to clear(), effectively leaving the capacity of the vector unchanged.

So the reason there is no apparent problem is because the vector still has the memory available in store. Of course this is merely implementation-specific, but not a bug. Besides, as the other answers point out, your program also does have Undefined Behavior for accessing the cleared contents in the first place, so technically anything can happen.

查看更多
该账号已被封号
4楼-- · 2019-01-11 13:14

try to access to an elements sup than 4 that you use for constructor may be you will get your segmentation fault An other idea from cplusplus.com:

Clear content

Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.

A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap:

vector().swap(x); // clear x reallocating

查看更多
你好瞎i
5楼-- · 2019-01-11 13:21

If you use

temp1.at(2) = 343.5;

instead of

temp1[2] = 343.5;

you would find the problem. It is recommended to use the function of at(), and the operator[] doesn't check the boundary. You can avoid the bug without knowing the implementation of STL vector.

BTW, i run your code in my Ubuntu (12.04), it turns out like what you say. However, in Win7, it's reported "Assertion Failed".

Well, that reminds me of the type of stringstream. If define the sentence

stringstream str;
str << "3456";

If REUSE str, I was told to do like this

str.str("");
str.clear();

instead of just using the sentence

str.clear();

And I tried the resize(0) in Ubuntu, it turns out useless.

查看更多
登录 后发表回答