-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic array.cpp
More file actions
36 lines (28 loc) · 844 Bytes
/
dynamic array.cpp
File metadata and controls
36 lines (28 loc) · 844 Bytes
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
35
36
#include <iostream>
int main()
{
int *numbers = new int[5]; //initialize with garbage values
//delete
delete []numbers;
numbers = nullptr;
int *numbers2 = new int[5]{}; //initialize with 0
//access dynamic array
for(int i=0; i<5; i++){
std::cout << numbers2[i] << "," << *(numbers2+i) << std::endl ;
}
std::cout << "\n\n";
delete []numbers2;
numbers2 = nullptr;
int *numbers3 = new int[10]{1,2,3,4,5}; //initialize fist 5 and rest with 0
//access dynamic array
for(int i=0; i<10; i++){
std::cout << numbers3[i] << "," << *(numbers3+i) << std::endl ;
}
delete []numbers3;
numbers3 = nullptr;
//
int *numbers4 = new(std::nothrow) int[]{1,2,3,4} ;//without exceptions
std::cout << numbers4[1];
delete []numbers4;
numbers4 = nullptr;
}