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

Slides:



Advertisements
Similar presentations
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Lecture 9 Concepts of Programming Languages
Pointers Applications
Abstract Data Types and Encapsulation Concepts
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
C Basics Computer Organization I 1 May 2010 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell Labs.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
CS 261 – Data Structures Introduction to C Programming.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 CS Programming Languages Class 22 November 14, 2000.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
More Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Allocating Arrays Dynamically You allocate an array by.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Abstract Data Types and Encapsulation Concepts
Linked Lists A linked list is a data structure that uses a "chain" of node objects, connected by pointers, to organize a collection of user data values.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to the C programming language
struct Copy Operation In C, variables are copied in three situations:
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
C Basics.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Pointers and References
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Abstract Data Types and Encapsulation Concepts
Java Programming Language
Pointers and References
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Presentation transcript:

structs in C Computer Organization I 1 November 2009 © 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

structs in C Computer Organization I 2 November 2009 © 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

structs in C Computer Organization I 3 November 2009 © 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

structs in C Computer Organization I 4 November 2009 © 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

structs in C Computer Organization I 5 November 2009 © 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);

structs in C Computer Organization I 6 November 2009 © 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

structs in C Computer Organization I 7 November 2009 © 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) {... }...

structs in C Computer Organization I 8 November 2009 © 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?

structs in C Computer Organization I 9 November 2009 © 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

structs in C Computer Organization I 10 November 2009 © 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…

structs in C Computer Organization I 11 November 2009 © 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.