0
votes

I'm just learning to program. We just covered variables. We were taught that they reference an address in memory. What is the size of that address? That is, are address 13,221 and address 13,222 separated by 8 bits? 16? 32? Does every individual bit have an address?

If the size varies, is the specific size determined by hardware? Disk format? Operating system? Compiler/interpreter?

If I'm only storing a byte of data in a 16 bit space, how is that implemented? Is it done by defaulting the leftmost 8 bits to 0, like writing '12' as '0012' by way of a base 10 comparison? Is the size flexible somehow to utilize those unused chunks of storage space?

2

2 Answers

1
votes

are address 13,221 and address 13,222 separated by 8 bits? 16? 32?

8 bits, almost always

Does every individual bit have an address?

No, only bytes. One uses other means to get the value of specific bits.

If the size varies, is the specific size determined by hardware? Disk format? Operating system? Compiler/interpreter?

Size does not vary and it is deeply hardware dependent.

If I'm only storing a byte of data in a 16 bit space, how is that implemented? Is it done by defaulting the leftmost 8 bits to 0, like writing '12' as '0012' by way of a base 10 comparison?

I do not really get what you mean by a 16 bits space. If you are considering a 2 bytes memory, every byte has an address (as in every memory) and if a byte is written, it is written at the address given by the computer. So in a 16 bits memory, there are two bytes at addresses 0 and 1.

But maybe you meant "if I have the address of 16 bits word and I use it to write a byte, where will it be written?". Actually, it depends on the computer. In some computers, address of the word is the address of its least significant byte (they are called little endian and x86 architecture has this charactristic), while for others at the address of the word is its most significant byte and the least significant one is at next address (big endian computers). Many computers can select their endianness at boot time. Actually, it a matter of convention (like writing from right to left or from left to right), but it has deep programming implications. Writing a byte (charin C) at a the address supposed to hold 2 bytes (short in C) make programs non portable and is strongly discouraged, unless you really know what you do. If the data type is properly respected, you do not have to worry on the way bytes are arranged in memory.

Is the size flexible somehow to utilize those unused chunks of storage space?

Computers allocate space for variables. Depending on the kind of variable, you have specific space reserved. For instance, in C, a char is stored in a byte, a short in two bytes, an int in 4 bytes most of the time, etc. This insures that whatever value get this variable, if it respects the constraints of its type (for instance an unsigned char must be between 0 and 255), it will be properly written in the memory. So it is not "unused" space. Adding "flexibility", would render memory management completely unworkable.

0
votes

For certain aspects, your question is too broad, as the memory layout depends first of all by the hardware, then by the operating system restrictions, then by the compiler, and finally by the software you write. In Intel /AMD platform, an address targets a specific byte. Each bye is composed by 8 bits. Intel and and platforms are little endian, so the least significant bit comes first.

A register is the basic "variable" of the hardware. Registers are of a fixed size. When you store fewer bytes than the register size is up to you to decide what is going on: the hardware instruction you use determines whether the sign bit will be extended, or the remaining bits will be reset...

When you store a small number in a wide variable, the compiler, according to the the type of the operands, will choose the appropriate hardware instructions to perform what you asked. For this reason, you can store the byte 5 in an int32 variable: the compiler will use the "byte to byte" store instruction which will set to zero all the extra 3 bytes.