{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

07-structures.pdf - Computer Systems Principles C...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Systems Principles C Structures 1 Syllabus/Agenda • Lectures: Structures, Pointers • Homeworks – Assess HW 01 (Java Set) √ – Complete HW 02 (Shell, etc.) √ – Read HW 03 (Bits and Bytes) • Quiz 04 Friday • Exam 01 Tuesday (see next slide) – Like quizzes but more questions 2 Exam 01 • Next Tuesday 10/03/2017 – Covers Week 01 – 04 Material – Review slides, code examples, readings – Review assignments 01 – 03 • Format – Same as quizzes (some questions may be the same) – Timing: 50 minutes in 75 minutes – No trickery, study the parts that you are confused about, scan readings (or read if you haven’t), it will be an honest treatment of the material. 3 Learning Objectives • • • • • To learn and apply C structures To understand a little about alignment To learn and apply C enums To learn and apply C unions To understand and apply C typedef 4 C Structures • Essential – For building up interesting data structures • Definition – A C structure is a collection of one or more variables, typically of different types, grouped together under a single name for convenient handling – Kind of like a Java class with public instance variables and no methods 5 C struct • Defines a new type – A new kind of data type that the compiler regards as a unit or aggregate of variables/types. • Example: struct Date { int day; int month; int year; }; 6 Structure Properties • Individual components of a struct type are called members (or fields). • Members can be of different types (primitive, array, or struct) • A struct is named as a whole while individual members are named using field identifiers • Complex data structures can be formed by defining arrays of structs, etc. 7 More struct Examples • Examples: struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentGrade { char name[25]; char course[9]; int lab[5]; int homework[7]; int exams[2]; }; 8 Declaring a struct Variable • Declaration of a variable of struct type: <struct type> <identifier list>; • Example: struct StudentRecord student1; 25 4 1 8 name id gen gpa 38 bytes 9 student‐01.c example • Let’s compile this example – What do you notice about the output of this program as compared with the previous slide? – Is the size of this struct the same as we predicted? – Why is this or is this not the case? 10 Data Allocation and Alignment • Data Allocation – Each variable definition is allocated bytes in memory according the type of that variable – e.g., char = 1 byte, int = 4 bytes, double = 8 bytes – This is allocated in a special place in memory known as the stack. • Data Alignment – Machines are more efficient if allocated data is on a word boundary. – A word is typically 4 bytes 11 Alignment in Memory Memory Each box is a byte 12 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 Each box is a byte and has a location. Memory is very much like a a giant character array! 13 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 So, how do we allocate the structure definition below? struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord student1; 14 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 We allocate 25 bytes for the character array name. In this example we start from 0, however, we could start from anywhere in memory. struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord student1; 15 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 But, machines are typically more efficient if data is allocated on the start of 4 byte “word boundaries” (every 4th byte) e.g., 4, 8, 12, 16, 20, 24, … That is, the starting memory index for allocating the next data type should be (index % 4) = 0 struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord student1; 16 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 So, the compiler will make the allocation of your structures more efficient by padding the bytes so the next allocation will be 4‐byte aligned! struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord student1; 17 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 So, the compiler will make the allocation of your structures more efficient by padding the bytes so the next allocation will be 4‐byte aligned! struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord student1; 18 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 Next, we allocate bytes for the next type – this is a 4‐byte integer, so it is already aligned properly. struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord student1; 19 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 Next is the character. Will the next allocation be aligned properly? struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord student1; 20 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 Nope! So the compiler will pad this out with 3 additional bytes struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord student1; 21 Memory Alignment in Memory 0 1 … 24 25 26 27 28 29 30 31 32 33 34 35 36 37 … 43 44 45 46 Lastly, we allocate the last field in the structure which is an 8‐byte double which is 4‐byte aligned. So, instead of 38 bytes – we get 44 bytes! struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord student1; 22 What if… We change around the fields in a structure? Let us take a look at this example: student‐01‐alt.c 23 iClicker Question struct StudentRecord { int id; double gpa; char name[25]; char gender; } The size of this struct will be: A. 38 bytes B. 40 bytes C. 44 bytes D. 48 bytes E. None of the above 24 What Happened? • Previously, we had 44 bytes. • Now, we have 40 bytes allocated! • Same fields ?! struct StudentRecord { char name[25]; int id; char gender; double gpa; }; struct StudentRecord { int id; double gpa; char name[25]; char gender; }; Think about this for a moment and write down the memory layout for the alternate struct! 25 Memory Alignment in Memory 2 byte padding 0 1 2 3 4 5 .. 10 11 12 13 .. 36 37 38 39 40 41 42 43 44 45 struct StudentRecord { int id; double gpa; char name[25]; char gender; }; struct StudentRecord student1; 26 Structure Initialization • There are four ways to initialize a struct – Positional initialization – Named initialization – Copy initialization – Initialize individual fields 27 Positional Initialization Positional initialization allows you to provide the values for each of the fields based on the position of each structure member: struct StudentRecord student1 = { “John Doe”, 1234567, ‘M’, 3.95 }; Not a recommended way because it relies on an order that is defined somewhere else and might change. It’s not very mnemonic either. 28 student‐02.c example • Let us compile this example – Notice that each value in the structure initializer is exactly the same position as the structure definition. – Look at how we use the ‘.’ operator to access the individual fields in a structure. This should be reminiscent of how you access fields in Java. 29 Named Initialization Named initialization allows you to provide the values for each of the fields based on the name of each structure member: struct StudentRecord student1 = { .id = 1234567, .gpa = 3.95, .gender = ‘M’, .name = “Harry Potter” }; 30 student‐03.c example • Let us compile this example – Notice that each value in the structure initializer can occur in any position. – Do not forget to use the ‘.’ prefix for each field name in the initializer! 31 Copy Initialization Copy initialization allows you to initialize a structure by assigning an existing structure: struct StudentRecord student1 = { .id = 1234567, .gpa = 3.95, .gender = ‘M’, .name = “Harry Potter” }; struct StudentRecord student2 = student1; 32 student‐04.c example • Let us compile this example – Notice that we can initialize by using the assignment operator ‘=‘. – This will automatically copy the memory from the structure on the right‐hand side to the structure on the left‐hand side. 33 Field Initialization Field initialization allows you to initialize a structure by assigning to its fields: struct StudentRecord student1; student1.id student1.gender student1.gpa = 1234567; = ‘M’; = 3.95; 34 Field Initialization Field initialization allows you to initialize a structure by assigning to its fields: struct StudentRecord student1; student1.id student1.gender student1.gpa student1.name = = = = 1234567; ‘M’; 3.95; “Harry Potter”; What about this one? 35 student‐05.c example • Let us compile this example – What problems do we encounter with this example? – Why can’t we assign a string to a character array? • The string is a char * type • Arrays are not modifiable values, that is, you can’t reassign them to “point” to different locations in memory. • Huh? This will be more clear when we talk about pointers 36 Field Initialization Field initialization allows you to initialize a structure by assigning to its fields: struct StudentRecord student1; student1.id student1.gender student1.gpa student1.name = = = = 1234567; ‘M’; 3.95; “Harry Potter”; So, how do we fix this? 37 strncpy • Copying Strings – #include <string.h> A library for manipulating C strings – To assign a new string value to a C string (e.g., character array) you must use the strncpy function to copy the bytes into the array. 38 Field Initialization Field initialization allows you to initialize a structure by assigning to its fields: struct StudentRecord student1; student1.id = 1234567; Size of the destination student1.gender = ‘M’; student1.gpa = 3.95; strncpy(student1.name, “Harry Potter”, 25); We use the strncpy function! 39 student‐05‐fix.c example • Let us compile this example – Notice that need to specify the number of bytes. • Why do I need to specify the number of bytes? – There is also a strcpy function that does not require the number of bytes. – But, this function is dangerous because it is possible to copy a larger string into a smaller destination array overwriting adjacent memory! – This is called buffer overflow and can be used to exploit system vulnerabilities! 40 student‐06.c example • One last example… – Creating a “constructor” function for creating new structures is a useful pattern in C. Let us compile this example – Notice that we need to specify the number of bytes. • Some new things: – strlen for computing the length of a string – Functions in C are “call by value”! • But, what about passing arrays to functions? 41 Activity! • strlen(char s) strncpy(char dest, char src, int n) – Take a moment to implement these functions! – Work with the people around you! – Write it down on a piece of paper! 42 Activity! • strlen(char s) strncpy(char dest, char src, int n) – Take a moment to implement these functions! – Work with the people around you! – Write it down on a piece of paper! Take a look at string‐functions.c 43 ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern