#include "stdafx.h" #include #include using namespace std; /* typedef int * intp; // intp *D; // D[i] = new int[4]; */ int main() { int n; cout << "How many elements?"; cin >> n; int *A; // A[2] = 56; // accessing a location in memory that is not yours A = new int[n]; if (A == NULL) { cout << "Out of mem!" << endl; exit(0); } A[2] = 56; // accessing a location in memory that is yours now // ... delete [] A; // return the memory to the operating system // allocate 1D array using the C syntax int *B; B = (int *) malloc(n * sizeof(int)); if (B == NULL) { cout << "Not enought mem" << endl; exit(0); } B[23] = 6; //... free(B); // allocate 2D array using C++ syntax int *C; C = new int[10*5]; // C[2][3] = 67; C[2 * 5 + 3] = 67; delete[] C; // version 2 int **D; D = new int *[5]; for (int i = 0; i < 5; i++) { D[i] = new int[4]; //.. D[i] == NULL? } D[2][3] = 67; for (int i = 0; i < 5; i++) { delete [] D[i]; } delete[] D; return 0; }