Justiceq123
Justiceq123

Reputation: 9

How are things stored in memory

I just started learning C and I learned that every byte of memory has an address.

My question is, what is the address of an integer that takes 2 bytes of memory?

What if we have some data that takes 9 bits of memory? I think of memory as a long block of boxes of size 1 byte with an address. 9 bits occupies one box and 1/9 of the next one, what happens to the remaining 8/9 of the box?

Upvotes: 0

Views: 208

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 224310

The 2018 C standard says in 6.2.6.1 2:

Except for bit-fields, objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified [in the C standard] or implementation-defined.

The standard allows implementations flexibility with bit-fields. Generally, consecutive bit-fields may share parts of bytes, or the implementation may separate them and leave some bits unused.

Taking the address of an object in C yields a pointer. Pointers in C point to whole objects; no distinction is made for which byte they point to, except for objects that are themselves single bytes, of course. When a pointer is converted to a pointer to a character type, the result is a pointer to the lowest-addressed byte in the object (per 6.3.2.3 7). However, this does not mean the original pointer was represented with that address.

Upvotes: 2

Related Questions