print bits of a void pointer

后端 未结 6 2193
[愿得一人]
[愿得一人] 2021-01-23 01:52

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?

For example:



        
6条回答
  •  忘掉有多难
    2021-01-23 02:19

    size_t size = 24;
    void *p = malloc(size);
    
    for (int i = 0; i < size; i++) {
      printf("%02x", ((unsigned char *) p) [i]);
    }
    

    Of course it invokes undefined behavior (the value of an object allocated by malloc has an indeterminate value).

提交回复
热议问题