DOC PREVIEW
Berkeley COMPSCI 61C - Lecture Notes

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 61C L4 Structs (1)A Carle, Summer 2005 © UCBinst.eecs.berkeley.edu/~cs61c/su05CS61C : Machine StructuresLecture #4: Strings & Structs2005-06-23Andy CarleCS 61C L4 Structs (2)A Carle, Summer 2005 © UCBReview: Arrays•Arrays are (almost) identical to pointers•char *string and char string[] are nearly identical declarations- They differ in subtle ways: incrementing, declaration of filled arrays- Key Difference: an array variable is a CONSTANT pointer to the first element.• ar[i] ÍÎ *(ar+i)CS 61C L4 Structs (3)A Carle, Summer 2005 © UCBReview: Arrays and Pointers• Array size n; want to access from 0 to n-1:Array Indexing Versions:#define ARSIZE 10int ar[ARSIZE];int i=0, sum = 0;...while (i < ARSIZE)sum += ar[i++];orwhile (i < ARSIZE)sum += *(ar + i++);Pointer Indexing Version:#define ARSIZE 10int ar[ARSIZE];int *p = ar, *q = &ar[10]*; int sum = 0;...while (p < q)sum += *p++;* C allows 1 past end of array!CS 61C L4 Structs (4)A Carle, Summer 2005 © UCBReview: Common C Errors•There is a difference between assignment and equality•a = bis assignment•a == b is an equality test•This is one of the most common errors for beginning C programmers!•Precedence Rules• int **a = {{1, 2}, {3, 4}}• *a[1]++; ([ ] > *)CS 61C L4 Structs (5)A Carle, Summer 2005 © UCBTopic Outline•Strings•Handles•Structs•Heap Allocation Intro•Linked List ExampleCS 61C L4 Structs (6)A Carle, Summer 2005 © UCBC Strings (1/3)•A string in C is just an array of characters.char string[] = "abc";•How do you tell how long a string is?• Last character is followed by a 0 byte (null terminator)int strlen(char s[]){int n = 0;while (s[n] != 0) n++; /* ‘\0’ */return n;}CS 61C L4 Structs (7)A Carle, Summer 2005 © UCBC Strings Headaches (2/3)• One common mistake is to forget to allocate an extra byte for the null terminator.• More generally, C requires the programmer to manage memory manually (unlike Java or C++).• When creating a long string by concatenating several smaller strings, the programmer must insure there is enough space to store the full string!• What if you don’t know ahead of time how big your string will be?• String constants are immutable:• char *f = “abc”; f[0]++; /* illegal */- Because section of mem where “abc” lives is immutable.• char f [ ] = “abc”; f[0]++; /* Works! */- Because, in declaration, c copies abc into space allocated for f.CS 61C L4 Structs (8)A Carle, Summer 2005 © UCBC String Standard Functions (3/3)• 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 and return dst. The caller must ensure that dst has enough memory to hold the data to be copied.CS 61C L4 Structs (9)A Carle, Summer 2005 © UCBPointers to 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…CS 61C L4 Structs (10)A Carle, Summer 2005 © UCBPointers to 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(&y);printf(“y = %d\n”, y);y = 6…review…CS 61C L4 Structs (11)A Carle, Summer 2005 © UCBPointers to 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 = 5050 60 70AqCS 61C L4 Structs (12)A Carle, Summer 2005 © UCBPointers to pointers (4/4)•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 = 6050 60 70AqqCS 61C L4 Structs (13)A Carle, Summer 2005 © UCBC structures : Overview (1/3)•A struct is a data structure composed of simpler data types.• Like a class in Java/C++ but without methods or inheritance. Don’t get hung up on this comparison.struct point {int x;int y;};void PrintPoint(struct point p){printf(“(%d,%d)”, p.x, p.y);}CS 61C L4 Structs (14)A Carle, Summer 2005 © UCBC structures: Pointers to them (2/3)•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);CS 61C L4 Structs (15)A Carle, Summer 2005 © UCBHow big are structs? (3/3)•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 yCS 61C L4 Structs (16)A Carle, Summer 2005 © UCBDynamic Memory Allocation (1/4)•C has operator sizeof() which gives size in bytes (of type or variable)•Assume size of objects can be misleading & is bad style, so use sizeof(type)• Many years ago an int was 16 bits, and programs assumed it was 2 bytesCS 61C L4 Structs (17)A Carle, Summer 2005 © UCBDynamic Memory Allocation (2/4)•To allocate room for something new to point to, use malloc() (with the help of a typecast and sizeof):ptr = (int *) malloc (sizeof(int));• Now, ptr points to a space somewhere in memory of size (sizeof(int)) in bytes.•(int *) simply tells the compiler what will go into that space (called a typecast).•malloc is almost never used for 1 varptr = (int *) malloc (n*sizeof(int));• This allocates an array of n integers.CS 61C L4 Structs (18)A Carle, Summer 2005 © UCBDynamic Memory Allocation (3/4)•Once malloc() is called, the memory location might contain anything, so don’t use it until you’ve set its value.•After dynamically allocating space, we must dynamically free it:free(ptr);•Use this command to clean up.• OS keeps track of size to free.CS 61C L4 Structs (19)A Carle, Summer 2005 © UCBDynamic Memory Allocation (4/4)•Malloc does notalways succeed.• System could be out of memory• An error occurred during the memory request• Operating system just doesn’t like you today…•Always check the pointer you get back to make sure it is not NULL.• int *p;if ((p = (int*) malloc(10 * sizeof(int))) == NULL){ /*do something to recover */}CS 61C L4 Structs (20)A


View Full Document

Berkeley COMPSCI 61C - Lecture Notes

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Lecture 7

Lecture 7

31 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?