#ifndef MEMUTIL_H_INCLUDED
#define MEMUTIL_H_INCLUDED
// Show memory
void showMem(void *, unsigned);
// Setup memory
int setMem(void *, const char *);
#endif // MEMUTIL_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Display memory
void showMem(void *p, unsigned size)
{
char *buf = 0;
int prs = 0;
unsigned i;
buf = (char *)malloc(size * 9);
printf("Show %p, count %u bits.\n", p, size * 8);
for (i = 0; i < size; i++)
{
char ch = ((char *)p)[i]; // Get char p[i]
int j;
for (j = 0; j < 8; j++) // p[i] to 8 bit unsigned int
{
unsigned tmp = 0;
tmp = ch >> (8 - j - 1) & 1;
sprintf(&buf[prs], "%u", tmp % 2);
prs = strlen(buf);
}
sprintf(&buf[prs], " ");
prs++;
}
buf[prs - 1] = '\0';
puts(buf);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MemUtil.h"
// Setup memory
// Return:
// 0 -Setting succeed
// !0 -Setting failed
int setMem(void *p, const char *c)
{
int le;
char *str = 0; // 去除空格的副本
int i;
int j;
char ch;
le = strlen(c);
i = le - 1;
str = (char *)malloc(le * 8 / 9 + 2);
str[0] = '\0';
// 去除空格
i = 0;
while ((ch = *(char *)c++) != '\0')
{
if (ch == ' ')
continue;
if (ch == '1' || ch == '0')
{
str[i++] = ch;
str[i] = '\0';
} else {
printf("错误, 未知的字符: %c.", ch);
return !0;
}
}
if ((le = strlen(str)) % 8 != 0)
{
printf("拒绝执行, 长度错误: %d.\n", le % 8);
return !0;
}
i = 0;
j = le / 8;
for (; i < j; i++)
{
char tmp = 0;
int k = 0;
for (; k < 8; k++)
{
tmp |= (str[i * 8 + k] ^ 0x30) << (7 - k);
}
((char *)p)[i] = tmp;
}
return 0;
}
上面这些代码看似可以工作, 然而!!!!!!!!!!!
就在某一天我准备使用它打造一个二进制文件工具的时候, 发生了爆炸!!!!!!! showMem 处理 200 KB居然耗时 80 s !!!!!!!!!!!!!!!!!!!! 当然其中 puts 占用了绝大部分"功劳", 但垃圾代码是没有资格找借口的!
吃够了没文化的苦头, 我开始了积极的代码优化, 我发誓再也不敢乱写了.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 6 char *showMem(void *p, unsigned size) 7 { 8 char *buf = 0; 9 int prs = 0; 10 unsigned i = 0; 11 unsigned j = 0; 12 char ch = '\0'; 13 unsigned tmp = 0; 14 15 if ((buf = (char *)malloc(size * 9)) == 0) 16 { 17 fprintf(stderr, "Execute failed, PC have not memory.\n"); 18 return 0; 19 } 20 21 printf("Show %p, count %u bits.\n", p, size * 8); 22 for (i = 0; i < size; i++) 23 { 24 ch = ((char *)p)[i]; // Get char p[i] 25 26 27 for (j = 0; j < 8; j++) // p[i] to 8 bit unsigned int 28 { 29 tmp = ch >> (7 - j) & 1; 30 buf[prs++] = (tmp == 0 ? 0x30 : 0x31); 31 } 32 buf[prs++] = ' '; 33 } 34 buf[prs - 1] = '\0'; 35 puts(buf); 36 return buf; 37 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #include "MemUtil.h" 6 7 // Setting memory 8 // Return: 9 // 设置完成的字节数 10 // -1 代表错误 11 int setMem(void *p, const char *c) 12 { 13 int le = 0; 14 char *str = 0; // 去除空格的副本 15 int i = 0; 16 int j = 0; 17 char ch = 0; 18 19 le = strlen(c); 20 str = (char *)malloc(le); 21 22 // 去除空格和换行 23 while ((ch = *(char *)c++) != '\0') 24 { 25 if (ch == ' ' || ch == '\n') 26 continue; 27 if (ch == 0x31 || ch == 0x30) 28 { 29 str[i++] = ch; 30 } else { 31 printf("Oops, dead character %c.", ch); 32 return -1; 33 } 34 } 35 str[i] = '\0'; 36 37 if ((le = strlen(str)) % 8 != 0) 38 { 39 printf("You setting data is fucking, 错误的余数 %d.\n", le % 8); 40 return -1; 41 } 42 43 i = 0; 44 j = le / 8; 45 for (; i < j; i++) 46 { 47 char tmp = 0; 48 int k = 0; 49 50 for (; k < 8; k++) 51 { 52 tmp |= (str[i * 8 + k] ^ 0x30) << (7 - k); 53 } 54 ((char *)p)[i] = tmp; 55 } 56 return j; 57 }