forked from sinairv/Cpp-Tutorial-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProg.cpp
34 lines (31 loc) · 817 Bytes
/
Prog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// "new" and "delete" operators for performing
// Dynamic Memory Allocation
// Syntax:
/* TypeName *ptrToTypeName;
ptrToTypeName = new TypeName;
if(ptrToTypeName == 0)
cout << "Not Enough Memory!" << endl;
delete ptrToTypeName;
*/
// Syntax for arrays:
/* TypeName *ptrToTypeName;
ptrToTypeName = new TypeName[nCount]; //size_t nCount
ptrToTypeName[i] = SomeValue; //How to assign some value.
delete [] ptrToTypeName; //How to deallocate.
*/
#include <iostream>
using namespace std;
int main( void )
{
float *ptr;
ptr = new float(3.14);
cout << *ptr << endl;
int Size = 10; // using a variable to show it is dynamic.
int *pArray = new int[Size];
pArray[0] = 5;
pArray[8] = 6;
cout << pArray[0] << endl;
cout << pArray[8] << endl;
delete [] pArray;
return 0;
}