A computer provides a Random Access Memory (RAM) for storing executable program
code as well as the data the program manipulates. This memory can be thought of
as a contiguous sequence of bits, each of which is capable of storing a binary
digit (0 or 1). Typically, the memory is also divided into groups of 8 consecutive
bits (called bytes). The bytes are sequentially addressed. Therefore each
byte can be uniquely identified by its address.
Figure: Bits and bytes in memory.
The C++ compiler generates executable code which maps data entities to memory
locations. For example, the variable definition
int
salary = 65000;
causes the compiler to allocate a few bytes to represent salary. The exact number of bytes allocated and the method used for the binary
representation of the integer depends on the specific C++ implementation, but
let us say two bytes encoded as a 2’s complement integer. The compiler uses the
address of the first byte at which salary is allocated to refer to it. The above assignment causes the value 65000 to
be stored as a 2’s complement integer in the two bytes allocated.
Figure: Representation of an integer
in memory.
While the exact binary representation of a data item is rarely of interest
to a programmer, the general organization of memory and use of addresses for
referring to data items (as we will see later) is very important.
Comments
Post a Comment
Give your valuable Comment...