|
|
| .. code-block:: cpp | | .. code-block:: cpp |
| | | |
| n | int size = 5; // create an integer | n | int size = 5; // create an integer |
| int *A; // create an array of pointers | | int *A; // create an array of pointers |
| | | |
| n | A = new int[ size ] | n | A = new int[ size ]; // Ask for a certain size for your array |
| | | |
| t | | t | A[0] = 10; // Give values to Array |
| | | A[1] = 20; |
| | | A[2] = 30; |
| | | A[3] = 40; |
| | | A[4] = 50; |
| | | |
| | | // 8 32 32 36 40 |
| | | cout << &A << A << A + 0 << A + 1 << A + 2 |
| | | |
| | | // 10 28 30 |
| | | cout << A[0] << A[1] << A[2] |
| | | |
| | | // 10 10 28 30 |
| | | cout << *A << *A + 0 << A + 1 << * A + 2 |
| | | |
| | | delete []A; |
|
|
| | | |
| | | |
| n | | n | Dynamic Allocation for arrays |
| | | -------------------------------------------- |
| | | |
| t | | t | .. code-block:: cpp |
| | | |
| | | int size = 5; // create an integer |
| | | int *A; // create an array of pointers |
| | | |
| | | A = new int[ size ] |
| | | |
|
|
| .. code-block:: cpp | | .. code-block:: cpp |
| | | |
| n | int *p, *q; | n | int *p, *q; // create two pointers, named p and q |
| | | |
| n | p = new int; | n | p = new int; // |
| | | |
| *p = 100; | | *p = 100; |
| | | |
| delete p; // give memory that p occupied back to operating system | | delete p; // give memory that p occupied back to operating system |
| t | | t | |
| p = NULL; // set p to memory address 0 | | p = NULL; // set p to memory address 0 |
| | | |
| | | delete q; // give memory that q occupied back to operating system |
| | | |
| | | q = NULL; // set q to memory address 0 |
| | | |
| | | |
| | | |
| | | |
|
|
| | | |
| *q = *p; | | *q = *p; |
| n | | n | |
| | | delete p; // give memory that p occupied back to operating system |
| | | |
| t | | t | p = NULL; // set p to memory address 0 |
|
|
| | | |
| *q = 200; | | *q = 200; |
| t | | t | |
| | | q = new int; |
| | | |
| | | *q = *p; |
| | | |
|
|
| the content of p (or the address that p points to) | | the content of p (or the address that p points to) |
| | | |
| n | dynamic memory allocation or alias | n | Dynamic memory allocation or alias |
| ------------------------------------------ | | ------------------------------------------ |
| | | |
| **Useful but dangerous...** | | **Useful but dangerous...** |
| t | | t | |
| .. code-block:: cpp | | .. code-block:: cpp |
| | | |
|
|
| ------------------------------------------ | | ------------------------------------------ |
| | | |
| t | | t | **Useful but dangerous...** |
| .. code-block:: cpp | | .. code-block:: cpp |
| | | |
|
|
| p | | p |
| the content of p (or the address that p points to) | | the content of p (or the address that p points to) |
| t | | t | |
| | | dynamic memory allocation or alias |
| | | ------------------------------------------ |
| | | |
| | | .. code-block:: cpp |
| | | |
| | | int *p, *q; |
| | | |
| | | p = new int; |
| | | |
| | | *p = 100; |
| | | |
| | | q = p; |
| | | |
| | | *q = 200; |
| | | |
|
|
| t | | t | cpp Pointers |
| | | ============== |
| | | |
| | | **if p is a pointer** |
| | | |
| | | &p |
| | | the memory address of p |
| | | |
| | | \*p |
| | | the content that p points to |
| | | |
| | | p |
| | | the content of p (or the address that p points to) |