Creating an Array: Difference between revisions
Content added Content deleted
Line 222: | Line 222: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
Using dynamically-allocated (i.e. [[Heap]]) memory: |
Using dynamically-allocated (i.e. [[Heap]]) memory: |
||
(Warning, deprecated and not safe) |
|||
<lang cpp> const int n = 10; |
<lang cpp> const int n = 10; |
||
⚫ | |||
int* myArray = new int[n]; |
|||
int* myArray = new int[n]; |
|||
⚫ | |||
myArray[0] = 1; |
myArray[0] = 1; |
||
myArray[1] = 2; |
myArray[1] = 2; |
||
delete[] myArray; |
delete[] myArray; |
||
myArray = NULL; |
myArray = NULL; |
||
} catch (std::bad_alloc a) { |
|||
std::cerr << "allocation failed" << std::endl; |
|||
}</lang> |
}</lang> |
||