#include <stdio.h>

/* This example shows the SUN alignment:
 * integers are always located at words (address % 4 = 0),
 * shorts are always located at half words (address % 2 = 0)
 * 
 * If we try to get an integer from a different address, we are in
 * trouble and get a bus error.
 *
 * This problem works on intel design, but not on sparc design.
 */

int main(int argc, char** argv)
{
    //file for output
    FILE *out;
    //we write some hex numbers into a buffer
    unsigned char buffer[5] = {0x12, 0x34, 0x56, 0x78, 0x9a};

    if(argc == 1)
	//open file, standard name output.txt
	out = fopen("output.txt", "w");
    else 
	//open file with given name
	out = fopen(argv[1], "w");
    
    //now we try to read them
    //on intel: 78563412 (little endian)
    //on sun: 12345678 (big endian)
    fprintf(out, "%x\n", *(int *) (buffer));

    //flush output to file, else it gets lost when bus error occurs
    fflush(out);

    //on intel: 9a785634 (little endian)
    //on sun: bus error
    fprintf(out, "%x\n", *(int *) (buffer+1));

    fclose(out);

    return 0;
}
