Skip to content

Instantly share code, notes, and snippets.

@lnrsoft
Created July 17, 2014 23:02
Show Gist options
  • Save lnrsoft/d620f49af312cca6a6da to your computer and use it in GitHub Desktop.
Save lnrsoft/d620f49af312cca6a6da to your computer and use it in GitHub Desktop.
How to create and delete pointers
// 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