Question: CENG 251 Lab 2: Memory For this lab you are to keep a time log just as you did in Lab 1. This time it

 CENG 251 Lab 2: Memory For this lab you are tokeep a time log just as you did in Lab 1. Thistime it is worth 2 marks. If you completed the lab say

CENG 251 Lab 2: Memory For this lab you are to keep a time log just as you did in Lab 1. This time it is worth 2 marks. If you completed the lab say so. If you did not, list the sections you completed. (2). Parts I, II and III should all be separate programs listings. Parts that are reused between the programs such as the declaration of the struct should be placed in a separate include file. Part 1: Exploring the Memory Layout of a Struct (9 marks) In class we explored the example of struct FLIGHT using the sizeof and & operators and the offset function. Write a program to accomplish the following: 1. Create and declare your own data structure consisting representing some kind of idea, ie: book, employer, student, product, beverage, planet, game, document, order, schedule....etc. Your data structure should consist of 6 fields with at least 3 different scalar data types. The field names should be meaningful. None should be pointers. Organize the data structure so that when you recompile it using -fpack-struct the sizeof the whole data structure will change. The easiest was to do this is to put a boolean, short or string with an odd number of characters between any two items from the types float, double, int, long long or long double, ie: struct EXAMPLE { double x; char name[9]; long long account; //This will align on a word boundary } eg: Given these requirements it's unlikely that any two students will have the same declaration. (2) 2. Write a program using the sizeof operator to determine the sizeof each of your fields and the size of the whole data structure. Is the sum of the parts equal to, larger than or smaller than the whole? (2) 3. Use the offsetof function to determine the offsets of each of the fields in your struct. Use this and the sizeof information to draw a data structure diagram. (2 marks) 4. Recompile your program using the -fpack-struct option and redraw the diagram. In words describe where the space saving(2) occurred. (If there is no difference in the size, talk to me. The instructions in #1 are supposed to force a difference in size.) (3) To hand in: Program listing, output, data structure diagram, statement regarding sizes. Part II: Creating a Database from your struct (11 marks) 0. Before starting up vim and editing your code, create an environment variable NCOPIES in the shell using the export command and another called DataBase representing a file name. (no mark) 1. Create and test a function that generates sets of random data for your struct. The example createPeople should give you an idea of how to do this. (3) 2. Create a function that uses fprintf to display your struct to the screen. Pass the structure to your function as an argument either by address or by value. (No global variables.) (2) 3. Write the main program that retrieves NCOPIES and generates that many sets of your random object, displays them as text on the screen and writes them in binary to the file name stored in DataBase. (2) 4. Examine shell script listFields which uses the shell command od (octal dump) to display information in the flight database and adapt it to display all 6 fields for 3-4 records in your database. The listFields example initially only displays 2 fields. We will be modifying either in class or in the lab. You will need the offsets and record size you determined in Part 1. (4) Part III: Address Arithmetic 18 Testing out of bound values Modify the example addressArithmetic3.c by creating an array of 5 values before numbers and a second array after numbers. Fill each with distinct values. Use #include to include the declaration of your datatype from Part I. (1 mark each except for g which is 2 marks) a. Display values from numbers using subscripts from 6 to 9. From which array (if either) do these values come from? b. Display values from numbers using subscripts from-1 to -5. From which array (if either) do these values come from? C. Modify your listing so that instead of using numbers[i] you use the pointer dereferencing notation *(numbers+i) to refer to elements of the array. Is there any change in output? d. Add the following declarations to your code after the 3 arrays: char *newString1=(char * )alloca(100); char newString2[100]; struct yourDataType *item1=(struct yourDataType *) calloc(sizeof (struct yourDataType),5); Print out the value of the pointers newStringi and newString2 Where in memory are they in relation to your arrays? Are they in the stack or the heap? e. Add values from -3 to +3 to both newString1 and newString2 and print out the values. Explain the results in terms of address arithmetic. f. Print out the values item1 and &item1. Where are they in memory in relationship to your arrays? Explain the results in terms of address arithmetic? 8. Set up your program so that it uses both the function and the script mtrace to trace memory allocation. Describe what you did. What does the mtrace shell command tell you about the allocation and freeing of memory in the program? (2 marks) Hand in your completed program (1 listing only) and your writeup. Part IV: Status Report 12 Summarize what you completed in each section. In your time log summarize problems you encountered

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!