Why memory addresses are even numbers?

c, c++, memory, pointers

Solution

Processors (and thus, compilers) generally like data to be "aligned" to a higher granularity than bytes. For example, since a 32-bit processor deals with numbers in 32-bit chunks, and not 8-bit chunks, it's more efficient for things to be aligned on 32-bit (4 byte) boundaries. In fact, some processors even require a certain level of alignment.

There are lots of other reasons for alignment, such as preventing a word from spanning across multiple memory pages. Compilers will often insert padding inside data structures so that each member of the structure is aligned to a certain multiple (usually powers of 2). Most compilers also allow you to change the alignment settings (using a `#pragma`).

Problem

After noticing in the VS debugger that memory adresses are usually even numbers I tried out the following program (MS Visual Studio 2012) ``` struct noise { int data[3]; }; int _tmain(int argc, _TCHAR* argv[]) { std::vector<noise*> vn; std::vector<std::complex<double>*> vv; for (int i(0); i < 90000; ++i) { std::complex<double> *cur = new std::complex<double>(1, 1); assert( ! ((int)cur&1) ); // assert if adress is not even vv.push_back(cur); if ( ! (i% (rand()%5 + 1)) ) vn.push_back(new noise); } for (std::size_t i(0), ie(vv.size()); i < ie; ++i) { delete vv[i]; } for (std::size_t i(0), ie(vn.size()); i < ie; ++i) { delete vn[i]; } system("PAUSE"); return 0; } ``` It never asserts! How so? Is there a way to make it assert? Are there any uses for this as a sideffect?

Original source