Skip to content

Commit 1d0bc58

Browse files
committed
添加deallocate和destory不同解析
1 parent 0a2fbf0 commit 1d0bc58

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

STL/STL.md

+39-12
Original file line numberDiff line numberDiff line change
@@ -401,22 +401,22 @@ Example
401401
#include <iostream>
402402
#include <array>
403403
404-
int main ()
404+
int main()
405405
{
406-
std::array<int,10> myarray;
407-
unsigned int i;
406+
std::array<int, 10> myarray;
407+
unsigned int i;
408408
409-
// assign some values:
410-
for(i=0; i<10; i++)
411-
myarray[i] = i;
409+
// assign some values:
410+
for (i = 0; i<10; i++)
411+
myarray[i] = i;
412412
413-
// print content
414-
std::cout << "myarray contains:";
415-
for(i=0; i<10; i++)
416-
std::cout << ' ' << myarray[i];
417-
std::cout << '\n';
413+
// print content
414+
std::cout << "myarray contains:";
415+
for (i = 0; i<10; i++)
416+
std::cout << ' ' << myarray.at(i);
417+
std::cout << '\n';
418418
419-
return 0;
419+
return 0;
420420
}
421421
```
422422
Output
@@ -1608,6 +1608,33 @@ Output
16081608
```
16091609
The allocated array contains: 0 1 2 3 4
16101610
```
1611+
1612+
注意:deallocate和destory的关系:
1613+
1614+
deallocate实现的源码:
1615+
1616+
template <class T>
1617+
inline void _deallocate(T* buffer)
1618+
{
1619+
::operator delete(buffer); //为什么不用 delete [] ? ,operator delete 区别于 delete
1620+
//operator delete 是一个底层操作符
1621+
}
1622+
1623+
destory:
1624+
1625+
template <class T>
1626+
inline void _destory(T *ptr)
1627+
{
1628+
ptr->~T();
1629+
}
1630+
1631+
destory负责调用类型的析构函数,销毁相应内存上的内容(但销毁后内存地址仍保留)
1632+
1633+
deallocate负责释放内存(此时相应内存中的值在此之前应调用destory销毁,将内存地址返回给系统,代表这部分地址使用引用-1)
1634+
1635+
1636+
1637+
16111638
#### relational operators (vector)
16121639
#### swap (vector)
16131640
#### vector <bool>

0 commit comments

Comments
 (0)