On many systems, in this circumstance, it is required that size
be a multiple of four (subject to additional conditions detailed below, including that the size of int
be four bytes on your system). On systems that do not require this, it is usually preferred.
First, the type of head
is void *
, and the C standard does not define what happens when you do pointer arithmetic with void *
.
Some compilers, notably GCC and its heirs, will treat this arithmetic as if the type were char *
. I will proceed on this basis.
Second, I am not aware of a guarantee that sbrk
returns an address with any particular alignment.
Let us suppose that sbrk
does return a well-aligned address, and that your C implementation does the plain thing to evaluate * (int *) (head + size) = value
, which is to issue a store instruction to write the value of value
(converted to an int
) to the address head + size
.
Then your question becomes: What does my computing platform do with an int
store to this address?
As long as head + size
is an address suitably aligned for int
on your platform, the store will execute as expected. On most platforms, four-byte integers prefer four-byte alignment, and eight-byte integers prefer eight-byte alignment. As long as head
is aligned to a multiple of this preference and size
is a multiple of this preference, then the store will execute normally.
Otherwise, what happens depends on your platform. On some platforms, the hardware executes the store but may do it more slowly than normal store instructions, because it breaks it into two separate writes to memory. (This also means that other processes sharing the same memory might be able to read memory while one part of the value has been stored but the other part has not. Again, this depends on the characteristics of your computing platform.)
On some platforms, the hardware signals an exception that interrupts program execution and transfers control to the operating system. Some operating systems fix up misaligned stores by analyzing the failing instruction and executing alternate instructions that perform the intended store (or the operating system relays the exception to special code in your program, possibly in automatically included libraries, that do this fix-up work). On these platforms, misaligned stores will be very slow; they can hugely degrade the performance of a program.
On some platforms, the hardware signals an exception, and the operating system does not fix up the misaligned store. Instead, the operating system either terminates your process or sends it a signal about the problem, which often results in your process terminating. (Other possibilities include triggering a debugger or entering special code you have included in your program to handle signals.)