CIS3355:
Business Data Structures |
How are Integers stored in RAM? Your computer's memory can be viewed as a series of cubbyholes. Each cubbyhole is one of many such holes all lined up. Each cubbyhole or memory location is numbered sequentially. These numbers are known as memory addresses. A variable reserves one or more addresses in which a binary value is stored. Each address is one byte (8 bits) large. If we have the variables “a & b” and want to initialize them (assign them values) we could declare the following statement: char a=97, b=98; In this statement the data type char is requesting 1 byte (8 bits) of available memory for each of the variables (a & b) illustrated in the diagram below. The values for a & b were randomly assigned to addresses 100 and 102 respectively. An Integer (int) is stored in RAM in the same manner as a character it only requires more space and contiguous memory. Depending on the type and age of the machine you are using a short integer is two bytes, a long integer is usually four bytes, and an integer (without the keyword short or long) can be two or four bytes. To make some sense of this a short must be less than or equal to the size of an int, which in turn must be less than or equal to the size of a long. The size of an integer is determined by the processor (16 bit or 32 bit) and the compiler you use. On modern 32-bit (Pentium) computers using modern compilers (for example, Visual C++ 4 or later), integers are four bytes. That said, for ease of simplicity and to follow class protocol, lets assume an int is 2 bytes (16 bits) as illustrated below in the statement declaration int c=99;
You will notice that address 101 was not used even though it is available. Remember contiguous memory is needed for integers and all data types above 1 byte. We could not place part of the integer in address 101 and 103 as this would not be contiguous. When referring to a variable address for any variable we refer to the base address where the variable starts in this case 103 would be the base address for int c. Test Your Knowledge (answers at bottom of page): 1. What is the difference between storing a character versus an integer in RAM? 2. Can an integer be stored in any available space in RAM? 3. How is a variable address referred to in RAM? Determine the Size of Variable Types on Your Computer (here's some quick code to key in and list them): #include <iostream> int main() { using std::cout; cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n"; cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n"; cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n"; cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n"; cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n"; cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n"; cout << "The size of a bool is:\t" << sizeof(bool) << " bytes.\n"; return; } Related Links and references: http://www.tvcc.cc/staff/fuller/cs160/chap1/chap1.html http://www.kelso.scotborders.sch.uk/computing/sgsystems/SYS3B.HTM http://www.kelso.scotborders.sch.uk/computing/sgsystems/sys3b.htm#integers http://calc.utep.edu/pkirs/3355/Lecture Slides/CH3 RAM Allocation.ppt Answers to Test Your Knowledge Questions. 1. An integer is stored in the same manner as a character in RAM. An integer however requires more space 2 or 4 bytes depending on machine as well as contiguous memory to accommodate the integer value of the variable. 2. Yes and NO, Integers can be stored in any available space as long as the addresses in memory are contiguous from the beginning byte to ending byte. 3. A variable address is referred to as the base location of the variable. If you have an integer that is occupying addresses 100 & 101 the variable address would be 100.
|