F28HS2 Hardware-Software Interface Lecture 4: Programming in C - 4.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
F28PL1 Programming Languages Lecture 8 Programming in C - 3.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
UBC104 Embedded Systems Variables, Structures & Pointers.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
CSc 352 C : Arrays, Structs, Pointers
Memory Layout C and Data Structures Baojian Hua
Pointers Applications
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
F28PL1 Programming Languages Lecture 9 Programming in C - 4.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
F28PL1 Programming Languages Lecture 10: Programming in C 5.
F28PL1 Programming Languages Lecture 7 Programming in C - 2.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
F28PL1 Programming Languages Lecture 5: Programming in C 5.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
F28HS2 Hardware-Software Interface Lecture 3 Programming in C - 3.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Advanced Pointers and Structures Pointers in structures Memory allocation Linked Lists –Stacks and queues Trees –Binary tree example.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Stack and Heap Memory Stack resident variables include:
Chapter 8 Arrays, Strings and Pointers
Dynamic Allocation Review Structure and list processing
5.13 Recursion Recursive functions Functions that call themselves
malloc(): Dynamic Memory Management
Programming Languages and Paradigms
Programming with Pointers
Review & Lab assignments
Outline Defining and using Pointers Operations on pointers
Chapter 9: Pointers and String
Presentation transcript:

F28HS2 Hardware-Software Interface Lecture 4: Programming in C - 4

Arrays and typed pointers type name [int]; allocates space for int elements of type type * name ; allocates space for pointer to type does not allocate space for elements

Array assignment cannot assign: – array to array – type pointer to array must copy element by element cannot assign array to type pointer must: – allocate space to type pointer – copy element by element can assign type pointer to type pointer

Dynamic Space Allocation char * malloc(int) allocates int bytes on heap returns address of 1 st byt like new in Java in stdlib

Coercions ( type ) expression 1.evaluate expression to value 2.now treat value as if type does not physically transform expression as if overlaid template for type on value also called cast

Dynamic Space Allocation use cast to coerce pointer to sequence of bytes to pointer to required type typical malloc use: ( type *)malloc( number *sizeof( type )) allocate space for number of size for type – but returns pointer to byte so now treat address as if pointer to type

Dynamic Space Allocation e.g. malloc(16)  pointer to 16 bytes (int *)malloc(4*sizeof(int))  (int *)malloc(16)  pointer to 4 int s

Dynamic Space Allocation free(void *) returns space to heap space must have been allocated by malloc NB does not recursively return space – must climb linked data structure freeing space

String Dynamic Space Allocation char * name; allocates space for pointer to characters can assign string constant to name – changes pointer must allocate space for characters name = malloc( size );

Example: string copy char * scopy(char s[]) make a new copy of string s 1.find length of s 2.allocate new byte sequence of this length 3.copy s element by element to new byte sequence 4.return address of new byte sequence

Example: string copy #include char * scopy(char s[]) { char * c; int l,i; l = slength(s); c = (char *)malloc(l); i = 0; while(i<l) { c[i] = s[i]; i = i+1; } return c; } e.g. scopy(“hello”); hello\0 s

Example: string copy #include char * scopy(char s[]) { char * c; int l,i; l = slength(s); c = (char *)malloc(l); i = 0; while(i<l) { c[i] = s[i]; i = i+1; } return c; } hello\0 e.g. scopy(“hello”); s c

Example: string copy #include char * scopy(char s[]) { char * c; int l,i; l = slength(s); c = (char *)malloc(l); i = 0; while(i<l) { c[i] = s[i]; i = i+1; } return c; } hello\0 e.g. scopy(“hello”); s c ello\0h

Example: string copy main(int argc,char ** argv) { char * s; s = scopy("water: the drink that is wet!"); printf("%s\n",s); } $ scopy water: the drink that is wet!

Structure assignment can assign one structure to another – fields copied automatically – two identical versions in different memory sequences – copying can assign pointer to structure to pointer to structure – both pointers now refer to same memory sequence aliases – sharing

Example: sort character counts quicksort(struct freq a[],int l,int r) { int p; if(l<r) { p = partition(a,l,r); quicksort(a,l,p-1); quicksort(a,p,r); }

Example: sort character counts int partition(struct freq a[],int l,int r) { int i,j,p; i = l; j = r; p = a[(i+j)/2].count; while(i<=j) { while(a[i].count<p)i = i+1; while(a[j].count>p)j = j-1; if(i<=j) { swap(a,i,j); i = i+1;j = j-1; } } return i; }

Example: sort character counts swap(struct freq a[],int i,int j) { struct freq t; t = a[i]; a[i] = a[j]; a[j] = t; } copy whole struct from – a[i] to t – a[j] to a[i] – t to a[j]

Stack of struct v stack of pointers stack of entries – allocate more struct space than actually use – copy whole struct s in swap instead: – allocate stack space for pointers to struct – copy pointers in swap must: – malloc space for struct s – access fields from pointer

Structure space allocation for struct name: (struct name *) malloc(sizeof(struct name ) allocate for size of type cast to pointer to type

Structure space allocation e.g. struct person {char * name;double height;int age;}; struct person * pat; pat = (struct person *) malloc(sizeof(struct person)); pat nameheightage

Structure pointer field access exp -> name i  (* exp ). name i i.e. contents of offset of preceding elements from byte sequence that identifier points at

Structure pointer field access e.g. pat->name = “Pat”; pat->height = 1.68; pat->age = 23; pat nameheightage aPt\0

Example: character frequency 2 #include #define MAX 255 struct freq {int ch;int count;}; struct freq * f[MAX]; int fp; countchf fp

Example: character frequency 2 incFreq(int ch) { int i; i=0; while(i<fp) if(f[i]->ch==ch) { f[i]->count = f[i]->count+1; return; } else i = i+1;

Example: character frequency 2 if(fp==MAX) { printf("more than %d different characters\n",MAX); exit(0); } f[fp] = (struct freq *) malloc(sizeof(struct freq)); f[fp]->ch = ch; f[fp]->count = 1; fp = fp+1; }

Example: character frequency 2 showFreq() { int i; i = 0; while(i<fp) { printf("%c : %d\n, ", f[i]->ch,f[i]->count); i = i+1; } printf("\n"); }

Example: character frequency 2 swap(struct freq * a[],int i,int j) { struct freq *t; t = a[i]; a[i] = a[j]; a[j] = t; }

Example: character frequency 2 int partition(struct freq * a[],int l,int r) { int i,j,p; i = l; j = r; p = a[(i+j)/2]->count; while(i<=j) { while(a[i]->count<p) i = i+1; while(a[j]->count>p) j = j-1;

Example: character frequency 2 if(i<=j) { swap(a,i,j); i = i+1; j = j-1; } return i; }

Recursive structures cannot directly define recursive struct s e.g. linked list struct list{ int value; struct list next;}; struct list l; l is allocated space for a struct list – space for an int – space for a struct list space for an int space for a struct list …

Recursive structures define recursive structures with pointers end chains with NULL e.g. linked list struct list{int value; struct list * next;}; nextvalue NULL1242

Recursive structures e.g. binary tree struct node {int value; struct node * left; struct node * right;}; leftvalue 17 right 142

Example: character frequency 3 allocating stack space for more pointers then needed replace stack with linked list struct freq {int ch;int count; struct freq * next;}; struct freq * fp; ‘a’42 fp NULL‘-’123

Example: character frequency 3 incFreq(int ch) { struct freq * f; if(fp==NULL) { fp = (struct freq *) malloc(sizeof(struct freq)); fp ->ch = ch; fp->count = 1; fp->next = NULL; return; } allocate first entry

Example: character frequency 3 f = fp; while(1) if(f->ch==ch) { f->count = f->count+1; return; } else if(f->next!=NULL) f = f->next; if character found then increment count if not at end then try next entry

Example: character frequency 3 else { f->next = (struct freq *) malloc(sizeof(struct freq)); f->next->ch = ch; f->next->count = 1; return; } at end so add new entry

Example: character frequency 3 showFreq() { struct freq * f; f = fp; while(f!=NULL) { printf("%c : %d\n, ",f->ch,f->count); f = f->next; } printf("\n"); }

Auto-increment exp ++  evaluate exp to address get value from address increment value – depending on type of exp update address with incremented value if in exp then return un- incremented value ++ exp  evaluate exp to address get value from address increment value – depending on type of exp update address with incremented value if in exp then return incremented value

Auto-increment exp --  evaluate exp to address get value from address decrement value – depending on type of exp update address with decremented value if in exp then return un- decremented value -- exp  evaluate exp to address get value from address decrement value – depending on type of exp update address with decremented value if in exp then return decremented value

Auto-increment for int / short / long / char variable – ++ / -- adds/subtracts one: name ++  postInc(& name ); int postInc(int * name ) { int t = * name ; * name = * name +1; return t; } ++ name  preInc(& name ); int preInc(int * name ) { * name = * name +1; return * name ; }

Auto-increment exp 1 + exp 2 == value of exp 1 added to value of exp 2 e.g. int i; int j; i = 0; j = 0; printf("++i + ++j == %d\n",++i + ++j); i = 0; j = 0; printf("++i + j++ == %d\n",++i + j++); i = 0; j = 0; printf("i j == %d\n",i j); i = 0; j = 0; printf("i++ + j++ == %d\n",i++ + j++); ==> ++i + ++j == 2 - before & before ++i + j++ == 1 - before & after i j == 1 - after & before i++ + j++ == 0 - after & after

Pointer arithmetic type * name ; arithmetic on name is in units of the size for type i.e. increases/decreases an address name ++  name = name + size for type i.e. pointer has moved on one element name --  name = name – size for type i.e. pointer has moved back one element

Pointer arithmetic int a[4]; int * b; printf(“a == %x\n”,a); for(i=0;i<3;i++) a[i] = i; b = a; printf(“b == %x; b* == %d\n",b,*b); b++; printf(“b == %x; b* == %d\n",b,*b); ==> a == 7eb i.e. address of the first byte for a b == 7eb68518; b* == 0 - i.e. b is same address as a b == 7eb6851c; b* == 1 - b incremented by 4 i.e. 4 bytes b == 7eb68520; b* == 2 - b incremented by 4 i.e. 4 bytes b == 7eb68524; b* == 3 - b incremented by 4 i.e. 4 bytes

Pointer arithmetic name + exp  name = name + exp * size for type i.e. pointer has moved on exp elements name - exp  name = name – exp * size for type i.e. pointer has moved back exp elements hard to test pointers for address boundary errors...