Advertisement
ruhan008

Untitled

Oct 3rd, 2023
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. //Q1) Create an structure and demonstrate the use of dynamic memory allocation.
  2.  
  3. #include <iostream>
  4. #include<stdlib.h>
  5. using namespace std;
  6.  
  7. struct dynamic{
  8.     string statement;
  9.     int size;
  10. };
  11.  
  12. int main(){
  13.    
  14.     dynamic* object=(dynamic*)malloc(sizeof(dynamic));
  15.  
  16.     object->statement="Dynamically allocated memory of size: ";
  17.     object->size=sizeof(dynamic);
  18.  
  19.     cout<<object->statement<<" "<<object->size<<endl;
  20.    
  21.     free(object);
  22.     object=NULL;
  23.     cout<<"Memory released!!!"<<endl;
  24.     return 0;
  25. }
  26. /*
  27.     OUTPUT:
  28.  
  29.     Dynamically allocated memory of size:  40
  30.     Memory released!!!
  31.  
  32. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement