Which keyword is used to allocate dynamic memory in C++?
1 Crore+ students have signed up on EduRev. Have you? Download the App |
Which operator is used to dynamically allocate memory for a single object in C++?
What happens if we forget to deallocate the memory allocated using new?
Which operator is used to deallocate memory allocated for a single object in C++?
What will be the output of the following code snippet?
int* ptr = new int(5);
cout << *ptr << endl;
delete ptr;
cout << *ptr << endl;
What will be the output of the following code snippet?
int* ptr = new int[3]{1, 2, 3};
cout << *ptr << endl;
delete[] ptr;
cout << *ptr << endl;
What will be the output of the following code snippet?
int a = 10;
int& ref = a;
int* ptr = &ref;
cout << *ptr << endl;
What will be the output of the following code snippet?
void changeValue(int* ptr) {
*ptr = 20;
}
int main() {
int value = 10;
changeValue(&value);
cout << value << endl;
return 0;
}
What will be the output of the following code snippet?
void changeArray(int arr[]) {
arr[0] = 5;
}
int main() {
int arr[3] = {1, 2, 3};
changeArray(arr);
cout << arr[0] << endl;
return 0;
}
What will be the output of the following code snippet?
```cpp
int** matrix = new int*[2];
for (int i = 0; i < 2; i++) {
matrix[i] = new int[3];
for (int j = 0; j < 3; j++) {
matrix[i][j] = i + j;
}
}
cout << matrix[1][2] << endl;
```
What will be the output of the following code snippet?
```cpp
int** matrix = new int*[3];
for (int i = 0; i < 3; i++) {
matrix[i] = new int[2];
for (int j = 0; j < 2; j++) {
matrix[i][j] = i * j;
}
}
cout << matrix[2][1] << endl;
```
What will be the output of the following code snippet?
```cpp
void changeValue(int** matrix) {
matrix[0][1] = 5;
}
int main() {
int** matrix = new int*[2];
for (int i = 0; i < 2; i++) {
matrix[i] = new int[2];
for (int j = 0; j < 2; j++) {
matrix[i][j] = i + j;
}
}
changeValue(matrix);
cout << matrix[0][1] << endl;
return 0;
}
```
What will be the output of the following code snippet?
```cpp
void changeValue(int** matrix) {
matrix[0] = new int[2]{5, 5};
}
int main() {
int** matrix = new int*[2];
for (int i = 0; i < 2; i++) {
matrix[i] = new int[2]{1, 1};
}
changeValue(matrix);
cout << matrix[0][1] << endl;
return 0;
}
```
What will be the output of the following code snippet?
```cpp
void changeValue(int*** matrix) {
matrix[1][0][0] = 10;
}
int main() {
int*** matrix = new int**[2];
for (int i = 0; i < 2; i++) {
matrix[i] = new int*[2];
for (int j = 0; j < 2; j++) {
matrix[i][j] = new int[2]{i, j};
}
}
changeValue(matrix);
cout << matrix[1][0][0] << endl;
return 0;
}
```
153 videos|115 docs|24 tests
|
153 videos|115 docs|24 tests
|