xxxxxxxxxx
/* These are preprocessor directives in C++. They might sound like quite a mouthful,
but they're simply instructions that tell the compiler to include libraries.
These libraries contain a set of pre-written functions and tools, making it easier
to perform various tasks without reinventing the wheel. */
// Library for standard input/output operations
#include <iostream>
// Library for general utilities
#include <cstdlib>
// It is preprocessor directives that allow access to these sets of functions
int main() {
std::cout << "Hello, World!\n"; // cout is imported from iostream
// Allocate memory for an array of 10 integers using new
int* var = new int[10]; // new is used in place of malloc in C++
// Check if memory allocation was successful
if (var == nullptr) {
std::cout << "Memory allocation failed!\n";
return 1; // Exit the program if memory allocation fails
}
// Assign values to the allocated memory
for (int i = 0; i < 10; i++) {
var[i] = i;
}
// Print values stored in allocated memory
for (int i = 0; i < 10; i++) {
std::cout << var[i] << " ";
}
std::cout << "\n"; // Newline for cleaner output
// Free the allocated memory to prevent memory leaks
delete[] var; // delete[] is used to free memory allocated with new
return 0; // Indicate successful program completion
}