Kurt Schmidt. October 30, 2018

Similar documents
CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

PRINCIPLES OF OPERATING SYSTEMS

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Final CSE 131B Spring 2004

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

ESC101N: Fundamentals of Computing End-sem st semester

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Understanding Pointers

CSCI 171 Chapter Outlines

Lectures 5-6: Introduction to C

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Lectures 5-6: Introduction to C

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CS 61c: Great Ideas in Computer Architecture

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Compiling and Running a C Program in Unix

Pointers and File Handling

Procedural programming with C

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

a data type is Types

211: Computer Architecture Summer 2016

Lecture 04 Introduction to pointers

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

Pointers, Dynamic Data, and Reference Types

Procedures, Parameters, Values and Variables. Steven R. Bagley

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Arrays and Memory Management

A brief introduction to C programming for Java programmers

Intermediate Programming, Spring 2017*

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

CS201 Some Important Definitions

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

C introduction: part 1

C programming basics T3-1 -

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

High Performance Programming Programming in C part 1

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Contents. A Review of C language. Visual C Visual C++ 6.0

Heap Arrays and Linked Lists. Steven R. Bagley

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Arrays and Pointers (part 1)

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

edunepal_info

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Memory Organization. The machine code and data associated with it are in the code segment

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Dynamic memory allocation (malloc)

Class Information ANNOUCEMENTS

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

In Java we have the keyword null, which is the value of an uninitialized reference type

CSC231 C Tutorial Fall 2018 Introduction to C

CS201- Introduction to Programming Current Quizzes

CSE 333 Midterm Exam 5/9/14 Sample Solution

CSE 374 Programming Concepts & Tools

POINTER AND ARRAY SUNU WIBIRAMA

C mini reference. 5 Binary numbers 12

C Structures & Dynamic Memory Management

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

C for C++ Programmers

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Heap Arrays. Steven R. Bagley

ECE264 Fall 2013 Exam 3, November 20, 2013

Data Representation and Storage

Quick review pointer basics (KR ch )

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Low-Level C Programming. Memory map Pointers Arrays Structures

TI2725-C, C programming lab, course

Chapter IV Introduction to C for Java programmers

Dynamic memory allocation

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

1d: tests knowing about bitwise fields and union/struct differences.

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CSE 333 Midterm Exam Sample Solution 7/28/14

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Arrays and Pointers in C. Alan L. Cox

Transcription:

to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array

Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed language To gain some experience with a compiled language To get practice in using pointers To get practice with dynamically-allocated memory Play around with linked lists Array

to Structs Array

duction to C to Structs Array C is the language of kernels Other languages, such as Java, Python, Bash, C#, C itself, and many others, are written in C Applications from super computers to embedded microcontrollers C is an imperative, procedural language (like Python) C is compiled to native executable (unlike Python) C is statically-typed (also unlike Python), and statically-bound (like Python) C supports recursion (like Python)

to Structs Array

Hello to Structs Array #include <stdio.h> int main( void ) { int i = 13 ; double d = 1.618034 ; char name[20] = "The Lorax" ; } printf( "I am %s, age %d, at %f\n", name, i, d ) ; return( 0 ) ; $ gcc hello.c -o hello $./hello I am The Lorax, age 13, at 1.618034 All code is in functions main is the entry point for every C program Statements (and type definitions) are terminated with a semicolon (;)

to Structs Array

Basic to Structs Array Basic types are integer and float Characters are simply small (1-byte) integers Delimited with single quotes C also understands strings Delimited with double quotes Stored as arrays of characters No Boolean type Value of 0 (zero) is false Everything else is true

C Built-in Integer char short int long Float float double long double to Structs Array The integer types have signed and unsigned flavors By default, types are signed

Declaring Variables to Structs type identifier [ = value ] ; held by variables known at compile time This allows us to check operations and assignments at compile time Globals are declared outside of functions Locals are declared inside functions, at the top Array

to Structs Array

C Structs to Structs Array struct foo { int i; char c; float k; } ; User-defined type Type name is struct foo Collection of fields Field can be any defined type Fields are accessed with a dot (.)

struct Example #include <string.h> #include <stdio.h> to Structs Array struct person { int age ; char[10] id ; /* string */ } ; int main( void ) { struct person p ; p.age = 28 ; strcpy( p.id, "abc123" ) ; } printf( "%s has %d years.\n", p.id, p.age ) ; return( 0 ) ; $ gcc structs.c -o structs $./structs abc123 has 28 years. Type definitions (e.g., structs) end w/a semicolon do not Do not assign (=) strings Use strcpy

to Structs Array

to Structs Can not be defined inside another function (must be global) Return type part of function declaration Can be void are passed by value Must be declared before use A prototype serves Array

Function Example to Structs Array #include <string.h> #include <stdio.h> /* prototype */ int foo( int k ) ; int main( void ) { int j = 12, k = 13, r ; } r = foo( j ) ; printf( "foo returned %d\n", r ) ; if( j!= 12 ) printf( "j changed\n" ) ; if( k!= 13 ) printf( "k changed\n" ) ; return( 0 ) ; int foo( int k ) { /* j is local, and k behaves so */ int j = 23 ; int rv = k * j ; } k = 928357 ; return( rv ) ; https: //goo.gl/v4rzgr

Function Separate Files to Structs Array main.c #include <string.h> #include <stdio.h> #include "myfoo.h" int main( void ) { int j = 12, k = 13, r ; } r = foo( j ) ; printf( "foo returned %d\n", r ) ; return( 0 ) ; $ gcc main.c foo.c -o foo foo.h #ifndef MY_FOO_H_ #define MY_FOO_H_ int foo( int k ) ; #endif /* MY_FOO_H_ */ foo.c #include "foo.h" int foo( int k ) { int j = 23 ; int rv = k * j ; k = 928357 ; } return( rv ) ;

in C to Structs Array C uses lexical scoping (or static binding) A symbol (variable name) is bound to a location at compile time A variable is either Local to the function it appears in (if it s declared there, or is a formal parameter), or Global (declared outside of all functions, possibly in another file) Pretty straightforward in C, since we can t define functions inside of other functions

to Structs Array

to Structs Array Allow us to access memory locations, variables, indirectly Pointer is just an integer which stores an address (memory location) Pointer and address are synonymous. Can point to local, global, or heap memory Or another pointer Even to a function (not this set of slides) NOTE: Run the examples in the following slides in http://www.pythontutor.com/c.html. It allows you to step through the code, provides visualisations

Declaration to Structs are typed Declared with a * To declare a pointer to type T: T *p = NULL ; NULL is a safe, default pointer It is 0, defined in stdlib.h Always point to NULL (0) when pointer not in use Array

& Operator Returns address of (pointer to) variable Prefix operator int i = 42 ; int *p = NULL ; /* initialise p */ p = &i ; to Structs Array

* Dereference Operator Used to get at the contents of memory pointed to Can be used to assign into memory int i = 42 ; int *p = NULL ; /* initialise p */ p = &i ; /* point p at i */ *p = 5 ; /* use p to modify i */ to Structs Array

E.g. s to Structs Array int main( void ) { int i = 5, j = 12 ; } int *p = NULL ; p = &i ; /* modifying pointer */ *p = 23 ; /* modifying thing pointed to */ printf( "*p is %d\n", *p ) ; printf( "i is now %d\n", i ) ; return( 0 ) ; 0 https://goo.gl/xzukwa

Passing to We can pass pointers to values into functions can then modify caller s data Indirectly, through the local pointer (formal parameter) Remember, addresses of variables must be passed in to Structs Array

Example Pass by void add3( int *p ) { *p += 3 ; } int main( void ) { int i = 28 ; add3( &i ) ; /* pass in ptr to i */ printf( "i is now %d\n", i ) ; to Structs Array } return( 0 ) ; 0 https://goo.gl/sswyh4

Counterexample Returning Pointer to Local to Structs Array int* foo( int i ) { i += 3 ; return( &i ) ; } int main( void ) { int j = 28 ; int *p = NULL ; } p = foo( j ) ; /* NO! */ return( 0 ) ; Can you guess the problem?

to Structs to Structs Array Given a person: struct person { int age ; char id[10] ; /* string */ } baker ; And a pointer, p, to a person: struct person *p = &Hannah ; If p points to a person, then *p is that person To access the age field: (*p).age = 28 C gives us gentler syntax: p->age = 28 ; strcpy( p->id, "s983742" ) ;

Example to Structs to Structs Array #include <stdio.h> #include <string.h> struct person { int age ; char id[10] ; } ; int main( int argc, char *argv[] ) { struct person baker ; struct person *p = &baker ; } p->age = 32 ; strcpy( p->id, "s9283" ) ; return( 0 ) ; 0 https://goo.gl/ogppts

to Structs Array

Declaration of Static to Structs Array T name[size] are typed Size of statically-allocated arrays (global or local) must be known at compile time Indexing starts at 0 (zero) Name of array is really a pointer to the first element So, generally, a[i] is equivalent to *(a+i) Can be initialised at declaration int a[] = { 121, 17, 2, 88, -273 } ;

Example to Structs int main( void ) { int i ; int a[5] ; int *p = NULL ; for( i=0; i<5; ++i ) a[i] = (i+2)*3 ; for( i=0; i<5; ++i ) printf( "*(a+%d) = %d\n", i, *(a+i) ) ; for( p=a, i=0; i<5; ++i, ++p ) *p += 2 ; /* add 2 to each element */ Array } return( 0 ) ; 0 https://goo.gl/5guxkm

Passing to to Structs Array are pass by reference Remember, name of an array is a pointer Array can be modified by the function Size (number of elements) must also be passed to function Unless a suitable sentinel value exists (e.g., \0 ) The following 2 prototypes are (almost) equivalent 1 void foo( float a[], int n ) ; void foo( float* a, int n ) ; 1 In the first case a is a const pointer

Example Array to Structs Array #include <stdio.h> void print( float a[], int n, FILE *ofile ) { for( int i=0; i<n; ++i ) fprintf( ofile, "%f ", a[i] ) ; /* access by index */ } void addphi( float *a, int n ) { for( int i=0; i<n; ++i, ++a ) *a += 1.61803398 ; /* access through ptr */ } First parameter same in both functions 1 FILE* is how we pass pointers to files 1 Not quite, in C++

Example Array (p. 2) int main( int argc, char **argv ) { float fs[] = { 3.1415926535, 2.718281828, 22.4, 1.414213562 } ; addphi( fs, 4 ) ; to Structs Array } printf( "The array now: " ) ; print( fs, 4, stdout ) ; printf( "\n" ) ; return( 0 ) ; Name of array is pointer to first element 1 https://goo.gl/g3aoed

to Structs Array

duction to Structs Array Also known as the free store Programmer can request chunks of memory (using malloc 1 ) Programmer is responsible for returning chunk to the heap (using free) Chunks are unnamed Must be accessed through a pointer Chunk can be any size One int Array of floats Person struct Good for resizeable arrays and strings, linked lists,... 1 or calloc, realloc, or strdup

Notes to Structs Array Memory that is malloc d should be free d Cast return from malloc Do not assume memory was available malloc returns NULL when it fails Check! Use sizeof operator to get size of types free( p ) does nothing to p itself Simply marks memory pointed to by p as available Don t free same chunk twice Always set pointers to NULL when not using them

Example malloc for individual items int main( void ) { int *ip = (int*) malloc( sizeof( int )) ; int *jp = (int*) malloc( sizeof( int )) ; *ip = 52 ; *jp = 88 ; printf( "*ip is %d, and *jp is %d\n", *ip, *jp ) ; free( ip ) ; ip = NULL ; jp = NULL ; /* MEMORY LEAK! */ to Structs Array } return( 0 ) ; 1 https://goo.gl/52sevc

Better Returning Pointer to int* foo( int i ) { int *rv = (int*) malloc( sizeof( int )) ; *rv = i+3 ; return( rv ) ; } int main( void ) { int *p = foo( 28 ) ; /* Okay! */ to Structs Array } if( p!= NULL ) /* Do something w/p... */ free( p ) ; p = NULL ; /* No dangling ptrs */ return( 0 ) ; 1 https://goo.gl/bfyx2h

duction Dynamically-allocated to Structs Array T *a = (T*) malloc( CAP * sizeof( T )) ; We can maintain our own arrays, for greater flexibility Used just the same as statically-allocated Still must remember size, the number of things of value it stores Must also remember capacity, how many things it can currently store A single value is simply an array of length one

Example Heap to Structs int main( void ) { int cap = 5 ; int *p = (int*) malloc( cap * sizeof( int )) ; int *t = NULL, i ; if( p==null ) return( 1 ) ; for( i=0; i<cap; ++i ) p[i] = (i+2)*3 ; for( i=0; i<cap; ++i ) printf( "*(p+%d) = %d\n", i, *(p+i) ) ; for( t=p, i=0; i<cap; ++i, ++t ) *t += 2 ; /* add 2 to each element */ Array } free( p ) ; return( 0 ) ; 1 https://goo.gl/ztjupy

Resizing to Structs Array If array is full: Get a larger array from the heap Copy contents of full array over Free old array Update array pointer Update capacity See realloc Read through the Data Structures intro notes from CS265

Example Resizing to Structs Array int main( void ) { int *t = NULL, i, cap = 5 ; int *p = (int*) malloc( cap * sizeof( int )) ; if( p==null ) return( 1 ) ; for( i=0; i<5; ++i ) p[i] = (i+2)*3 ; /* need more space */ t = (int*) malloc( cap * 2 * sizeof( int )) ; if( t==null ) return( 1 ) ; for( i=0; i<cap; ++i ) t[i] = p[i] ; free( p ) ; p = t ; t = NULL ; cap *= 2 ; } return( 0 ) ; 1 https://goo.gl/kxnyag

Linked to Structs Array to heap memory are well-suited to working with linked lists A List: Linear, homogeneous container Not indexed (no constant-time access) Middle can be modified in constant time Made of nodes Container for single element Has references (pointers) to other nodes

Linked snode to Structs Array typedef struct snode snode ; struct snode { snode *next ; other data fields... } ; Such a node makes up a singly-linked list Can only be traversed in one direction We could make a doubly-linked list The typedef allows us to refer to the type simply as snode

Example Linked List to Structs Array #include <stdio.h> #include <stdlib.h> typedef struct snode snode ; struct snode { snode *next ; int data ; } ; void print( snode *l, FILE *fp ) { fprintf( fp, "<" ) ; while( l!= NULL ) { fprintf( fp, " %d", l->data ) ; l = l->next ; } fprintf( fp, " >" ) ; }

Example Linked List (p. 2) int main( int argc, char *argv[] ) { snode *l = NULL, *t = NULL ; l = (snode*) malloc( sizeof( snode )) ; l->data = 13 ; l->next = NULL ; t = (snode*) malloc( sizeof( snode )) ; t->data = 12 ; t->next = l ; l = t ; to Structs Array } t = (snode*) malloc( sizeof( snode )) ; t->data = 5 ; t->next = l ; l = t ; print( l, stdout ) ; printf( "\n" ) ; return( 0 ) ; /* memory leak */ 1 https://goo.gl/shqhrk