Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structs in C Computer Organization I 1 November 2009 ©2006-09 McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.

Similar presentations


Presentation on theme: "Structs in C Computer Organization I 1 November 2009 ©2006-09 McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar."— Presentation transcript:

1 structs in C Computer Organization I 1 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: -supports the creation of user-defined data types - struct types encapsulate data members struct Location { int X, Y; }; But there are vital differences: - struct data members are "public", in fact there is no notion of access control - struct types cannot have function members -there is no concept of inheritance or of polymorphism

2 structs in C Computer Organization I 2 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens A struct Example struct Location { // declare type globally int X, Y; }; int main() { struct Location A; // declare variable of type Location A.X = 5; // set its data members A.X = 6; struct Location B; // declare another Location variable B = A; // copy members of A into B return 0; } Note: -assignment is supported for struct types -type declaration syntax used here requires specific use of struct in instance declarations

3 structs in C Computer Organization I 3 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens Another struct Example struct locationType { // declare type globally int X, Y; }; typedef struct locationType Location; // alias a type name int main() { Location A; // declare variable of type Location A.X = 5; // set its data members B.X = 6; Location B; // declare another Location variable B = A; // copy members of A into B return 0; } Note: -use of typedef creates an alias for the struct type -simplifies declaration of instances

4 structs in C Computer Organization I 4 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens struct Limitations What else is supported naturally for struct types? Not much… -no automatic support for equality comparisons (or other relational comparisons) -no automatic support for I/O of struct variables -no automatic support for deep copy -no automatic support for arithmetic operations, even if they make sense… -can pass struct variables as parameters (default is pass-by-copy of course) -can return a struct variable from a function -can implement other operations via user-defined (non-member) functions

5 structs in C Computer Organization I 5 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens A struct Function Example struct locationType { // declare type globally int X, Y; }; typedef struct locationType Location; // alias a type name void initLocation(Location* L, int x, int y) { (*L).X = x; (*L).Y = y; } Note: -must pass Location object by pointer so function can modify original copy -given a pointer to a struct variable, we access its members by dereferencing the pointer (to get its target) and then using the member selector operator '.' -the parentheses around the *L are necessary because * has higher precedence than. -however, we can write L->X instead of (*L).X. -use of address-of '&' operator in call to create pointer to B // call: initLocation(&A, 5, 6);

6 structs in C Computer Organization I 6 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens Another struct Function Example struct locationType { // declare type globally int X, Y; }; typedef struct locationType Location; // alias a type name Location updateLocation(Location Old, Location Move) { Location Updated; // make a local Location object Updated.X = Old.X + Move.X; // compute its members Updated.Y = Old.Y + Move.Y; return Updated; // return copy of local object; } Note: -we do not allocate Updated dynamically (via malloc ); there is no need since we know at compile time how many we need (1) and we can just return a copy and avoid the cost of a dynamic allocation at runtime -in C, dynamic allocation should only be used when logically necessary

7 structs in C Computer Organization I 7 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens Typical struct Code Organization // header file Location.h contains declaration of type and // supporting functions #ifndef LOCATION_H #define LOCATION_H struct locationType { // declare type globally int X, Y; }; typedef struct locationType Location; // alias a type name Location updateLocation(Location Old, Location Move);... #endif // Source file Location.c contains implementations of supporting // functions #include "Location.h" Location updateLocation(Location Old, Location Move) {... }...

8 structs in C Computer Organization I 8 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens More Complex struct Types // A struct type may contain array members, members of other // struct types, anything in fact: #ifndef QUADRILATERAL_H #define QUADRILATERAL_H #include "Location.h" #define NUMCORNERS 4 struct quadrilateralType { Location Corners[NUMCORNERS]; }; typedef struct quadrilateralType Quadrilateral;... #endif Note: -even though you cannot assign one array to another and you cannot return an array from a function, you can do both of those things with a struct variable that contains an array member -Why?

9 structs in C Computer Organization I 9 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens A C Library Macro For example, given the Location type defined earlier, you could do something like this: offsetof(type, member-designator) expands to an integer constant expression that has type size_t, the value of which is the offset in bytes, to the structure member (designated by member-designator), from the beginning of its structure (designated by type). Location A; int *p = &A.X; // p points to A.Y Location *q = (uint8_t*)p – (uint8_t*)offsetof(Location, X); // q points to A // we need the pointer casts to make the arithmetic work

10 structs in C Computer Organization I 10 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens More Complex struct Types #ifndef SNODE_H #define SNODE_H struct snodeType { void* Elem; typedef struct snodeType* Next; }; typedef struct snodeType SNode; #endif There are two approaches to making a generic list in C: -use a void* member to point to a data object of any type, which loses all ability to do compile-time type checking -embed the node type as a member within the user data object…

11 structs in C Computer Organization I 11 CS@VT November 2009 ©2006-09 McQuain, Feng & Ribbens Another C Macro /* Converts pointer to list element LIST_ELEM into a pointer to the structure that LIST_ELEM is embedded inside. Supply the name of the outer structure STRUCT and the member name MEMBER of the list element. See the Pintos source distribution for examples. */ #define list_entry(LIST_ELEM, STRUCT, MEMBER) \ ((STRUCT *) ((uint8_t *) &(LIST_ELEM)->next \ - offsetof(STRUCT, MEMBER.next))) The pre-processor macro above provides the client a pointer to a user data object when given a pointer to a struct LIST_ELEM object that is contained within the user data object… adapted from the Pintos OS kernel code used in CS 3204.


Download ppt "Structs in C Computer Organization I 1 November 2009 ©2006-09 McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar."

Similar presentations


Ads by Google