Why does the value of the address in C and C++ is always even? For example: I declare a variable int x and x has a memory address 0x6ffe1c (in hexa decimal). No matter what. That value is never odd number it is always an even number. Why is that so??
-
Are powers of 2 even or odd? – NathanOliver Oct 14 '19 at 15:46
-
Not a duplicate, but overlaps [Why “any primitive object of K bytes must have an address that is a multiple of K”?](https://stackoverflow.com/questions/53018194/why-any-primitive-object-of-k-bytes-must-have-an-address-that-is-a-multiple-of/53024022#53024022). – Eric Postpischil Oct 14 '19 at 15:48
-
1The address being an odd number would be weird. Usually variables are aligned to addresses that are a power of two, since that's usually what's most efficient for the processor. Different powers of two for different types. An odd number would be an unaligned address (always) and that would just be inefficient (or Impossible, depending on the CPU). – Jesper Juhl Oct 14 '19 at 15:49
-
@JesperJuhl: Objects are usually allocated to addresses that are multiples of certain small powers of two, not to addresses that are powers of two. If objects could only be put at addresses that are powers of two, we could have only 32 objects in a 32-bit system—one at address 1, one at 2, one at 4, one at 8, one at 16, one at 32, one at 64, and so on. – Eric Postpischil Oct 14 '19 at 15:58
-
1@Eric "Objects are usually allocated to addresses that are multiples of certain small powers of two, not to addresses that are powers of two" - what? How is that different than what I said? – Jesper Juhl Oct 14 '19 at 16:02
-
2@EricPostpischil "aligned to" is the same as saying "multiple of" – Patrick Roberts Oct 14 '19 at 16:06
-
@JesperJuhl: “Aligned to a power of two” means an address is arranged so that it is a power of two. “Aligned to a multiple of a power of two” means an address is arranged so that it is a multiple of a power of two (which is only useful if we mean certain powers of two, since one is a power of two). – Eric Postpischil Oct 14 '19 at 16:33
-
@PatrickRoberts: That is, at best, sloppy use of language. Experts of course know what is meant, but that does not help students, who need explicit and correct language to convey the concepts. – Eric Postpischil Oct 14 '19 at 16:33
-
@EricPostpischil nobody says that. When someone says a type is "4-byte aligned" it doesn't mean that value is at the address 4, it means that value is at an address which is a multiple of 4 bytes. Similarly when someone says an address is aligned to a power of two, that means the value stored at an address which is a multiple of a power of two. There's nothing "sloppy" about that and your insistence on pedantry is the only thing muddling the already-sufficient explanation that was given by Jesper. – Patrick Roberts Oct 14 '19 at 16:50
-
1@PatrickRoberts: I agree people use “4-byte aligned” to mean aligned to a multiple of four bytes. I do not agree that “aligned to a power of two” means aligned to a multiple of a power of two. As I stated, it is, at best, a sloppy use of language. The fact that **you** can figure it out, knowing what you know about computers and terminology, does not mean that students can understand it. How would they know just from the words? **The words need to convey the information.** That is what they are for. There is no reason not to be precise about language when teaching. – Eric Postpischil Oct 14 '19 at 17:00
-
1In processors that can address 8-bit quantities, you can have odd and even addresses. Bytes and characters can exist on odd boundaries, depend on the processor. – Thomas Matthews Oct 14 '19 at 18:26
2 Answers
Computer memory is composed of bits. The bits are organized into groups. A computer may have a gigabyte of memory, which is over 1,000,000,000 bytes or 8,000,000,000 bits, but the physical connections to memory cannot simply get any one particular bit from that memory.
When the processor wants data from memory, it puts a signal on a bus that asks for a particular word of memory. A bus is largely a set of wires that connects different parts of the computer. A word of memory is a group of bits of some size particular to that hardware, perhaps 32 bits. When the memory device sees a request for a word, it gets those bits and puts them on the bus, all at once. (The bus for that will have 32 or more wires, so it can carry all the data for one word at one time.)
Let’s continue with the example of 32-bit words. Since memory is grouped into words of 32 bits, each word has a memory address that is a multiple of 32 bits, or four bytes. And every address that is a multiple of four (0, 4, 8, 12, 16, … 4096, 4100, 4104,…) is the address of a word. The processor always reads or writes memory in units of words—that is the only interaction the hardware can do; the processor cannot read individual bytes from memory. If your int
is in a single word, then the processor can get it from memory by asking for that word.
On the other hand, suppose your int
starts at address 99. Then one byte of it is in the word that starts at address 96 (addresses 96 to 99), and three bytes of it are in the word that starts at address 100 (addresses 100 to 103). In order to get your int
, the processor has to read two words and then stitch together bytes from them to make one int
.
First, that is a waste of time. Doing two reads from memory takes longer than doing one read. Second, if the processor has to have extra wires and circuits for doing that, it makes the processor more expensive and use more energy, and it takes resources away from other things the processor could be doing, like adding or multiplying.
So processors are designed to prefer aligned data. They may have components for handling unaligned data, but using those components may take extra time or resources. So compilers are designed to align objects in ways that are preferable for the target architecture.

- 195,579
- 13
- 168
- 312
-
Because of this, C/C++ require types to be aligned (most compilers and CPUs don't care, but it is technically undefined behavior to dereference an unaligned pointer). It is also more work to access an unaligned pointer, so compilers usually align things anyways. – EasyasPi Oct 14 '19 at 20:48
-
@EasyasPi: What makes dereferencing a pointer without correct alignment not defined by the standards is the fact that it cannot be referring to an object of the pointed-to type and therefore violates aliasing rules, rather than alignment rules. In C, the creation of a pointer (by converting from another pointer) without the alignment required for its type has behavior not defined by the standard (C 2018 6.3.2.3 7). In C++, the rules are more complicated. – Eric Postpischil Oct 14 '19 at 21:20
-
1True. However, my explanation also "works" — don't mess with unaligned pointers unless you truly know what you are doing. – EasyasPi Oct 14 '19 at 21:57
-
A great concise answer, which I appreciate. Thanks for all the senior programmers here. – Smart Humanism May 02 '22 at 08:19
Why does the value of the address in C and C++ is always even?
This is not true in general. For example, if you have an array char[2]
, you'll find that exactly one of those elements has an even address, and the other must have an odd address.
I declare a variable int x and x has a memory address 0x6ffe1c
Every type in C and C++ have have some alignment requirement. That is, objects of the type must be stored in an address that is divisible by that alignment. Alignment requirement is an integer that is always a power of two.
There is exactly one power of two that is not even: 20 == 1. Objects with an alignment requirement of 1 can be stored in odd addresses. char
always has size and alignment of 1 byte. int
typically has a higher alignment requirement in which case it will be stored in an even address.
The reason why alignment is important is that there are CPU instruction sets which only allow reading and writing to memory addresses that are aligned to the width of the CPU word. Other CPU instruction sets may support operations on addresses aligned to some fractions of the word size. Further, some CPU instruction sets, such as the x86 support operating on entirely unaligned address but (at least on older models), such operations may be much slower.

- 232,697
- 12
- 197
- 326