mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-09-13 05:42:12 +00:00
Update Pointers_C++.md
This commit is contained in:
@@ -941,7 +941,7 @@ Ex. `int *ptr = new int;`
|
|||||||
Ex. `int *ptr_arr = new int[10];`
|
Ex. `int *ptr_arr = new int[10];`
|
||||||
Note that length can be any value you provide at run time or compile time.
|
Note that length can be any value you provide at run time or compile time.
|
||||||
|
|
||||||
The way `new` operator works is that it first checks whether a computer has free memory of the amount requested by a programmer.
|
The way `new` operator works is that it first checks whether a computer has free memory of the amount requested by a programmer.
|
||||||
|
|
||||||
If yes, then it will return the address of that available memory. In case that much memory is not available, it will result in `bad_alloc` exception followed by termination of the program.
|
If yes, then it will return the address of that available memory. In case that much memory is not available, it will result in `bad_alloc` exception followed by termination of the program.
|
||||||
|
|
||||||
@@ -1011,6 +1011,8 @@ A memory leak happens when your program loses the address of dynamically allocat
|
|||||||
|
|
||||||
**Solution:** Delete(deallocate memory) before anything goes wrong.
|
**Solution:** Delete(deallocate memory) before anything goes wrong.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Dangling pointer
|
### Dangling pointer
|
||||||
|
|
||||||
A pointer that is pointing to a deallocated memory is called a **dangling pointer**. Dereferencing or again deallocating a dangling pointer will lead to undefined behavior(run-time error).
|
A pointer that is pointing to a deallocated memory is called a **dangling pointer**. Dereferencing or again deallocating a dangling pointer will lead to undefined behavior(run-time error).
|
||||||
|
Reference in New Issue
Block a user