struct Copy Operation In C, variables are copied in three situations:

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Programming in C Advanced Pointers. Pointers to Pointers Since a pointer is a variable type, a pointer may point to another pointer.Since a pointer is.
C Basics Computer Organization I 1 May 2010 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell Labs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
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.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
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.
Structs in C Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to.
Dynamic Allocation in C
Yan Shi CS/SE 2630 Lecture Notes
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
Pointers and Dynamic Arrays
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.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
The Three Attributes of an Identifier
C++ Plus Data Structures
CSE 374 Programming Concepts & Tools
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.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Classes with Dynamically Allocated Data
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Pointers, Dynamic Data, and Reference Types
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Data Structures
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
CS148 Introduction to Programming II
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
9-10 Classes: A Deeper Look.
Pointers Pointers point to memory locations
ECE 103 Engineering Programming Chapter 62 Stack Implementation
CSC212 Data Structure - Section RS
Introduction to Data Structures and Software Engineering
Class: Special Topics 2 For classes using memory allocation
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Doubly Linked List Implementation
Programming in C Advanced Pointers.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Destructors, Copy Constructors & Copy Assignment Operators
Pointers, Dynamic Data, and Reference Types
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
Destructors, Copy Constructors & Copy Assignment Operators
Basic Guarantees Right off the bat… what I'm going to describe is what almost certainly happens on contemporary machines, but there is absolutely nothing.
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
Module 13 Dynamic Memory.
Presentation transcript:

struct Copy Operation In C, variables are copied in three situations: - when used as the right side of an assignment operation - when used as a parameter in a function call - when used as the return value from a function struct LocationType { int X, Y; }; typedef struct LocationType Location; . . . Location A; A.X = 1; A.Y = 5; Location B; B = A; // members of A are copied into the // corresponding members of B In most cases, the default copy mechanism for struct types is adequate. CS@VT June 2010 ©2006-10 McQuain, Feng & Ribbens

struct with Deep Content A struct type may have "deep" content: struct PolynomialType { unsigned int Degree; int* Coeff; // dynamically-allocated array }; typedef struct PolynomialType Polynomial; . . . Polynomial P; P.Degree = 2; P.Coeff = malloc(3 * sizeof(int)); Degree: 2 Coeff Note: Coeff is a member of P, but… … the array is NOT a member. P CS@VT June 2010 ©2006-10 McQuain, Feng & Ribbens

struct Variable Initialization bool initPoly(Polynomial* const P, const uint8_t D, const int64_t* const C) { if ( P == NULL || C == NULL ) return false; P->Degree = D; P->Coeff = malloc((P->Degree + 1) * sizeof(int64_t)); if ( P->Coeff == NULL ) { P->Degree = 0; return false; } for (uint8_t i = 0; i <= P->Degree; i++) { P->Coeff[i] = C[i]; return true; Guard against NULL pointer errors Allocate array to hold coefficients Check for allocation failure Copy coefficients CS@VT June 2010 ©2006-10 McQuain, Feng & Ribbens

Copying a struct with Deep Content Copying a struct variable that has "deep" content may have unintended consequences: Polynomial P, Q; P.Degree = 2; P.Coeff = malloc(3 * sizeof(int)); Q = P; Degree: 2 Coeff P When the value of P.Coeff is copied into Q.Coeff, we get an effect of sharing that is rarely desirable… … this is known as the deep copy problem (or the shallow copy problem). Degree: 2 Coeff Q CS@VT June 2010 ©2006-10 McQuain, Feng & Ribbens

What's Wrong with a Shallow Copy? Degree: 2 Coeff P Polynomial P, Q; P.Degree = 2; P.Coeff = malloc(3 * sizeof(int)); P->Coeff[0] = 1; P->Coeff[1] = 2; P->Coeff[2] = 3; Q = P; P->Coeff[2] = 5; // "back-door" // change to Q free(Q->Coeff); // P loses its // coefficients 1 2 3 Degree: 2 Coeff Q CS@VT June 2010 ©2006-10 McQuain, Feng & Ribbens

Making a Deep Copy The usual semantics of assignment would lead you to expect we'd have gotten: Degree: 2 Coeff Degree: 2 Coeff P Q But this is NOT what happens by default. To make this happen, you must explicitly (i.e., via code you write): - copy the "shallow" content from P into Q - allocate new memory for the "deep" content to be copied from P into Q - copy the "deep" content (e.g., the coefficient values in the array) CS@VT June 2010 ©2006-10 McQuain, Feng & Ribbens

Implementing a Deep Copy In C we handle deep copying by implementing an appropriate function: /** * Initializes *Target from *Source as described below. * * Pre: Target != NULL, * Source != NULL, * Source->C[i] initialized for i = 0:Source->Degree * Post: Target->Degree == Source->Degree * Target->Coeff != Source->Coeff * Target->Coeff[i] == Source->Coeff[i] for i = 0:Source->Degree * Returns: false if *Target cannot be properly initialized, true otherwise */ bool copyPoly(Polynomial* const Target, const Polynomial* const Source) { . . . } CS@VT June 2010 ©2006-10 McQuain, Feng & Ribbens

Implementing a Deep Copy The basic steps are relatively straightforward: bool copyPoly(Polynomial* const Target, const Polynomial* const Source) { if ( Source == NULL || Source->Coeff == NULL || Target == NULL ) return false; free(Target->Coeff); Target->Coeff = malloc( (Source->Degree) * sizeof(int64_t) ); if ( Target->Coeff == NULL ) { Target->Degree = 0; } Target->Degree = Source->Degree; for (int i = 0; i <= Source->Degree; i++) { Target->Coeff[i] = Source->Coeff[i]; return true; Guard against NULL pointer errors Deallocate old array in Target, if any Allocate new array for Target Copy Source into Target Could this be simplified by calling initPoly()? If so, how? CS@VT June 2010 ©2006-10 McQuain, Feng & Ribbens