Advertisement
Python253

c_array_mem_allocation_test

Apr 6th, 2024
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. // Filename: c_array_mem_allocation_test.cpp
  2. // Version: 1.0.0
  3. // Author: Jeoi Reqi
  4.  
  5. /*
  6. DESCRIPTION:
  7. This C++ program demonstrates dynamic memory allocation by prompting the user to input the size of an array,
  8. creating an array of that size dynamically, filling it with random integers, and then deallocating the memory.
  9.  
  10. Author: [Author's Name]
  11. Date: [Date of Creation]
  12.  
  13. REQUIRED LIBRARIES:
  14. - iostream: For input and output operations.
  15.  
  16. USAGE:
  17. - Compile and run the program.
  18. - Input the size of the array when prompted.
  19. - The program will generate random integers and output them.
  20. */
  21.  
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. int main() {
  26.     int size;
  27.  
  28.     cout << "Enter the size of the array (1-1000): ";
  29.  
  30.     cin >> size;
  31.  
  32.     int* ptr = new int;
  33.  
  34.     int* arr = new int[size];
  35.    
  36.     for (int i = 0; i < size; i++) {
  37.         arr[i] = rand() % 1000;
  38.         cout << arr[i] << " ";
  39.     }
  40.  
  41.     delete ptr;
  42.  
  43.     delete[] arr;
  44.  
  45.     return 0; // Return 0 to indicate successful program execution
  46. }
  47.  
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement