CIS3355:
Business Data Structures |
7.30 How is a struct stored in RAM and how do we determine the address of each component? A struct is a distinct data type which consists of a collection of many data types. Structured data objects give the ability to keep information such as an employee's name, date of birth, and salary all together in a single variable. Structs are similar to other data types such as arrays, it requires contiguous bytes of RAM for storage. The one key difference is that a struct does not have to contain the same data types throughout. This tutorial will explain how a struct is stored in RAM and how can each components address can be determined.
The database is a common structure used to maintain valuable information for many establishments. Structures are useful for databases because it offers a way of storing many different variables and types under the same name. Assume you work for a credit card company and you are asked to design a database for each of its customers. The database below is information that may be required to be maintained by a credit card company.
What types of data are represented here and how may bytes are required? This database is very simple and consists of two data types char and float. A customer ID number is required to distinguish each customer from another. For each record we will require a total of 136 bytes of contiguous storage . For easier manipulation ,a null character is required for all variables within the struct. So consequently instead of 2 bytes for storage of the state we require 3 bytes.
How is the srtuct stored in RAM and How do we determine the addresses of each component?
Assuming a base address of 1000 in RAM the following may be present:
Determining the address of each component is rather simple after the base address is provided. Examine each of the fields and determine how many bytes each field requires. Assuming a base address of 1000 we know the first 9 bytes consists of the character array for customer ID. At the address of 1010 begins the character array for the name of the customer, which requires 31 bytes of storage, ends at address1040. The pattern continues until we reach the ending array for the credit balance whose beginning address is 1134 and ending address is 1137. |