2008SpCS61C-L05-ddg-c3-6up

# 2008SpCS61C-L05-ddg-c3-6up - CS61C L05 Introduction to C...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS61C L05 Introduction to C (pt 3) (1) Garcia, Spring 2008 UCB Lecturer SOE Dan Garcia www.cs.berkeley.edu/~ddgarcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 Introduction to C (pt 3) C Memory Management 2008-02-01 Hello to Scott Sloan from Superior, WI! Sci-Fi Realities the predictions of the Sci-Fi analysts feel will become reality and when: Biometric Security voice, iris, retinal scans [2010], Space Tourism [2013], Holodeck [2016], Self-Aware Computers [2019], Domestic Robots [2020]. Get in line! www.pcworld.com/article/id,141682-page,1-c,desktoppcs/article.html CS61C L05 Introduction to C (pt 3) (2) Garcia, Spring 2008 UCB Review Pointers and arrays are virtually same C knows how to increment pointers C is an efcient language, with little protection Array bounds not checked Variables not automatically initialized (Beware) The cost of efciency is more overhead for the programmer. C gives you a lot of extra rope but be careful not to hang yourself with it! CS61C L05 Introduction to C (pt 3) (3) Garcia, Spring 2008 UCB Pointers (1/4) 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 review CS61C L05 Introduction to C (pt 3) (4) Garcia, Spring 2008 UCB Pointers (2/4) Solved by passing in a pointer to our subroutine. Now what gets printed? void AddOne(int * p) { * p = * p + 1; } int y = 5; AddOne( &amp; y); printf(y = %d\n, y); y = 6 review CS61C L05 Introduction to C (pt 3) (5) Garcia, Spring 2008 UCB Pointers (3/4) 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 50 60 70 A q CS61C L05 Introduction to C (pt 3) (6) Garcia, Spring 2008 UCB Pointers (4/4) Solution! Pass a pointer to a pointer , declared as **h Now what gets printed? void IncrementPtr(int **h ) { *h = *h + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr( &amp; q); printf(*q = %d\n, *q); *q = 60 50 60 70 A q q CS61C L05 Introduction to C (pt 3) (7) Garcia, Spring 2008 UCB Dynamic Memory Allocation (1/4) C has operator sizeof() which gives size in bytes (of type or variable) Assume size of objects can be misleading and is bad style, so use sizeof(type) Many years ago an int was 16 bits, and programs were written with this assumption. What is the size of integers now? sizeof knows the size of arrays: int ar[3]; // Or: int ar = {54, 47, 99} sizeof(ar) 12 as well for arrays whose size is determined at run-time: int n = 3; int ar[n]; // Or: int ar[fun_that_returns_3()]; sizeof(ar) 12 CS61C L05 Introduction to C (pt 3) (8) Garcia, Spring 2008 UCB Dynamic Memory Allocation (2/4) To allocate room for something new to point to, use...
View Full Document

## 2008SpCS61C-L05-ddg-c3-6up - CS61C L05 Introduction to C...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online