码迷,mamicode.com
首页 > 编程语言 > 详细

Modern C++ Course [Lecture 7] {Pointers, const with pointers, Stack and Heap, Memory leaks, Dangling pointers}

时间:2019-01-01 12:31:54      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:can   分享图片   memory   c++11   tps   ons   ott   opera   ted   

技术分享图片

 

技术分享图片

https://en.cppreference.com/w/cpp/language/range-for

 

 技术分享图片

 

 

 技术分享图片

 

 技术分享图片

every object has a pointer to itsleft.

 

 

 技术分享图片

 

 

 技术分享图片

 

 

 

技术分享图片

stack operations are very quick

it‘s computationally expensive to search variables in a stack, when the scopes and functions are very large. 

 So, keep your scope and func small.

 

 

An example: 

 技术分享图片

then we create a new scope

技术分享图片

技术分享图片

技术分享图片

end of scope, and the scope does 2x pop, and remove everything from the first parenthesis

 (beauty of C++ is that it manages its memory based on scopes)

the problem now is that prt loses its value

技术分享图片

 

 

 

技术分享图片 

heap is much more complicated than stack

heap usually has much larger volume than stack

memory management is not automatically done.

 

 

技术分享图片

?????

技术分享图片

 

 技术分享图片

 

 技术分享图片

 

 技术分享图片

 eg, first we assign ptr 1 and 2 to two different velues

技术分享图片

then we assign ptr 2 to the address of the upper variable

技术分享图片

the system thinks we might use the bottom variable, but we have lost access to it, and we can‘t remove it by delete some_ptr

 

技术分享图片

we tried to delete some memory twice.

技术分享图片

 

 

 技术分享图片

in line 10, every time we allocate a new data, system creates a new ptr to it. we can‘t use line 16 to free the memory

 

 

技术分享图片

 

 

技术分享图片

 

 技术分享图片

 

 技术分享图片

 

 

 技术分享图片

 

 

this is the way before c++11

 

技术分享图片

 

 

 技术分享图片

 

 技术分享图片

 

Modern C++ Course [Lecture 7] {Pointers, const with pointers, Stack and Heap, Memory leaks, Dangling pointers}

标签:can   分享图片   memory   c++11   tps   ons   ott   opera   ted   

原文地址:https://www.cnblogs.com/ecoflex/p/10204432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!