print bits of a void pointer

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

    void print_memory(void *ptr, int size)
    { // Print 'size' bytes starting from 'ptr'
        unsigned char *c = (unsigned char *)ptr;
        int i = 0;
    
        while(i != size)
            printf("%02x ", c[i++]);           
    }
    

    As H2CO3 mentioned, using uninitialized data results in undefined behavior, but the above snipped should do what want. Call:

    print_memory(p, 24);
    

提交回复
热议问题