CPP   60
dump memory
Guest on 11th February 2023 01:19:03 AM


  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <ctype.h>
  4.  
  5. void dump_memory(int type, void *address, int n) ;
  6. int main()
  7. {
  8.     int i , j ;
  9.     /*char *a , *k ;
  10.     k = a ;
  11.     for( i = 0 ; i<4 ; i++ )
  12.     {
  13.         scanf( "%s" , a ) ;
  14.         *( a + strlen(a) ) = '\0' ;
  15.         a += strlen(a)+1 ;
  16.     }
  17.     dump_memory( 4 , k , 4 ) ;*/
  18.     //char a[15] = { "1\nwe2f\054f5\0fwrg" };
  19.     double a[9] ;
  20.     for( i = 0 ; i<9 ; i++ )
  21.         scanf( "%lf" , &a[i] ) ;
  22.     dump_memory( 2 , a , 9 ) ;
  23.     scanf( " " ) ;
  24. }
  25.  
  26. void dump_memory(int type, void *address, int n)
  27. {
  28.     int i , j ;
  29.     if( type == 1 )
  30.     {
  31.         for( i = 0 ; i<n ; i++ )
  32.         {
  33.             if( i%6 == 0 )
  34.                 printf( "%08X:" , (unsigned int*)address + i ) ;
  35.             printf( " %08X" , *( (unsigned int*)address+i ) ) ;
  36.             if( i%6 == 5 )
  37.                 putchar( '\n' ) ;
  38.         }
  39.     }
  40.     else if( type == 2 )
  41.     {
  42.         for( i = 0 ; i<n ; i++ )
  43.         {
  44.             if( i%4 == 0 )
  45.                 printf( "%08X:" , (double*)address + i ) ;
  46.             printf( " %15e" , *( (double*)address+i ) ) ;
  47.             if( i%4 == 3 )
  48.                 putchar( '\n' ) ;
  49.         }
  50.     }
  51.     else if( type == 3 )
  52.     {
  53.         for( i = 0 ; i<n ; i++ )
  54.         {
  55.             if( i%8 == 0 )
  56.                 printf( "%08X:" , (char*)address + i ) ;
  57.             if( isprint( *( (char*)address+i ) ) )
  58.                 printf( " %-4c" , *( (char*)address+i ) ) ;
  59.             else
  60.                 printf( " 0x%02X" , *( (char*)address+i) ) ;
  61.             if( i%8 == 7 )
  62.                 putchar( '\n' ) ;
  63.         }
  64.     }
  65.     else
  66.     {
  67.         int longest = 0 ;
  68.         for( i = 0 , j = 0 ; i<n ; i++ )
  69.         {
  70.             if( strlen( (char*)address+j ) > longest )
  71.                 longest = strlen( (char*)address+j ) ;
  72.             j += ( strlen( (char*)address+j )+1 ) ;
  73.         }
  74.         for( i = 0 , j = 0 ; i<n ; i++ )
  75.         {
  76.             printf("%08X: %*s\n", ( char* )address + j , longest , ( char* )address + j );
  77.             j += (strlen( (char*)address+j )+1) ;
  78.         }
  79.     }
  80. }

Raw Paste

Login or Register to edit or fork this paste. It's free.