A
anon.asdf
Hello!
Is it legal to access a 4-byte int at any offset (alignment) in the
following array:
char arr[8] = {0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1};
?? (See question 3 later...)
Example:
printf("%08x\n", *((int *) (arr + 0)));
printf("%08x\n", *((int *) (arr + 1)));
printf("%08x\n", *((int *) (arr + 2)));
printf("%08x\n", *((int *) (arr + 3)));
/*
Output assuming the following
~32 bit natural word size of processor
~little endian: the address of a single byte 'Q' corresponds with the
adress of a 4-byte int only, if that int has this same byte 'Q' as the
LSB
0d0c0b0a
0e0d0c0b
0f0e0d0c
000f0e0d
*/
4 Questions:
* assume 32-bit processor: natural word size is 4 bytes
* alignment: a 4-byte int is aligned if it lies on an address that is
a multiple of 4
1)
~Will "char arr[8]" always be 4-byte aligned (according to the natural
word size)???
~Or will it rather be 1-byte aligned (meaning it can lie where it
wants)???
(thinking ahead...) If not 4-byte aligned, but one desires a 4 byte
alignment, then one would have to use:
int arr_tmp[2];
char *arr = (char *)arr_tmp;
~would that work?
~any other possibilities?
2)
~Will "short sh;" (where short has 2 bytes) always be 4-byte aligned
(according to the natural word size)???
~If not, and one desires it to be 4-byte aligned, one could use:
int tmp;
#define sh ( *((short *)(&tmp)) )
~would that work?
~are there any other possibilities?
3)
*((int *) (arr + 1)) can be an unaligned 4-byte int-access (see
above). As can be seen above, it works with my gcc compiler. I believe
it splits into 2 accesses (that are then combined).
~Will it work on every standards-compliant C compiler (assuming int is
4 bytes)?
//That was a read.
//Now a write:
~ Is this legal:
int i;
*((int *)(arr+1)) = i;
? (It works with gcc!)
Does it depend on the C-compiler? Or on the processor? or...?
4)
(gcc...)
struct struc_a {
char a; /*1 byte */
short b; /*2 bytes*/
char c; /*3 bytes*/
} __attribute__ ((packed));
/*gcc extension*/
/* sizeof(struct struc_a) is 4. */
~Will "struct struc_a tmp" be 4-byte aligned?
~gcc: Could one align "struct struct_a tmp" so that it lies at an
addess x,
where x % 4 = 1 ?
---- Offtopic: ----
Are there any good books that can be recommended on C programming and
in particular how this relates to the actual machine code generated?
Things like:
alignment, byte-ordering, portable C programming, how loops are
implemented in machine code, optimization
I wonder if reading the C standard is beneficial (or in what respect
it would be beneficial)?
I'm really impressed by some people, who quote relevant information
directly from the standard!
Or reading a book on GCC?
Or maby Jens Schweikhardt's book, if it's ever finished:
http://www.schweikhardt.net/#mybook
Or Knuth's output?
?
At this point I think that practical programming combined with
comp.lang.c is the best resource, thanks to some really intelligent
people giving quality comments.
?
But still: I'm pretty sure going to have a *real* look at assembly
language and some debugger tools in the near future.
Thanks -Albert
Is it legal to access a 4-byte int at any offset (alignment) in the
following array:
char arr[8] = {0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1};
?? (See question 3 later...)
Example:
printf("%08x\n", *((int *) (arr + 0)));
printf("%08x\n", *((int *) (arr + 1)));
printf("%08x\n", *((int *) (arr + 2)));
printf("%08x\n", *((int *) (arr + 3)));
/*
Output assuming the following
~32 bit natural word size of processor
~little endian: the address of a single byte 'Q' corresponds with the
adress of a 4-byte int only, if that int has this same byte 'Q' as the
LSB
0d0c0b0a
0e0d0c0b
0f0e0d0c
000f0e0d
*/
4 Questions:
* assume 32-bit processor: natural word size is 4 bytes
* alignment: a 4-byte int is aligned if it lies on an address that is
a multiple of 4
1)
~Will "char arr[8]" always be 4-byte aligned (according to the natural
word size)???
~Or will it rather be 1-byte aligned (meaning it can lie where it
wants)???
(thinking ahead...) If not 4-byte aligned, but one desires a 4 byte
alignment, then one would have to use:
int arr_tmp[2];
char *arr = (char *)arr_tmp;
~would that work?
~any other possibilities?
2)
~Will "short sh;" (where short has 2 bytes) always be 4-byte aligned
(according to the natural word size)???
~If not, and one desires it to be 4-byte aligned, one could use:
int tmp;
#define sh ( *((short *)(&tmp)) )
~would that work?
~are there any other possibilities?
3)
*((int *) (arr + 1)) can be an unaligned 4-byte int-access (see
above). As can be seen above, it works with my gcc compiler. I believe
it splits into 2 accesses (that are then combined).
~Will it work on every standards-compliant C compiler (assuming int is
4 bytes)?
//That was a read.
//Now a write:
~ Is this legal:
int i;
*((int *)(arr+1)) = i;
? (It works with gcc!)
Does it depend on the C-compiler? Or on the processor? or...?
4)
(gcc...)
struct struc_a {
char a; /*1 byte */
short b; /*2 bytes*/
char c; /*3 bytes*/
} __attribute__ ((packed));
/*gcc extension*/
/* sizeof(struct struc_a) is 4. */
~Will "struct struc_a tmp" be 4-byte aligned?
~gcc: Could one align "struct struct_a tmp" so that it lies at an
addess x,
where x % 4 = 1 ?
---- Offtopic: ----
Are there any good books that can be recommended on C programming and
in particular how this relates to the actual machine code generated?
Things like:
alignment, byte-ordering, portable C programming, how loops are
implemented in machine code, optimization
I wonder if reading the C standard is beneficial (or in what respect
it would be beneficial)?
I'm really impressed by some people, who quote relevant information
directly from the standard!
Or reading a book on GCC?
Or maby Jens Schweikhardt's book, if it's ever finished:
http://www.schweikhardt.net/#mybook
Or Knuth's output?
?
At this point I think that practical programming combined with
comp.lang.c is the best resource, thanks to some really intelligent
people giving quality comments.
?
But still: I'm pretty sure going to have a *real* look at assembly
language and some debugger tools in the near future.
Thanks -Albert