Structs & typedef Already seen in tirgul.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Programming in C Chapter 10 Structures and Unions
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Structures Spring 2013Programming and Data Structure1.
Complex Number Module The code given here relies upon ANSI C’s ability to return a struct. Esakov and Weiss use the argument list to return a pointer to.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
. Plab – Tirgul 4 structs & arrays, file I/O, debugging memory errors.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS 261 – Data Structures Introduction to C Programming.
ECE Application Programming
Structs & typedef. Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example:
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Pointers to pointers & multi-dimensional arrays
Generic Programming in C
Chapter 12 Classes and Abstraction
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Functions + Overloading + Scope
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Pointers and Dynamic Arrays
Section 11: Comparing Java and C
Function: Declaration
Chapter 8 Arrays, Strings and 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.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Structs (C,C++) Already seen in tirgul.
Structs (C,C++).
A bit of C programming Lecture 3 Uli Raich.
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data
Motivation and Overview
Introduction to the C programming language
FUNCTIONS In C++.
Chapter 19 Data Structures
Pointers in C.
CSE 374 Programming Concepts & Tools
Functions Separate Compilation
Introduction to Classes
C Basics.
Structured Data Types A structure can be used to combine data of different types into a single (compound) data value. Like all data types, structures must.
Lecture 6 C++ Programming
Templates.
Heterogeneous Data Structures & Alignment
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Machine-Level Programming 6 Structured Data
Machine-Level Programming IV: Data
C Structures, Unions, Bit Manipulations and Enumerations
Introduction to Classes
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Built-In (a.k.a. Native) Types in C++
Lecture 18 Arrays and Pointer Arithmetic
Machine-Level Programming IV: Data
Instructors: Majd Sakr and Khaled Harras
Functions with arrays.
CS111 Computer Programming
References Revisited CSE 333 Summer 2018
Dr Tripty Singh Arrays.
Pointers Pointers point to memory locations
COP 3330 Object-oriented Programming in C++
Programming in C Pointers and Arrays.
References Revisited CSE 333 Autumn 2018
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
Structs (C) Already seen in tirgul.
Structs & typedef Already seen in tirgul.
Presentation transcript:

structs & typedef Already seen in tirgul

Possible Memory Layout Structs Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: Possible Memory Layout struct rec {    int i;    int a[3];    int *p; }; i a p 4 16 Pointers are 4 bytes

Struct initialization structs can be initialized in a way similar to arrays: struct rec { int i; int a[3]; int *p; }; ... int k; struct rec r = { 5, { 0,1,2}, &k }; r.i=1; r.a[0]=5; r.p=&k;

Struct initialization structs can be initialized in a way similar to arrays: struct rec { int i; int a[3]; int *p; }; ... int k; struct rec r = { 5, { 0,1,2}, &k }; r.i=1; r.a[0]=5; Do we really need to write struct rec??? r.p=&k;

typedef Synonyms for variable types – make your program more readable Syntax: Example: typedef <existing_type_name> <new_type_name>; typedef unsigned int size_t; Already seen in tirgul

typedef Synonyms for variable types – make your program more readable struct _Complex {    double _real, _imag; }; typedef struct _Complex Complex; Complex addComplex(Complex, Complex); Complex subComplex(Complex, Complex); complex.h Already seen in tirgul

typedef Synonyms for variable types – make your program more readable typedef struct _Complex {    double _real, _imag; } Complex; Complex addComplex(Complex, Complex); Complex subComplex(Complex, Complex); complex.h Already seen in tirgul

typedef- why? Readibilty: shorter names or dedicated names typedef unsigned int size_t;

typedef- why? Portability: #ifdef INT_4_BYTES typedef int int32; typedef short int16; #else typedef long int32; typedef int int16; #endif

typedef- why? Generic code: e.g. changing numbers in a data structure from char to int easily Not always recommended

Pointers to structs You can have a pointer to structs the same way you have pointers to built in type.

Access to struct members via pointers typedef struct _MyStr {    int _a[10]; } MyStr; main() {    MyStr x;    MyStr *p_x = &x;    x._a[2] = 3;    (*p_x)._a[3] = 5;    p_x->_a[4] = 6; } Just a reminder.

Structs - Code Offset of each structure member determined at compile time r struct rec {    int i;    int a[3];    int *p; }; i a p 4 16 r + 4 + 4*idx int* find_a(struct rec *r, int idx) {    return &(r->a[idx]); }

Alignment in memory Compiler specific. In MSVC 2012 default params: Note: MSVC compiler has flags to control this ! struct S1 {    char c;    int i[2];    double v; }; v i[0] c i[1] P+0 P+4 P+16 P+24

struct Sequential in memory, but may have gaps: sizeof(S1) != sizeof(char)+sizeof(double)+sizeof(int)*2 Member offset determined in compile time Access member with "." e.g. a.i[0], a.v. Access member of struct pointer contents: (*p). or p-> As always, pass by value! struct S1 {    char c;    int i[2];    double v; }; struct S1 a,*p; How would this look under linux gcc? v i[0] c i[1] P+0 P+4 P+16 P+24

Structs – old object oriented design struct Complex {    double _real, _imag; }; struct Complex addComplex(struct Complex, struct Complex); Complex.h Complex.c #include "Complex.h" // implementation struct Complex addComplex(struct Complex a, struct Complex b) { Complex.c #include "Complex.h" int main() {    struct Complex c; ... MyProg.c

Structs – old object oriented design – Better design – more on this later struct _Complex; typedef struct _Complex Complex; Complex* Complex_alloc(); Complex.h Complex.c #include "Complex.h" struct _Complex {double _real, _imag} Complex* Complex_alloc(double real, double imag) {... Complex.c #include "Complex.h" int main() {    Complex* c_ptr= Complex_alloc(3.0, -1.2); ... MyProg.c

#ifndef – for header safety Complex.h: struct Complex { ...      MyStuff.h: #include "Complex.h" Main.c: #include "MyStuff.h" #include "Complex.h" Only one definition is allowed! Error: Complex.h:1: redefinition of `struct Complex'

#ifndef – header safety Complex.h (revised): #ifndef COMPLEX_H #define COMPLEX_H struct Complex { ... #endif Main.c: #include "MyStuff.h" #include "Complex.h" // no error this time

#pragma once – header safety Complex.h (revised): #pragma once struct Complex { ... Main.c: #include "MyStuff.h" #include "Complex.h" // no error this time

Copy structs using ‘=‘: copy just struct values!!! structs copying Copy structs using ‘=‘: copy just struct values!!! Complex a,b; a._real = 5; a._imag = 3; b = a; _real = 5 _imag = 3 a: _real = ? _imag = ? b:

Copy structs using ‘=‘: copy just struct values!!! structs copying Copy structs using ‘=‘: copy just struct values!!! Complex a,b; a._real = 5; a._imag = 3; b = a; _real = 5 _imag = 3 a: b:

Arrays in structs copying struct definition: typedef struct Vec {    double _arr [MAX_SIZE]; } Vec; Vec addVec(Vec, Vec); ... vec.h

Arrays in structs copying copy struct using ‘=‘: Vec a,b; a._arr[0] = 5; a._arr[1] = 3; b = a; _arr = {5,3,?,…} a: _arr = {?,?,?,…} b:

Arrays in structs copying Copy struct using ‘=‘: copy just struct values!!! Vec a,b; a._arr[0] = 5; a._arr[1] = 3; b = a; _arr = {5,3,?,…} a: b:

But !!!

Pointers in structs copying struct definition: typedef struct Vec {    double _arr [MAX_SIZE];    double * _p_arr; } Vec; Vec addVec(Vec, Vec); ... vec.h

Pointers in structs copying Copy structs using ‘=‘: copy just struct values!!! Vec a,b; a._arr[0] = 5; a._arr[1] = 3; a._p_arr = a._arr; b = a; _arr = {5,3,?,…} _p_arr = 0x55 a: _arr = {?,?,?,…} _p_arr = ? b:

Pointers in structs copying Copy structs using ‘=‘: copy just struct values!!! Vec a,b; a._arr[0] = 5; a._arr[1] = 3; a._p_arr = a._arr; b = a; _arr = {5,3,?,…} _p_arr = 0x55 a: b: Pointers copied by value!!!

Pointers in structs copying The result: Vec a,b; a._arr[0] = 5; a._arr[1] = 3; a._p_arr = a._arr; b = a; *(b._p_arr) = 8; printf ("%f", a._arr[0]); // output 8

How to deep copy structs correctly? Implement a clone function: Vec* Vec_clone (const Vec* v) {    ...

Arrays & structs as arguments When an array is passed as an argument to a function, the address of the 1st element is passed. Structs are passed by value, exactly as the basic types. Array == (in run time) pointer to the first element!

Arrays & structs as arguments typedef struct _MyStr {    int _a[10]; } MyStr; void f(int a[]) {    a[7] = 89; } void g(MyStr s) {    s._a[7] = -1; } main() {    MyStr x;    x._a[7] = 0;    f(x._a);    printf("%d\n", x._a[7]);    g(x);    printf("%d\n", x._a[7]); } Output: 89