Show memory state sample shares

  • 2020-04-02 02:11:20
  • OfStack


#include <stdio.h>
void memstat(void *memory, size_t memsize) {
 printf("-------memory------ ----n");
 for(int i=0; i<memsize; i++) {
  char *p = (char*) memory;
  printf("%d%d%d%d "
    , 0x1 & p[i] >> 3
    , 0x1 & p[i] >> 2
    , 0x1 & p[i] >> 1
    , 0x1 & p[i]);

  if(i % 4 == 3) {
   #define FILTER(c) ((c)<' '? '.': (c))
   printf("%c%c%c%c"
     , FILTER(p[i-3])
     , FILTER(p[i-2])
     , FILTER(p[i-1])
     , FILTER(p[i]));
   puts("");
  }
 }
 printf("------------------- ----n");
}
int main(int argc, char **argv) {
 int memory[8];

 memset(memory, -1, sizeof(memory));

 memory[0] = 3;
 memory[2] = 0xAAAAAAAA;
 memory[3] = 0;

 strcpy((char *)&memory[4], "= =  .  ");

 memstat(memory, sizeof(memory));
}



Related articles: