March, 2006 Saeid Nooshabadi

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

Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
CS61C L04 Introduction to C (pt 2) (1) Garcia, Fall 2011 © UCB Reference slides You ARE responsible for the material on these slides (they’re just taken.
CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L4 C Memory Management (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4.
CS61C L3 C Pointers (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 – C Strings,
CS61C L04 Introduction to C (pt 2) (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L06 C Memory Management (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C :
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS 61C L03 C Pointers (1)Garcia / Patterson Fall 2002 © UCB CS61C - Machine Structures Lecture 3 C pointers  Dan Garcia (
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
Pointers Applications
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS 61C L04 C Structures, Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L4 C Pointers (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 –C Strings,
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Pointers OVERVIEW.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topic 4: C Data Structures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS 61C: Great Ideas in Computer Architecture C Pointers Instructors: Vladimir Stojanovic & Nicholas Weaver 1.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CS61C L04 Introduction to C (pt 2) (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Object Lifetime and Pointers
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
5.13 Recursion Recursive functions Functions that call themselves
C Primer.
CSCI206 - Computer Organization & Programming
A bit of C programming Lecture 3 Uli Raich.
C Programming Tutorial – Part I
CSE 303 Concepts and Tools for Software Development
March, 2006 Saeid Nooshabadi
Pointers and Memory Overview
Arrays in C.
CSC 253 Lecture 8.
Instructor Paul Pearce
CSC 253 Lecture 8.
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 – Introduction to C (pt 3) C Memory Management Lecturer SOE Dan Garcia.
Memory Allocation CS 217.
CS61C - Machine Structures Lecture 4 C Structures Memory Management
Programming in C Pointer Basics.
Hello to Scott Sloan from Superior, WI!
CSE 214 – Computer Science II Pointers & Memory Management
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
There is one handout today at the front and back of the room!
Programming in C Pointer Basics.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Chapter 16 Pointers and Arrays
C++ Pointers and Strings
Exercise Arrays.
C Programming - Lecture 5
C++ Pointers and Strings
15213 C Primer 17 September 2002.
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

March, 2006 Saeid Nooshabadi saeid@unsw.edu.au ELEC2041 Microprocessors and Interfacing Lecture 4: C-Language Review - III http://webct.edtec.unsw.edu.au/ March, 2006 Saeid Nooshabadi saeid@unsw.edu.au

Overview Pointers to Pointers Linked List Shift Operators

Review: C memory allocation Address ¥ Stack Space for saved procedure information sp stack pointer Heap Explicitly created space, e.g., malloc(); C pointers global pointer gp Static Variables declared once per program Code Program

Pointer Arithmetic Summary x = *(p+1)  x = *(p+1) ; x = *p+1  x = (*p) + 1 ; x = (*p)++  x = *p ; *p = *p + 1; x = *p++ ? (*p++) ? *(p)++ ? *(p++)  x = *p ; p = p + 1; x = *++p  p = p + 1 ; x = *p ; Lesson? Avoid the confusing syntaxes! =

C String Standard Functions int strlen(char *string); compute the length of string int strcmp(char *str1, char *str2); return 0 if str1 and str2 are identical (how is this different from str1 == str2?) char *strcpy(char *dst, char *src); copy the contents of string src to the memory at dst. The caller must ensure that dst has enough memory to hold the data to be copied.

Pointers to pointers (#1/5) Sometimes you want to have a procedure increment a variable? What gets printed? void AddOne(int x) { x = x + 1; } int y = 5; AddOne( y); printf(“y = %d\n”, y); y = 5

Pointers to pointers (#2/5) Solved by passing in a pointer to our subroutine. Now what gets printed? void AddOne(int *p) { *p = *p + 1; } int y = 5; AddOne(&y); printf(“y = %d\n”, y); y = 6

Pointers to pointers (#3/5) But what if what you want changed is a pointer? What gets printed? void IncrementPtr(int *p) { p = p + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr( q); printf(“*q = %d\n”, *q); *q = 50 q A 50 60 70

Pointers to pointers (#4/5) Solution! Pass a pointer to a pointer, called a handle, declared as **h Now what gets printed? void IncrementPtr(int **h) { *h = *h + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(&q); printf(“*q = %d\n”, *q); *q = 60 q A 50 60 70

Pointers to pointers (#5/5) 80 96 ptr2 ptr1 65 84 88 32 92 90 96 45 55 int **ptr1 int *ptr2 11 88 100 - 103 80

Recall: C Syntax; Arguments to main To get the main function to accept arguments, use this: int main (int argc, char *argv[]) What does this mean? argc will contain the number of strings on the command line (the executable counts as one, plus one for each argument). argv is a pointer to an array containing the rest of the arguments as strings

C structures : Overview A struct is a data structure composed for simpler data types. Like a class in Java/C++ but without methods or inheritance. struct point { int x; int y; }; void PrintPoint(point p) { printf(“(%d,%d)”, p.x, p.y); }

C structures: Pointers to them The C arrow operator (->) dereferences and extracts a structure field with a single operator. The following are equivalent: struct point *p; printf(“x is %d\n”, (*p).x); printf(“x is %d\n”, p->x);

struct p { char x; int y; }; How big are structs? Recall C operator sizeof() which gives size in bytes (of type or variable) How big is sizeof(p)? struct p { char x; int y; }; 5 bytes? 8 bytes? Compiler may word align integer y

Which are guaranteed to print out 5? Peer Instruction Which are guaranteed to print out 5? I: main() { int *a_ptr; *a_ptr = 5; printf(“%d”, *a_ptr); } II: main() { int *p, a = 5; p = &a; ... /* code; a & p NEVER on LHS of = */ printf(“%d”, a); } III: main() { int *ptr; ptr = (int *) malloc(sizeof(int)); *ptr = 5; printf(“%d”, *ptr); } Answer: 2: III only I fails for TWO reasons. You can’t use “-” in names of variables and you didn’t allocate int storage! II fails because a subroutine could be called with a pointer to a (e.g., p) which changes a, or someone else could point at a or p and change it. I II III 1: - - - 2: - - YES 3: - YES - 4: - YES YES 5: YES - - 6: YES - YES 7: YES YES - 8: YES YES YES

Linked List Example (#1/7) Let’s look at an example of using structures, pointers, malloc(), and free() to implement a linked list of strings. struct Node { char *value; struct Node *next; }; typedef Node *List; /* Create a new (empty) list */ List ListNew(void) { return NULL; }

Linked List Example (#2/7) /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } node: list: string: “abc” … NULL ?

Linked List Example (#3/7) /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } list: node: … … ? NULL string: ? “abc”

Linked List Example (#4/7) /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } list: node: … … NULL string: ? “abc” “????”

Linked List Example (#5/7) /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } list: node: … … NULL string: ? “abc” “abc”

Linked List Example (#6/7) /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } list: node: … … NULL string: “abc” “abc”

Linked List Example (#7/7) /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } node: … … NULL “abc”

Peer Instruction int main(void){ int A[] = {5,10}; int *p = A; printf(“%u %d %d %d\n”,p,*p,A[0],A[1]); p = p + 1; printf(“%u %d %d %d\n”,p,*p,A[0],A[1]); *p = *p + 1; printf(“%u %d %d %d\n”,p,*p,A[0],A[1]); } If the first printf outputs 100 5 5 10, what will the other two printf output? 1: 101 10 5 10 then 101 11 5 11 2: 104 10 5 10 then 104 11 5 11 3: 101 <other> 5 10 then 101 <3-others> 4: 104 <other> 5 10 then 104 <3-others> 5: One of the two printfs causes an ERROR 6: I surrender! 5 10 A[0] A[1] p 2: 104 10 5 10 then 104 11 5 11 …because ints in this system are 4-bytes long and the actual address increments by 4 even though it appears to only incrememt 1.

References: Nick Parlante: Stanford CS Education Library (http://cslibrary.stanford.edu/); A Collection of very useful material including: Essential C: (http://cslibrary.stanford.edu/101/) A relatively quick, 45 page discussion of most of the practical aspects of programming in C. Binky Pointer Video: (http://cslibrary.stanford.edu//104) Silly but memorable 3 minute animated video demonstrating the basic structure, techniques, and pitfalls of using pointers. Pointer Basics: (http://cslibrary.stanford.edu/106) The companion text for the Binky video. Pointers and Memory: (http://cslibrary.stanford.edu/102) A 31 page explanation of everything you ever wanted to know about pointers and memory. Linked List Basics: (http://cslibrary.stanford.edu/103) A 26 page introduction to the techniques and code for building linked lists in C.

Important Logical Operators Logical AND (&&) and bitwise AND (&) operators: char a=4, b=8, c; c = a && b; /* After this statement c =1*/ c = a & b; /* After this statement c =0*/ Dec Binary a 4 0000 0100 > 0 b 8 0000 1000 > 0 a && b True a &b 0 0000 0000 Similarly logical OR (||) and bitwise OR (I) operators:

Important Shift Operators Logical shift left (<<) and shift right (>>) operators: char a=4, b=8, c; c = a << 2; /* After this statement c =16*/ c = b >> 3; /* After this statement c =1*/ Dec Binary a 4 0000 0100 b 8 0000 1000 a << 2 16 0001 0000 b >>3 1 0000 0001

What is this stuff good for? StressEraser: Biofeedback device promises to reduce stress aid for deep breathing exercises, (prescribed to alleviate stress.) The device tells when to inhale and when to stop. It does this by divining the state of your nervous system by some clever analysis of your heart rate device is a pulse oximeter integrated with a display and a microprocessor. US$399 Pulse oximeters identify heartbeats by the variation in the amount of light absorbed through the skin of your finger as fresh blood pulses through it. It monitors heart rate to identify the activity level of the vagus nerve, one of 12 nerves that emanate directly from your brain rather than through your spinal cord. In all fairness, I'm a tough nut for this kind of product to crack. First, I find it difficult to stick to any kind of routine that involves self-indulgence. I can't get to the gym regularly or reliably remember to pack a lunch. So managing to use the device three times a day for a whole week, as recommended, was out of my reach. Luckily, guilt is an excellent motivator. I happened to run into Helicor's Forbes at least three times while I was reviewing the device. Each of those encounters led to a renewed dedication to spend time staring at my heart-rate waveform. IEEE Spectrum March 2006

If you remember nothing else, remember this: World’s Last C Bug If you remember nothing else, remember this: while (1) { status = GetRadarInfo(); if (status = 1) { LaunchMissiles(); } } = is used instead of ==

World’s Last C Bug Improved! launches = 0; while (1){ status = GetRadarInfo(); if (status = 1){ LaunchMissiles(); launches++; } if (launches > 1){ apologize(); Steve Litt: www.troubleshooters.com

Example #1: How many bugs in this code? Choices: 1 or none, 2, 3, 4, #include <stdio.h> int main ( ) { int numAs; /* counts A's in the input */ char c; while (c = getchar ( ) != EOF) { /* getchar returns EOF if no more chars to read. */ if (c = 'A') { numAs++; } printf ("%d A's in the input\n", numAs); return 0; Choices: 1 or none, 2, 3, 4, 5 or more

Example #1 (Solution): 3 Bugs How many bugs in this code? #include <stdio.h> int main ( ) { int numAs; /* counts A's in the input */ char c; numAs = 0; while ((c = getchar ( )) != EOF) { /* getchar returns EOF if no more chars to read. */ if (c == 'A') { numAs++; } printf ("%d A's in the input\n", numAs); return 0; 3 Bugs

Bug Symptoms A value that’s wildly out of range suggests an uninitialized variable or function return value. A loop that’s executed 0 times or the maximum number of times when it shouldn’t be suggests misuse of = in a test, or misparenthesization.

What output is produced by this code? Example #2 What output is produced by this code? void addOne (int x) { x = x + 1; } int main ( ) { int y = 3; addOne (y); printf (%d, y); return 0; Choices: 3, 4, unknown,

What output is produced by this code? Example #2 (Solution) What output is produced by this code? void addOne (int x) { x = x + 1; } int main ( ) { int y = 3; addOne (y); printf (%d, y); return 0; 3 is Produced Passed by value: Make a copy of the original argument. The original won’t change

Example #3 What choice for the blank ensures that 7 = 7 is printed? #include <stdio.h> int *f (int varNotToSet, int varToSet) { int n = 7; varToSet = 7; return _____ ; /* &n, &varNotToSet, or &varToSet could go here */ } int main ( ) { int *ptr; int k1 = 7, k2 = 0; ptr = f(k1, k2); printf ("7 = "); printf ("%d\n", *ptr); return 0; Choices: 1) &n, 2) &varNotToSet, 3) &varToSet 1 2, 3, none

Variables and function parameters are allocated on the system stack Example #3 (Solution) Answer: none Variables and function parameters are allocated on the system stack When a function exits, its allocated space gets reallocated to another function.

Example #4 What choice for the blank ensures that values[0]=x Answer: int main ( ){ int values[20]; int x; ... assign ( _____ , x); } void assign ( int *y , int x) { *y = x; Answer: 1. values 2. &values[0]

Example #5 How to copy one array to another array int main ( ){ char sFrom[6], sTo[6]; copy (sTo[6], sFrom[6]); } void copy (char sTo[], char sFrom[]) { ---- sTo[0] sTo[1] sTo[2] sTo[3] sTo[4] sTo[5] sFrom[0] sFrom[1] sFrom[2] sFrom[3] sFrom[4] sFrom[5] ‘h’ ‘e’ ‘l’ ‘o’ ‘h’ ‘e’ ‘l’ ‘o’

Example #5 (Solution #1/4) void copy (char sTo[], char sFrom[]) { sTo = Sfrom; } sTo[0] sTo[1] sTo[2] sTo[3] sTo[4] sTo[5] sFrom[0] sFrom[1] sFrom[2] sFrom[3] sFrom[4] sFrom[5] ‘h’ ‘e’ ‘l’ ‘o’ sTo sFrom Similarly you don’t compare two string using ==

Example #5 (Solution #2/4) Straight Forward Array Version void copy (char sTo[], char sFrom[]) { int k = 0; while (sFrom[k] != '\0') { sTo[k] = sFrom[k]; k++; } sTo[k] = sFrom[k]; /* copy terminating 0 */ sTo[0] sTo[1] sTo[2] sTo[3] sTo[4] sTo[5] sFrom[0] sFrom[1] sFrom[2] sFrom[3] sFrom[4] sFrom[5] ‘h’ ‘e’ ‘l’ ‘o’ ‘h’ ‘e’ ‘l’ ‘l’ ‘o’

Example #5 (Solution #3/4) Array Version (Taking advantage of value returned by assignment operator void copy (char sTo[], char sFrom[]) { int k = 0; while ((sTo[k] = sFrom[k]) != '\0'){ k++; } sTo[0] sTo[1] sTo[2] sTo[3] sTo[4] sTo[5] sFrom[0] sFrom[1] sFrom[2] sFrom[3] sFrom[4] sFrom[5] ‘h’ ‘e’ ‘l’ ‘o’ ‘h’ ‘e’ ‘l’ ‘o’

Example #5 (Solution #3/4) Pointer Version void copy (char sTo[], char sFrom[]) { while ((*sTo = *sFrom) != '\0') { /* pointer arithmetic (K&R 5.4) */ sFrom++; sTo++; } sTo[0] sTo[1] sTo[2] sTo[3] sTo[4] sTo[5] sFrom[0] sFrom[1] sFrom[2] sFrom[3] sFrom[4] sFrom[5] ‘h’ ‘e’ ‘l’ ‘o’ ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ sTo sFrom

Things to Remember (#1/2) Use handles to change pointers Create abstractions with structures A struct is a data structure composed for simpler data types. We can change the value of a pointer in a function by pointer to pointer feature.

Things to Remember (#2/2) Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection Array bounds not checked Variables not automatically initialized (Beware) The cost of efficiency is more overhead for the programmer. “C gives you a lot of extra rope but be careful not to hang yourself with it!”