print bits of a void pointer

后端 未结 6 2182
[愿得一人]
[愿得一人] 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:30

    You can't - reading those bytes before initializing their contents leads to undefined behavior. If you really insist on doing this, however, try this:

    void *buf = malloc(24);
    unsigned char *ptr = buf;
    for (int i = 0; i < 24; i++) {
        printf("%02x ", (int)ptr[i]);
    }
    
    free(buf);
    printf("\n");
    

提交回复
热议问题