Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng
|
|
- Lydia Blake
- 5 years ago
- Views:
Transcription
1 Slide Set 4 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018
2 ENCM 335 Fall 2018 Slide Set 4 slide 2/49 Contents The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
3 ENCM 335 Fall 2018 Slide Set 4 slide 3/49 Outline of Slide Set 4 The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
4 ENCM 335 Fall 2018 Slide Set 4 slide 4/49 The sizeof operator sizeof is an operator that can be used to determine how many bytes of memory are needed to hold the 1 s and 0 s belonging to a variable, function parameter, or other data object. Here are the allowable ways to make an expression with sizeof: sizeof expression sizeof( expression ) sizeof( type ) The first two above expressions are slightly different-looking ways to say the same thing. The last two expressions look like function calls, but they are not function calls.
5 ENCM 335 Fall 2018 Slide Set 4 slide 5/49 A sizeof expression produces an integer that is not of type int. The type is actually size_t, which is an unsigned integer type with enough range to represent the size of the largest variable or array you could create with whatever C programming system you are using. For most current C programming systems for laptops, desktops, and servers, size_t is a 64-bit unsigned integer type. To print the value of a sizeof expression with printf, use %zd. What would be the output of the program on the next slide, first assuming a typical desktop from around the year 2000, then assuming a typical desktop system of 2018?
6 #include <stdio.h> int main(void) { char c = A ; int i = 42; double d[3]; char *p; int *q; double *r; } printf("one: %zd\n", sizeof c); printf("two: %zd\n", sizeof(c)); printf("three: %zd\n", sizeof(char)); printf("sizeof(i): %zd\n", sizeof(i)); printf("sizeof(d): %zd\n", sizeof(d)); printf("pointer sizes: %zd, %zd, %zd\n", sizeof p, sizeof q, sizeof r); printf("sizeof a sizeof: %zd\n", sizeof(sizeof c)); return 0;
7 ENCM 335 Fall 2018 Slide Set 4 slide 7/49 Outline of Slide Set 4 The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
8 ENCM 335 Fall 2018 Slide Set 4 slide 8/49 Computer arithmetic The term computer arithmetic refers generally to the systems by which computers do things such as addition, subtraction, multiplication, and division of numbers; inequality comparison of numbers using operators such as <, <=, >, and >=. Two main types of computer arithmetic are integer arithmetic and floating-point arithmetic.
9 ENCM 335 Fall 2018 Slide Set 4 slide 9/49 An example of integer arithmetic in C is the addition in this fragment: int a = 42, b = 9, c; c = a + b; With most current C systems, int is 32-bit two s-complement, so the addition can be done with a 32-bit integer adder, as discussed in ENEL 353.
10 ENCM 335 Fall 2018 Slide Set 4 slide 10/49 The C (and C++) double type is an example of a floating-point type. Here is an example of floating-point arithmetic: double d = 3.7, e = 0.2, f; f = d + e; Here 64-bit patterns approximating 3.7 and 0.2 will be added using a circuit called a floating-point adder, a circuit which is quite different from and much more complicated than an integer adder.
11 ENCM 335 Fall 2018 Slide Set 4 slide 11/49 Outline of Slide Set 4 The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
12 ENCM 335 Fall 2018 Slide Set 4 slide 12/49 Pointer arithmetic In addition to integer and floating-point arithmetic, C and C++ support a third form of arithmetic, called pointer arithmetic. As you might guess, pointer arithmetic is arithmetic involving memory addresses. More specifically, pointer arithmetic is arithmetic involving addresses of array elements. It s crucial to understand that all addition and subtraction involving pointers in some way takes array element size into account.
13 ENCM 335 Fall 2018 Slide Set 4 slide 13/49 Array element organization in memory To understand pointer arithmetic, it s important to know this fact: In C and C++, it s guaranteed that arrays will be in contiguous chunks of memory, with element 0 at the lowest address of all the element addresses, element 1 at the next lowest address, and so on. Let s make sketches to show how the following arrays are organized, assuming the given addresses for elements with index 0. char a[5]; // &a[0] is 7800 int b[4]; // &b[0] is 7808 double c[3]; // &c[0] is 7824 (We ll assume sizes for int and double that are most common in C systems in 2018.)
14 ENCM 335 Fall 2018 Slide Set 4 slide 14/49 To repeat: In C and C++, it s guaranteed that arrays will be in contiguous chunks of memory, with element 0 at the lowest address of all the element addresses, element 1 at the next lowest address, and so on. Let s write a formula for the address of element i of an array, given the address of element 0 and the size of an element.
15 ENCM 335 Fall 2018 Slide Set 4 slide 15/49 Outline of Slide Set 4 The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
16 ENCM 335 Fall 2018 Slide Set 4 slide 16/49 Pointer addition In C and C++ it can make sense to add together a pointer and some kind of integer. It never makes sense to add two pointers together. Pointer addition generally does this: Start with the address of an array element, and generate the address of some other element in the same array. (We ll see later that there is a slight extension to this rule.) Here s a very brief summary of addition with pointers: If p is a pointer and k is some kind of integer, then *(p + k) means exactly the same thing as p[k].
17 ENCM 335 Fall 2018 Slide Set 4 slide 17/49 Here s another way to say the same thing: If p is a pointer and k is some kind of integer, then p + k means exactly the same thing as &p[k]. For the example below, let s draw blob-and-arrow diagrams for points (1) and (2), then another diagram for point (2), assuming that the address of x[0] is 9008 and that the size of a double is 8 bytes. int main(void) { double x[4] = { 1.5, 2.5, 3.5, 4.5 }; double *y; y = x + 2; // (1) *(y + 1) += 3.0; // (2) return 0; }
18 ENCM 335 Fall 2018 Slide Set 4 slide 18/49 Pointer addition with ++ or += These are allowed, and do what you expect if you know what pointer + integer means. For example... void copy1(char *dest, const char *src); void copy2(char *dest, const char *src); int main(void) { char a[4], b[4]; copy1(a, "foo"); copy2(b, "bar"); return 0; } //... program continues on next slide...
19 // functions like strcpy, but without return values void copy1(char *dest, const char *src) { int i; for (i = 0; src[i]!= \0 ; i++) dest[i] = src[i]; dest[i] = \0 ; // (1) return; } void copy2(char *dest, const char *src) { for ( ; *src!= \0 ; src++, dest++) *dest = *src; *dest = \0 ; // (2) return; } Let s draw diagrams for points (1) and (2) to help us understand what src++ and dest++ do.
20 ENCM 335 Fall 2018 Slide Set 4 slide 20/49 In copy2 the expression src++ modifies the contents of the parameter src. The type of src is const char *. Why is src++ allowed within the function definition of copy2?
21 ENCM 335 Fall 2018 Slide Set 4 slide 21/49 A goofy example to help understand the rules int main(void) { int x; x = 12[" ABCDEF"]; return 0; } What value does the assignment give to x, and why? (It helps to know that expression 1 + expression 2 has to mean exactly the same thing as expression 2 + expression 1 ). (Note: Don t put weird things like this in production code!)
22 ENCM 335 Fall 2018 Slide Set 4 slide 22/49 Outline of Slide Set 4 The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
23 ENCM 335 Fall 2018 Slide Set 4 slide 23/49 Pointer subtraction: Part I Let s look at expressions of the form or pointer - integer pointer -- Assuming that the value of integer is positive, the idea in the two expressions is similar: Starting from an element in the middle or near the end of an array, go backwards, or in other words, generate the address of an element closer to element 0. For the example program on the next slide, let s make a diagram for point (1), and determine the program output.
24 ENCM 335 Fall 2018 Slide Set 4 slide 24/49 #include <stdio.h> int main(void) { double x[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 }; double *a, *b; a = x + 4; b = a - 2; a--; // (1) printf("%f %f %f %f\n", *a, a[1], *b, *(b - 1)); return 0; } Suppose that the address of x[0] is 6000, and that the size of a double is 8 bytes. What are the numbers in a and b at point (1)?
25 ENCM 335 Fall 2018 Slide Set 4 slide 25/49 Pointer subtraction: Part II There is another form of subtraction involving pointers: pointer1 - pointer2 For this to make sense the two pointers must be of exactly the same type, and the values of the pointers must be addresses related to a single array. The result of the subtraction is the distance between two array elements, measured not in bytes but in number of array elements. What are the values of b and c at point (1) on the next slide?
26 ENCM 335 Fall 2018 Slide Set 4 slide 26/49 int main(void) { double x[4] = { 1.1, 2.2, 3.3, 4.4 }; double *a; int y[5] = { 10, 20, 30, 40, 50 }; int b, c; a = &x[3]; b = a - &x[1]; c = y - &y[4]; // (1) } return 0; To solve this problem, we DON T we need to know the size in bytes of an int or a double. Why not?
27 ENCM 335 Fall 2018 Slide Set 4 slide 27/49 Outline of Slide Set 4 The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
28 ENCM 335 Fall 2018 Slide Set 4 slide 28/49 Null pointer values So far we ve seen examples of pointer expressions having the following kinds of values: some unpredictable address, as a result of the pointer variable being uninitialized and not yet assigned-to; the address of a variable that is not an array element; the address of an array element. There are a few other possible kinds of values for pointer expressions. One very important kind of value for a pointer expression is the null pointer value, which indicates that a pointer definitely does not point to any data at all.
29 ENCM 335 Fall 2018 Slide Set 4 slide 29/49 To give the null pointer value to a pointer variable, you can use either 0, which, unlike other int constants, is also a pointer constant, or NULL, which is defined in <stdio.h>, <stdlib.h>, and a few other library header files. Let s make a diagram for point (1) in the program on the next slide. Let s then go over a few different graphical ways to indicate a null pointer value.
30 ENCM 335 Fall 2018 Slide Set 4 slide 30/49 #include <stdlib.h> int main(void) { int a, b[3]; int *p1, *p2, *p3, *p4, *p5; p1 = &a; p2 = b; p4 = 0; p5 = NULL; // (1) // Use the pointers here... return 0; }
31 ENCM 335 Fall 2018 Slide Set 4 slide 31/49 Practical uses for null pointers There are many practical uses. Probably most common: A function is supposed to search for some data, and return the address of that data. To indicate that the search failed, the function can return a null pointer. Another common use: Avoiding the mysterious and inconsistent program behaviour that can result from use of uninitialized pointers. See the next slide...
32 ENCM 335 Fall 2018 Slide Set 4 slide 32/49 int main(void) { int a = 33; int *p; } *p = 42; //... return 0; int main(void) { int a = 33; int *p; p = 0; *p = 42; } //... return 0; Both programs are defective! What will be the difference in behaviour between the two programs?
33 ENCM 335 Fall 2018 Slide Set 4 slide 33/49 Outline of Slide Set 4 The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
34 ENCM 335 Fall 2018 Slide Set 4 slide 34/49 Pointer comparison Consider the expression pointer1 op pointer2. If op is one of <, <=, >, or >=, the expression is only guaranteed to be meaningful if the values of both pointers are addresses related to a single array, and indicates where two array elements are located relative to each other. For example, what does this mean? pointer1 < pointer2
35 ENCM 335 Fall 2018 Slide Set 4 slide 35/49 // Draw diagrams for points (1) and (2). int main(void) { int a[5] = {100, 200, 300, 400, 500}; int *p, *q, temp; p = a; q = a + 4; // (1) while (p < q) { temp = *p; *p = *q; *q = temp; p++; q--; } } // (2) return 0;
36 ENCM 335 Fall 2018 Slide Set 4 slide 36/49 Pointer comparison with == and!= This expression... pointer1 == pointer2... has a value of 1 if both pointers point to the same thing, and also has a value of 1 if both pointers are null. Otherwise the value is 0. So what does!= mean when placed between two pointer expressions?
37 ENCM 335 Fall 2018 Slide Set 4 slide 37/49 Pointers pointing just past the last element of an array It is allowable and sometimes useful to generate the address of an array element one element past the last actual element of an array such an address can safely be used in pointer comparisons. It is not safe to try to use such an address to access data! The above two points are illustrated on the next slide...
38 ENCM 335 Fall 2018 Slide Set 4 slide 38/49 int main(void) { int a[3]; int *p = a, *beyond = a + 3; // (1) while (p!= beyond) { // Comparison is SAFE. *p = 789; p++; } // (2) // The commented-out statement below is NOT SAFE! // *p = 456; } return 0; Let s draw diagrams for points (1) and (2), then make a few related notes.
39 ENCM 335 Fall 2018 Slide Set 4 slide 39/49 Do you need to know about pointer arithmetic? Reasons for a NO answer: Anything you can do with pointer arithmetic in C can be done just as efficiently using square brackets ([ and ]) and indexes. Reasons for a YES answer: You will be tested on pointer arithmetic on the Quiz #2, the midterm, and the final exam. You will be expected to be comfortable with pointer arithmetic when you start ENCM 369. There is a lot of pointer arithmetic in real-world C code. Iterator types, used with C++ container types, are much easier to understand if you already understand C pointer arithmetic.
40 ENCM 335 Fall 2018 Slide Set 4 slide 40/49 Outline of Slide Set 4 The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
41 ENCM 335 Fall 2018 Slide Set 4 slide 41/49 Initialization versus assignment The = operator in C can be used for initialization and assignment. These two things are not exactly the same. Let s write down carefully what the difference is. The distinction is not very important when we work with variables of simple types like int and double let s do a quick example of that. The distinction is important when we look at initialization and assignment involving pointers, arrays, and/or string constants.
42 ENCM 335 Fall 2018 Slide Set 4 slide 42/49 Initialization of pointers Consider this code fragment: int a, b; int *p = &a; int *q; q = &b; *q = 99; Let s write down exactly what each of the three above uses of = mean. What does this mean? int *p = 0; How about this? int *p = 42;
43 ENCM 335 Fall 2018 Slide Set 4 slide 43/49 Initializers for arrays Initializers for array variables usually involve { and }, which are called left brace and right brace. (There is another kind of initializer for arrays of char we ll get to that special case soon, but not right away.) There are two main cases to consider: number of elements in array is not explicitly specified; number of elements in array is explicitly specified.
44 ENCM 335 Fall 2018 Slide Set 4 slide 44/49 Number of elements not explicitly specified This is pretty easy to explain with a couple of examples: int x[ ] = {10, 20, 30, 40}; double y[ ] = {1.5, 2.5, 3.5}; How many elements will x and y have, and what will their initial values be?
45 ENCM 335 Fall 2018 Slide Set 4 slide 45/49 Number of elements explicitly specified Now we need to know some not-totally-obvious rules... int a[3] = {111, 222, 333}; int b[4] = {444, 555}; double c[4] = {1.0}; int d[3] = {1000, 2000, 3000, 4000}; How many elements will a, b, c and d have, and what will their initial values be?
46 ENCM 335 Fall 2018 Slide Set 4 slide 46/49 Outline of Slide Set 4 The sizeof operator Computer Arithmetic Pointer Arithmetic Pointer Addition Pointer subtraction Null pointer values Pointer comparison Initialization of Arrays and Pointers Initializers Involving String Constants
47 ENCM 335 Fall 2018 Slide Set 4 slide 47/49 Initializers involving string constants Before we discuss this kind of initializer, let s review a rule about function parameter types, and think about an apparently similar situation for variables. As a function parameter type declaration, type foo[ ] means exactly the same thing as type *foo. What is the type of foo here? So, do var1 and var2 have the same type in these two variable declarations? type var1[ ] = initializer 1 ; type *var2 = initializer 2 ;
48 ENCM 335 Fall 2018 Slide Set 4 slide 48/49 Let s draw a diagram for point (1). Then, let s write down a few remarks about the variables s and t. int main(void) { char *s = "ick"; char t[ ] = "meh"; // (1) //... more code... return 0; }
49 ENCM 335 Fall 2018 Slide Set 4 slide 49/49 Does it seem to you that programming in C and C++ requires knowledge of an unnecessarily large number of unnecessarily weird rules? Dennis Ritchie, the late designer of C, and Bjarne Stroustrup, the original designer of C++ and still an important figure in the C++ community, have essentially admitted in essays, interviews, and books that their languages have features they ve wished they could take back. On the other hand, it s pretty clear from the record that both designers were proud of the many things they got right. That s justified by the wide adoption and continued use of C and C++!
Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 4 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01
More informationSlide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng
Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide
More informationSlide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 3 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01
More informationSlide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 3 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 3 slide 2/46
More informationSlide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng
Slide Set 5 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section
More informationSlide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 6 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide
More informationSlide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 8 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide
More informationSlide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng
Slide Set 9 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2018 ENCM 335 Fall 2018 Slide Set 9 slide 2/32
More informationSlide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 1 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 1 slide 2/43
More informationBinary Representations and Arithmetic
Binary Representations and Arithmetic 9--26 Common number systems. Base : decimal Base 2: binary Base 6: hexadecimal (memory addresses) Base 8: octal (obsolete computer systems) Base 64 (email attachments,
More informationSlide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng
Slide Set 3 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018 Section
More informationSlide Set 5. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng
Slide Set 5 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary February 2018 ENCM 369 Winter 2018 Section
More informationInteger Multiplication and Division
Integer Multiplication and Division for ENCM 369: Computer Organization Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 208 Integer
More informationENCM 339 Fall 2017 Tutorial for Week 8
ENCM 339 Fall 2017 Tutorial for Week 8 for section T01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 2 November, 2017 ENCM 339 T01 Tutorial
More informationT02 Tutorial Slides for Week 2
T02 Tutorial Slides for Week 2 ENEL 353: Digital Circuits Fall 2017 Term Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 19 September, 2017
More informationSlide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 14 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2016 ENCM 339 Fall 2016 Slide Set 14 slide 2/35
More informationSlide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 5 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2016 ENCM 339 Fall 2016 Slide Set 5 slide 2/32
More informationContents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides
Slide Set 1 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section
More informationSlide Set 5. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 5 for ENEL 353 Fall 207 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 207 SN s ENEL 353 Fall 207 Slide Set 5 slide
More informationSlide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng
Slide Set 11 for ENCM 369 Winter 2015 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2015 ENCM 369 W15 Section
More informationSlide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng
Slide Set 1 for ENEL 339 Fall 2014 Lecture Section 02 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2014 ENEL 353 F14 Section
More informationSlide Set 1 (corrected)
Slide Set 1 (corrected) for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018
More informationSlide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 14 for ENCM 339 Fall 2015 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2015 SN s ENCM 339 Fall 2015 Slide Set 14 slide
More informationENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2
page 1 of 11 ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2 Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2017 Lab instructions and
More informationENCM 335 Fall 2018 Tutorial for Week 13
ENCM 335 Fall 2018 Tutorial for Week 13 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 06 December, 2018 ENCM 335 Tutorial 06 Dec 2018 slide
More informationPointers in C/C++ 1 Memory Addresses 2
Pointers in C/C++ Contents 1 Memory Addresses 2 2 Pointers and Indirection 3 2.1 The & and * Operators.............................................. 4 2.2 A Comment on Types - Muy Importante!...................................
More informationENCM 369 Winter 2019 Lab 6 for the Week of February 25
page of ENCM 369 Winter 29 Lab 6 for the Week of February 25 Steve Norman Department of Electrical & Computer Engineering University of Calgary February 29 Lab instructions and other documents for ENCM
More informationENCM 335 Fall 2018 Lab 2 for the Week of September 24
page 1 of 8 ENCM 335 Fall 2018 Lab 2 for the Week of September 24 Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2018 Lab instructions and other documents
More informationSlide Set 1. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 1 for ENEL 353 Fall 2017 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2017 SN s ENEL 353 Fall 2017 Slide Set 1 slide
More informationData Representation and Storage. Some definitions (in C)
Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use pointer arithmetic correctly Explain
More informationMemory, Arrays & Pointers
1 Memory, Arrays & Pointers Memory int main() { char c; int i,j; double x; c i j x 2 Arrays Defines a block of consecutive cells int main() { int i; int a[3]; i a[0] a[1] a[2] Arrays - the [ ] operator
More informationprintf( Please enter another number: ); scanf( %d, &num2);
CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful
More informationWhat Every Programmer Should Know About Floating-Point Arithmetic
What Every Programmer Should Know About Floating-Point Arithmetic Last updated: October 15, 2015 Contents 1 Why don t my numbers add up? 3 2 Basic Answers 3 2.1 Why don t my numbers, like 0.1 + 0.2 add
More informationC Pointers. CS 2060 Week 6. Prof. Jonathan Ventura
CS 2060 Week 6 1 Pointer Variables 2 Pass-by-reference 3 const pointers 4 Pointer arithmetic 5 sizeof 6 Arrays of pointers 7 Next Time Pointers The pointer is one of C s most powerful and important features.
More informationC Introduction. Comparison w/ Java, Memory Model, and Pointers
CS 261 Fall 2018 Mike Lam, Professor C Introduction Comparison w/ Java, Memory Model, and Pointers Please go to socrative.com on your phone or laptop, choose student login and join room LAMJMU The C Language
More informationSlide Set 8. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng
Slide Set 8 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary March 2018 ENCM 369 Winter 2018 Section 01
More informationCSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community
CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine
More informationContents Slide Set 9. Final Notes on Textbook Chapter 7. Outline of Slide Set 9. More about skipped sections in Chapter 7. Outline of Slide Set 9
slide 2/41 Contents Slide Set 9 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014
More informationSlides for Lecture 15
Slides for Lecture 5 ENEL 353: Digital Circuits Fall 203 Term Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October, 203 ENEL 353 F3 Section
More informationAgenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion
CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Numbers wrap-up This is not on the exam! Break C Primer Administrivia,
More informationContents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs
Slide Set 2 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section
More informationCS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz
CS 61C: Great Ideas in Computer Architecture Lecture 2: Numbers & C Language Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Numbers wrap-up This is not on the exam! Break C Primer Administrivia,
More informationPointers. 10/5/07 Pointers 1
Pointers 10/5/07 Pointers 1 10/5/07 Pointers 2 Variables Essentially, the computer's memory is made up of bytes. Each byte has an address, associated with it. 10/5/07 Pointers 3 Variable For example 1:#include
More informationSlide Set 7. for ENCM 501 in Winter Term, Steve Norman, PhD, PEng
Slide Set 7 for ENCM 501 in Winter Term, 2017 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2017 ENCM 501 W17 Lectures: Slide
More informationENCM 369 Winter 2015 Lab 6 for the Week of March 2
page of 2 ENCM 369 Winter 25 Lab 6 for the Week of March 2 Steve Norman Department of Electrical & Computer Engineering University of Calgary February 25 Lab instructions and other documents for ENCM 369
More informationSlide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 18 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary December 2017 ENCM 339 Fall 2017 Section 01
More informationCS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor
CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static
More informationECE264 Fall 2013 Exam 1, September 24, 2013
ECE264 Fall 2013 Exam 1, September 24, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.
More informationSlide Set 5. for ENCM 501 in Winter Term, Steve Norman, PhD, PEng
Slide Set 5 for ENCM 501 in Winter Term, 2017 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2017 ENCM 501 W17 Lectures: Slide
More informationENCM 501 Winter 2015 Tutorial for Week 5
ENCM 501 Winter 2015 Tutorial for Week 5 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 11 February, 2015 ENCM 501 Tutorial 11 Feb 2015 slide
More informationSlide Set 18. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 18 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary December 2016 ENCM 339 Fall 2016 Slide Set 18 slide 2/26
More informationChapter 1 & 2 Introduction to C Language
1 Chapter 1 & 2 Introduction to C Language Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 1 & 2 - Introduction to C Language 2 Outline 1.1 The History
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationby Pearson Education, Inc. All Rights Reserved.
Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]
More informationThe University of Calgary. ENCM 339 Programming Fundamentals Fall 2016
The University of Calgary ENCM 339 Programming Fundamentals Fall 2016 Instructors: S. Norman, and M. Moussavi Wednesday, November 2 7:00 to 9:00 PM The First Letter of your Last Name:! Please Print your
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationFast Introduction to Object Oriented Programming and C++
Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming
More informationC for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.
C for Java Programmers 1 Last Week Very short history of C Overview of the differences between C and Java The C language (keywords, types, functies, etc.) Compiling (preprocessor, compiler, linker) C for
More informationUniversity of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman
page 1 of 6 University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman Fall 2018 MIDTERM TEST Thursday, November 1 6:30pm to 8:30pm Please do not write your
More informationCS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018
CS 3: Intro to Systems Binary Representation Kevin Webb Swarthmore College September 6, 28 Reading Quiz Announcements Sign up for Piazza! Let me know about exam conflicts! Register your clicker (clarification
More informationMARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012
MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)
More informationCS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015
CS 141 - Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015 You may take this test with you after the test, but you must turn in your answer sheet. This test has the following sections:
More information11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {
This document contains the questions and solutions to the CS107 midterm given in Spring 2016 by instructors Julie Zelenski and Michael Chang. This was an 80-minute exam. Midterm questions Problem 1: C-strings
More informationCS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29
CS 31: Introduction to Computer Systems 03: Binary Arithmetic January 29 WiCS! Swarthmore Women in Computer Science Slide 2 Today Binary Arithmetic Unsigned addition Subtraction Representation Signed magnitude
More informationToday s lecture. Continue exploring C-strings. Pointer mechanics. C arrays. Under the hood: sequence of chars w/ terminating null
Today s lecture Continue exploring C-strings Under the hood: sequence of chars w/ terminating null Review implementation of strcpy, strncpy As abstraction: client of string.h functions Write pig latin
More informationVariables and literals
Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of
More informationEC312 Chapter 4: Arrays and Strings
Objectives: (a) Describe how an array is stored in memory. (b) Define a string, and describe how strings are stored. EC312 Chapter 4: Arrays and Strings (c) Describe the implications of reading or writing
More informationCMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009
CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009 Name Score UMBC Username Notes: a. Please write clearly. Unreadable answers receive no credit. b. For short answer questions your answer should
More informationCS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.
CS 33 Introduction to C Part 5 CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. Basic Data Types int short char -2,147,483,648 2,147,483,647-32,768 32,767-128
More informationLecture 04 Introduction to pointers
Lecture 04 Introduction to pointers A pointer is an address in the memory. One of the unique advantages of using C is that it provides direct access to a memory location through its address. A variable
More information9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C
Overview The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments
More informationSample Examination. Family Name:... Other Names:... Signature:... Student Number:...
Family Name:... Other Names:... Signature:... Student Number:... THE UNIVERSITY OF NEW SOUTH WALES SCHOOL OF COMPUTER SCIENCE AND ENGINEERING Sample Examination COMP1917 Computing 1 EXAM DURATION: 2 HOURS
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationThe C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)
The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments
More informationCSE413 Midterm. Question Max Points Total 100
CSE413 Midterm 05 November 2007 Name Student ID Answer all questions; show your work. You may use: 1. The Scheme language definition. 2. One 8.5 * 11 piece of paper with handwritten notes Other items,
More informationCSE 333 Midterm Exam 2/14/14
Name There are 4 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed
More informationQUIZ. What is wrong with this code that uses default arguments?
QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code
More informationPhysics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That
Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That 1. Introduction You have seen situations in which the way numbers are stored in a computer affects a program. For example, in the
More informationC BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.
C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,
More informationarrays and strings week 3 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 22 imperative programming review
of char imperative week 3 and Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 22 : miscellaneous of char several library functions are have put or get in their name
More informationCAAM 420 FALL 2012 Lecture 9. Mishael Owoyemi
CAAM 420 FALL 2012 Lecture 9 Mishael Owoyemi September 21, 2012 Table of Contents 1 Variables 3 1.1 Recap and Introduction.................................. 3 1.1.1 Recap........................................
More informationSlide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng
Slide Set 4 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018 Section
More informationSlide Set 9. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng
Slide Set 9 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary March 2018 ENCM 369 Winter 2018 Section 01
More informationSlides for Lecture 6
Slides for Lecture 6 ENCM 501: Principles of Computer Architecture Winter 2014 Term Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 28 January,
More informationCSCI S-Q Lecture #12 7/29/98 Data Structures and I/O
CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O Introduction The WRITE and READ ADT Operations Case Studies: Arrays Strings Binary Trees Binary Search Trees Unordered Search Trees Page 1 Introduction
More informationCS 61c: Great Ideas in Computer Architecture
Arrays, Strings, and Some More Pointers June 24, 2014 Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address
More informationFirst of all, it is a variable, just like other variables you studied
Pointers: Basics 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 address (rather than the value)
More informationENCM 369 Winter 2017 Lab 3 for the Week of January 30
page 1 of 11 ENCM 369 Winter 2017 Lab 3 for the Week of January 30 Steve Norman Department of Electrical & Computer Engineering University of Calgary January 2017 Lab instructions and other documents for
More informationLecture 05 I/O statements Printf, Scanf Simple statements, Compound statements
Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple
More informationC++ for Java Programmers
Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More informationGoals of this Lecture
C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing
More informationMachine Arithmetic 8/31/2007
Machine Arithmetic 8/31/2007 1 Opening Discussion Let's look at some interclass problems. If you played with your program some you probably found that it behaves oddly in some regards. Why is this? What
More informationCOSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)
COSC 243 Data Representation 3 Lecture 3 - Data Representation 3 1 Data Representation Test Material Lectures 1, 2, and 3 Tutorials 1b, 2a, and 2b During Tutorial a Next Week 12 th and 13 th March If you
More informationPointers and References
Steven Zeil October 2, 2013 Contents 1 References 2 2 Pointers 8 21 Working with Pointers 8 211 Memory and C++ Programs 11 212 Allocating Data 15 22 Pointers Can Be Dangerous 17 3 The Secret World of Pointers
More informationENCM 501 Winter 2016 Assignment 1 for the Week of January 25
page 1 of 5 ENCM 501 Winter 2016 Assignment 1 for the Week of January 25 Steve Norman Department of Electrical & Computer Engineering University of Calgary January 2016 Assignment instructions and other
More informationComputer Architecture I Midterm I
Computer Architecture I Midterm I April 11 2017 Computer Architecture I Midterm I Chinese Name: Pinyin Name: E-Mail... @shanghaitech.edu.cn: Question Points Score 1 1 2 12 3 16 4 14 5 18 6 17 7 22 Total:
More informationCSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11
... 1/16 CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11 Alice E. Fischer February 3, 2016 ... 2/16 Outline Basic Types and Diagrams ... 3/16 Basic Types and Diagrams Types in C C has
More informationCS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays
CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements
More informationSlide Set 15 (Complete)
Slide Set 15 (Complete) for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2017 ENCM 339 Fall 2017
More information