Advertisement
DEKTEN

TWITCH/0321/HEAP_VS_STACK/S_D.C11

Jun 21st, 2021
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1.  
  2.  
  3. #include <stdio.h>  //: for: printf(...)
  4. #include <stdlib.h> //: for: malloc(...)
  5.  
  6. struct MyStruct{
  7.  
  8.     int a;
  9.     int b;
  10. };
  11.  
  12. int main( void ){
  13.  
  14.     //:Allocated On The Stack:
  15.     struct MyStruct my_struct_001;
  16.  
  17.     //:Allocate On The Heap:
  18.     struct MyStruct* my_struct_002=(
  19.         malloc( sizeof( struct MyStruct ) )
  20.     );;
  21.  
  22.     //:Allocate On The Heap:
  23.     //:The way people say you should do it in case the
  24.     //:variable's underlying type changes in the future.
  25.     //:(I think this way is stupid as hell.)
  26.     struct MyStruct* my_struct_003 =((void*)0);
  27.     my_struct_003=(
  28.         malloc( sizeof( my_struct_003 ) )
  29.     );;
  30.  
  31.     my_struct_001.a = 10;
  32.     my_struct_001.b = 11;
  33.  
  34.     my_struct_002 -> a = 20;
  35.     my_struct_002 -> b = 21;
  36.  
  37.     my_struct_003 -> a = 30;
  38.     my_struct_003 -> b = 31;
  39.  
  40.     printf( "[my_struct_001.a]:%d\n" , my_struct_001.a );
  41.     printf( "[my_struct_001.b]:%d\n" , my_struct_001.b );
  42.  
  43.     printf( "[my_struct_002->a]:%d\n" , my_struct_002->a );
  44.     printf( "[my_struct_002->b]:%d\n" , my_struct_002->b );
  45.  
  46.     printf( "[my_struct_003->a]:%d\n" , my_struct_003->a );
  47.     printf( "[my_struct_003->b]:%d\n" , my_struct_003->b );
  48.    
  49.    
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement