wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

STRUCTURED DATA OBJECTS

 

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.

Name Address City State Zip Code Phone Number Credit Balance
Buck Rogers 12258 Some Street El Paso TX 79938 915-888-1234 1500.00

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.

Field Customer ID Name Address City State Zip Code Phone Number Credit Balance
Data Type  Char Char Char Char Char Char Char Float
Bytes Required 9 31 51 20 3 6 13 4

How is the srtuct stored in RAM and How do we determine the addresses of each component?

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
1 2 3 4 5 6 7 8 9 \0
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
B u c k space R o g e r
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
s \0 --- --- --- --- --- --- --- ---
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
--- --- --- --- --- --- --- --- --- ---
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
--- 1 2 2 5 8 space S o m
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
e space S t r e e t \0 ---
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
--- --- --- --- --- --- --- --- --- ---
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
--- --- --- --- --- --- --- --- --- ---
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
--- --- --- --- --- --- --- --- --- ---
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
--- --- E L space P a s o ---
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
\0 --- --- --- --- --- --- --- --- ---
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
--- --- T X \0 7 9 9 3 8
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
\0 9 1 5 - 8 8 8 - 1
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
2 3 4 \0 1500.00    
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
                   

 

Assuming a base address of 1000  in RAM the following may be present:

Field Data Type Bytes Required Starting Address Ending Address
Customer ID Char 9 1000 1009
Name Char 31 1010 1040
Address Char 51 1041 1091
City Char 20 1092 1111
State Char 3 1112 1114
Zip Code Char 6 1115 1120
Phone Number Char 13 1121 1133
Credit Balance Float 4 1134 1137

 

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.