Solution for Exercise 20-6

ex2006

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned char *junk;
    int x;

    /* allocate memory */
    junk = malloc(64);
    if( junk==NULL )
    {
        puts("Unable to allocate memory");
        exit(1);
    }

    /* output the buffer */
    for(x=0;x<64;x++)
    {
        printf("%02X ",*(junk+x));
        if( (x+1) % 8 == 0 )
            putchar('\n');
    }

    return(0);
}

Output

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

Notes

* The output shown above is accurate when memory contains all zero values, which can happen. Otherwise, you see remnants of whatever data lingers in memory.

* The unsigned char data type is best for representing raw bytes.

* At Line 10, the malloc() function isn't typecast because the buffer is examined as raw data. The function's argument is set to 64 for 64 bytes.