S
S
sddvxd2019-05-09 23:14:55
C++ / C#
sddvxd, 2019-05-09 23:14:55

Why is word alignment necessary?

Why does it add efficiency to the program code? What prevents addressing at odd addresses and why the structure

struct block{
char a;
char b;
char* c;
};

takes not 16, but 8 bytes (in a 32-bit program)? If the compiler generates instead of this structure such
struct decoratedblock{
char a;
char zero[1];
char b;
char zero[1];
char* c;
};

It turns out that b can be located at an address that is not a multiple of a 32-bit machine word. Why is there not 3, but 1 null byte between a and b? Please tell me I'm a little confused

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2019-05-09
@sddvxd

Most modern processors read data from memory not in separate bytes, but in blocks of 2-4-8 bytes. Accordingly, if your data is not aligned in memory and the beginning falls into one block and the end into another, then you will need to get two such blocks to read.

S
Sergey Sakhno, 2019-05-10
@Punk_Joker

Because the size of the pointer is 4 bytes, so the rest of the elements are aligned on 4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question