Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vector& operator=(const vector& rhs);rhs的size大于当前对象的size并且小于等于当前的capacity时,会造成内存泄漏 #144

Open
CookLiuTest opened this issue Jan 25, 2024 · 1 comment

Comments

@CookLiuTest
Copy link

mystl::vector<int> vector(10);
mystl::vector<int> v(1);
*(int*)(v.begin() + 10) = 200;
v = vector;
std::cout << "v.capaty:" << v.capacity() << " v.size:" << v.size() << " "<< * (int*)(v.begin() + 10);//v的初始容量为16,但是经过赋值构造后,cap_指向了第10个元素的后面,导致后面6个元素内存没有被释放,内存泄露
@frederick-vs-ja
Copy link
Contributor

*(int*)(v.begin() + 10) = 200; 这行怎么看都是错的。

最好先提供一个正确的代码。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants