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

关于allocator<T>::deallocate(pointer ptr, size_type n) #168

Open
Martin8311 opened this issue Nov 10, 2024 · 1 comment
Open

关于allocator<T>::deallocate(pointer ptr, size_type n) #168

Martin8311 opened this issue Nov 10, 2024 · 1 comment

Comments

@Martin8311
Copy link

template
void allocator::deallocate(pointer ptr, size_type n){
if(ptr == nullptr)
return ;
::operator delete(ptr);
}
在这段代码中ptr是指向连续n个元素内存空间首个元素的指针吗?那么delete(ptr)是只释放了单个元素所在的内存空间还是会全部释放掉,如果只释放单个元素会不会出现内存泄漏问题?

@winner245
Copy link
Contributor

这里没有问题,因为分配时使用的也是 non-array form ::operator new(n * sizeof(T)),不是 array-form operator new[],因此释放时也是 non-array form。

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