打印空指针的位 [英] print bits of a void pointer
本文介绍了打印空指针的位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我创建一个void指针,并为该void指针分配了一段内存,那么如何打印出刚分配的各个位?
If I create a void pointer, and malloc a section of memory to that void pointer, how can I print out the individual bits that I just allocated?
例如:
void * p;
p = malloc(24);
printf("0x%x\n", (int *)p);
我希望上面显示我刚刚分配的24位.
I would like the above to print the 24 bits that I just allocated.
推荐答案
size_t size = 24;
void *p = malloc(size);
for (int i = 0; i < size; i++) {
printf("%02x", ((unsigned char *) p) [i]);
}
当然,它会调用未定义的行为(malloc
分配的对象的值具有不确定的值).
Of course it invokes undefined behavior (the value of an object allocated by malloc
has an indeterminate value).
这篇关于打印空指针的位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文