Update Pointers_C++.md

This commit is contained in:
Aakash Panchal
2020-05-17 14:08:58 +05:30
committed by GitHub
parent 3c34bfa2fc
commit 7af44a9ea8

View File

@@ -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).