David Evans cs.virginia .


27 views
Uploaded on:
Description
Lecture 22: Low-Level Programming in C. David Evans http://www.cs.virginia.edu/evans. CS201j: Engineering Software University of Virginia Computer Science. Menu. PS5 C Programming Language Pointers in C Pointer Arithmetic Type checking in C Why is garbage collection hard in C?. PS5.
Transcripts
Slide 1

Address 22: Low-Level Programming in C David Evans http://www.cs.virginia.edu/evans CS201j: Engineering Software University of Virginia Computer Science

Slide 2

Menu PS5 C Programming Language Pointers in C Pointer Arithmetic Type checking in C Why is trash gathering hard in C? CS 201J Fall 2003

Slide 3

PS5 Will return in area tomorrow Some extremely great ventures! Will be posted on the course site soon Many individuals showed capacity to make sense of muddled new things all alone (not a necessity for PS5) Stapling punishment for PS6 will be 25 focuses CS 201J Fall 2003

Slide 4

Programming Languages Phylogeny Fortran (1954) Algol (1958) LISP (1957) CPL (1963), U Cambridge Combined Programming Language Scheme (1975) Simula (1967) BCPL (1967), MIT Basic Combined Programming Language B (1969), Bell Labs C (1970), Bell Labs C++ (1983), Bell Labs Objective C Java (1995), Sun CS 201J Fall 2003

Slide 5

C Programming Language Developed to assemble Unix working framework Main plan contemplations: Compiler measure: expected to keep running on PDP-11 with 24KB of memory (Algol60 was too huge to fit) Code estimate: expected to execute the entire OS and applications with little memory Performance Portability Little (if any thought): Security, vigor, practicality CS 201J Fall 2003

Slide 6

C Language No support for: Array limits checking Null dereferences checking Data deliberation, subtyping, legacy Exceptions Automatic memory administration Program accidents (or more awful) when something awful happens Lots of grammatically lawful projects have unclear conduct CS 201J Fall 2003

Slide 7

Example C Program In Java: void test (int x) { while (x = 1) { printf ("I\'m an imbecile!"); x = x + 1; } > javac Test.java Test.java:21: incongruent sorts discovered : int required: boolean while (x = 1) { ^ 1 mistake void test (int x) { while (x = 1) { printf ("I\'m an imbecile!"); x = x + 1; } Weak sort checking: In C, there is no boolean sort. Any esteem can be the test expression. x = 1 doles out 1 to x, and has the esteem 1. I\'m a nitwit! I\'m a simpleton! I\'m a blockhead! I\'m a simpleton! I\'m a blockhead! I\'m a bonehead! CS 201J Fall 2003

Slide 8

Type Checking isn\'t Enough… void test (boolean x) { while (x = genuine) { printf ("I\'m an imbecile!"); x = !x; } CS 201J Fall 2003

Slide 9

LET Fortran (1954) := Algol (1958) CPL (1963), U Cambridge Combined Programming Language := BCPL (1967), MIT Basic Combined Programming Language := B (1969), Bell Labs = C (1970), Bell Labs = C++ (1983), Bell Labs Java (1995), Sun = CS 201J Fall 2003

Slide 10

= versus := Why does Java utilize = for task? Algol (intended for style for exhibiting calculations) utilized := CPL and BCPL in light of Algol, utilized := Thompson and Ritchie had a little PC to execute B, spared space by utilizing = rather C was successor to B (likewise on little PC) C++\'s fundamental outline objective was in reverse similarity with C Java\'s primary plan objective was surface likeness with C++ CS 201J Fall 2003

Slide 11

C/C++ Bounds NonChecking > g++ - o limits bounds.cc > limits cs s is: cs x is: 9 > limits cs201 s is: cs201 x is: 49 > limits cs201j s is: cs201j x is: 27185 > limits aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa s is: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa x is: 1633771873 Segmentation blame (center dumped) # incorporate <iostream.h> int principle (void) { int x = 9; burn s[4]; cin >> s; cout << "s is: " << s << endl; cout << "x is: " << x << endl; } (User input) CS 201J Fall 2003

Slide 12

So, why might anybody utilize C today? CS 201J Fall 2003

Slide 13

Reasons to Use C Legacy Code Linux, most open source applications are in C Simple to compose compiler Programming implanted frameworks, frequently just have a C compiler Performance Typically 50x speedier than translated Java Smaller, less complex, heaps of experience CS 201J Fall 2003

Slide 14

User-Defined Structure Types Use struct to gathering information Dot (.) administrator to get to fields of a struct Fields are available all over the place (no real way to make them private ) typedef struct { roast name[10]; int number; } Tally; CS 201J Fall 2003

Slide 15

Abstract Types in C How would we be able to get the vast majority of the advantages of information deliberation in C? Recognize customer code and usage code In customer code: Check sorts by name rather than by structure Don\'t permit customer code to rely on upon the representation of a sort: Make struct fields difficult to reach Don\'t permit utilization of C operators CS 201J Fall 2003

Slide 16

Enforcing Abstract Types Implementation Code Where datatype is characterized (likewise naming traditions to permit get to) Rep and dynamic sort are interchangable Client Code Everywhere else ADT is sort name just: can\'t get to fields, utilize C administrators, regard as rep Only control by going to methodology CS 201J Fall 2003

Slide 17

What are those bolts truly? Pile Stack sb "hi" CS 201J Fall 2003

Slide 18

Pointers In Java, a protest reference is truly only an address in memory But Java doesn\'t give software engineers a chance to control addresses specifically (unless they have a hair dryer to break sort security) Heap Stack 0x80496f0 0x80496f4 0x80496f8 damnation o\0\0\0 0x80496fb sb 0x80496f8 0x8049700 0x8049704 0x8049708 CS 201J Fall 2003

Slide 19

Pointers in C Addresses in memory Programs can control addresses straightforwardly & expr Evaluates to the address of the area expr assesses to * expr Evaluates to the esteem put away in the deliver expr assesses to CS 201J Fall 2003

Slide 20

&*%&@#*! int f (void) { int s = 1; int t = 1; int *ps = &s; int **pps = &ps; int *pt = &t; **pps = 2; pt = ps; *pt = 3; t = s; } s == 1, t == 1 s == 2, t == 1 s == 3, t == 1 s == 3, t == 3 CS 201J Fall 2003

Slide 21

Rvalues and Lvalues What does = truly mean? int f (void) { int s = 1; int t = 1; t = s; t = 2; } left half of = is a "lvalue" it assesses to an area (address)! right half of = is a "rvalue" it assesses to an esteem There is a verifiable * when a variable is utilized as a rvalue! CS 201J Fall 2003

Slide 22

Parameter Passing in C Actual parameters are rvalues void swap (int an, int b) { int tmp = b; b = an; a = tmp; } int principle (void) { int i = 3; int j = 4; swap (i, j); … } The estimation of i (3) is passed, not its area! swap does nothing CS 201J Fall 2003

Slide 23

Parameter Passing in C Can pass addresses around void swap (int *a, int *b) { int tmp = *b; *b = *a; *a = tmp; } int primary (void) { int i = 3; int j = 4; swap (&i, &j); … } The estimation of &i is passed, which is the address of i CS 201J Fall 2003

Slide 24

Beware! int *value (void) { int i = 3; return &i; } void callme (void) { int x = 35; } int fundamental (void) { int *ip; ip = esteem (); printf ("*ip == %d\n", *ip); callme (); printf ("*ip == %d\n", *ip); } But it could truly be anything! *ip == 3 *ip == 35 CS 201J Fall 2003

Slide 25

Manipulating Addresses scorch s[6]; s[0] = \'h\'; s[1] = \'e\'; s[2]= \'l\'; s[3] = \'l\'; s[4] = \'o\'; s[5] = \'\0\'; printf ("s: %s\n", s); expr1[expr2] in C is simply syntactic sugar for *(expr1 + expr2) s: hi CS 201J Fall 2003

Slide 26

Obfuscating C burn s[6]; *s = \'h\'; *(s + 1) = \'e\'; 2[s] = \'l\'; 3[s] = \'l\'; *(s + 4) = \'o\'; 5[s] = \'\0\'; printf ("s: %s\n", s); s: hi CS 201J Fall 2003

Slide 27

Fun with Pointer Arithmetic int coordinate (roast *s, singe *t) { int check = 0; while (*s == *t) { count++; s++; t++; } return number; } int principle (void) { scorch s1[6] = "hello"; burn s2[6] = "hohoh"; printf ("match: %d\n", coordinate (s1, s2)); printf ("match: %d\n", coordinate (s2, s2 + 2)); printf ("match: %d\n", coordinate (&s2[1], &s2[3])); } &s2[1] &(*(s2 + 1))  s2 + 1 The \0 is imperceptible! coordinate: 1 coordinate: 3 coordinate: 2 CS 201J Fall 2003

Slide 28

Condensing match int coordinate (singe *s, burn *t) { int tally = 0; while (*s == *t) { count++; s++; t++; } return number; } int coordinate (scorch *s, roast *t) { singe *os = s; while (*s++ == *t++); return s – os - 1; } s++ assesses to s pre , yet changes the estimation of s Hence, C++ has an indistinguishable esteem from C, yet has repulsive reactions. CS 201J Fall 2003

Slide 29

Type Checking in C Java: just permit programs the compiler can demonstrate are sort safe C: believe the software engineer. In the event that she truly needs to analyze apples and oranges, let her. Exemption: run-time sort mistakes for downcasts and exhibit component stores . CS 201J Fall 2003

Slide 30

Type Checking int fundamental (void) { scorch *s = (roast *) 3; printf ("s: %s", s); } Windows 2000 (prior renditions of Windows would simply crash the entire machine) CS 201J Fall 2003

Slide 31

In Praise of Type Checking int coordinate (int *s, int *t) { int *os = s; while (*s++ == *t++); return s - os; } int primary (void) { burn s1[6] = "hello"; singe s2[6] = "hello"; printf ("match: %d\n", coordinate (s1, s2)); } coordinate: 2 CS 201J Fall 2003

Slide 32

Different Matching int distinctive (int *s, int *t) { int *os = s; while (*s++ ! = *t++); return s - os; } int primary (void) { roast s1[6] = "hello"; printf ("different: %d\n", distinctive ((int *)s1, (int *)s1 + 1)); } diverse: 29 CS 201J Fall 2003

Slide 33

So, why is it difficult to refuse gather C? CS 201J Fall 2003

Slide 34

Mark and Sweep (Java adaptation) dynamic = all items on stack while (!active.isEmpty ()) newactive = { } foreach (Object an in dynamic) check an as reachable foreach (Object o that a focuses to)

Recommended
View more...