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

 

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

 

 

 

 

 

 

 

posted @ 2019-01-01 11:40  ecoflex  阅读(125)  评论(0编辑  收藏  举报