Created
July 17, 2014 23:02
-
-
Save lnrsoft/d620f49af312cca6a6da to your computer and use it in GitHub Desktop.
How to create and delete pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This source code written by Roland Ihasz | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int* MyPointer = nullptr; // new pointer declared | |
MyPointer = new int[3]; // memory dynamically allocated | |
MyPointer[0] = 24; | |
MyPointer[1] = 67; | |
MyPointer[2] = 32; | |
cout << MyPointer[0] << endl; | |
cout << MyPointer[1] << endl; | |
cout << MyPointer[2] << endl; | |
cout << "Releasing dynamically allocated memory..." << endl; | |
delete[] MyPointer; // memory freed up | |
MyPointer = nullptr; // pointer changed to nullptr (null pointer) | |
return 0; | |
} | |
/* | |
Arrays, allocated with new[], must be deallocated with delete[] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment