Systems Programming and Computer Architecture ( )

Size: px
Start display at page:

Download "Systems Programming and Computer Architecture ( )"

Transcription

1 Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture ( ) Timothy Roscoe Herbstsemester

2 4: Pointers Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe 2

3 Process address space 0xffffffffffffffff OS gives each process an address space Contains process virtual memory, visible only to it 2 32 bytes on 32 bit host 2 64 bytes on 64 bit host (shown) code, data, libraries, stack, etc. AS x

4 When the OS loads a program, it: creates an address space inspects the executable file to see what s in it (lazily) copies regions of the file into the right place in the address space does any final linking, relocation, or other needed preparation Loading 0xffffffff 0xc x x Kernel virtual memory User stack (created at runtime) Memory-mapped region for shared libraries Run-time heap (created by malloc) Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused Memory inaccessible to user code Stack pointer brk Loaded from the executable file AS x

5 4.1: Recap: the stack Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe 5

6 Stack-Based Languages Languages supporting recursion (C, C#, Eiffel, Java..) Code must be reentrant Multiple simultaneous instantiations of single procedure Need to store state of each instantiation Arguments, local variables, return pointer Stack discipline State for given procedure needed for limited time From when called to when returns Callee returns before caller does Stack allocated in Frames state for single procedure activation 6

7 Call Chain Example yoo( ) who(); who( ) ami(); ami(); ami( ) ami(); Example call chain yoo who ami ami ami ami These are all different activations Procedure ami is recursive AS

8 Stack Frames Contents Local variables Return information Temporary space Previous Frame Management Space allocated when enter procedure Set-up code Deallocated when return Finish code Frame Pointer: Stack Pointer: Frame for proc Stack Top 8

9 Example Stack who( ) ami(); ami(); yoo who ami ami ami Frame Stack yoo who ami 9

10 Example Stack ami( ) ami(); yoo who ami ami ami yoo who ami ami ami Frame ami AS Stack

11 Example Stack ami( ) yoo who ami ami ami Frame yoo who ami ami Stack 11

12 Example Stack you( ) who(); yoo who ami ami ami Frame Stack yoo ami 12

13 4.2: Pointers in C Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe 13

14 Addresses and & &x produces the virtual address where the value of x is stored. You can use %p in printf() to print it out. addresses.c #include <stdio.h> int foo(int x) return x+1; int main(int argc, char *argv[]) int x, y; int a[2]; printf("x is at %p\n", &x); printf("y is at %p\n", &y); printf("a[0] is at %p\n", &a[0]); printf("a[1] is at %p\n", &a[1]); printf("foo is at %p\n", &foo); printf("main is at %p\n", &main); return 0; AS

15 Pointers type *name; // declare a pointer type *name = address; // declare + initialize a pointer A variable that contains a memory address points to somewhere in the process virtual address space AS 2016 int main(int argc, char **argv) int x = 42; int *p; // p is a pointer to an integer p = &x; // p now stores the address of x printf("x is %d\n", x); printf("&x is %p\n", &x); printf("p is %p\n", p); return 0; pointy.c

16 Dereferencing a pointer v = *pointer; *pointer = value; // dereference a pointer // dereference / assign dereference: access the memory referred to by a pointer #include <stdio.h> AS 2016 int main(int argc, char **argv) int x = 42; int *p; // p is a pointer to an integer p = &x; // p now stores the address of x printf("x is %d\n", x); *p = 99; printf("x is %d\n", x); return 0; deref.c

17 NULL a guaranteed-to-be-invalid memory location typeof(null) is void * In C on Linux: NULL is 0x (or 0x ) Any attempt to dereference NULL segmentation fault #include <stdio.h> AS 2016 int main(int argc, char *argv[]) int *p = NULL; *p = 1; // causes a segmentation fault return 0; segfault.c

18 Something curious Run this several times: #include <stdio.h> asr.c int main(int argc, char *argv[]) int x = 1; int *p = &x; AS 2016 printf( &x: %p; p: %p; &p: %p\n, &x, p, &p); return 0; troscoe@cixous:~$ gcc -Wall asr.c troscoe@cixous:~$./a.out &x: 0x7fff33b06314; p: 0x7fff33b06314; &p: 0x7fff33b06318 troscoe@cixous:~$./a.out &x: 0x7fffee820f34; p: 0x7fffee820f34; &p: 0x7fffee820f38 troscoe@cixous:~$./a.out &x: 0x7fff230d8b64; p: 0x7fff230d8b64; &p: 0x7fff230d8b68

19 Address Space Layout Randomization Linux randomizes: Base of stack Shared library locations Security feature We ll see some overflow attacks later Makes debugging tougher Kernel virtual memory User stack shared libraries Run-time heap Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused 0xffffffffffffffff 0x AS

20 Summary Every variable in C has: Name: what is it called? Address: where in memory is it? Type: how to interpret the value Value: what is stored at the address C pointers are variables that contain the addresses of other variables. Java has no pointers (only object references), C# has them only in unsafe code // px is a ptr to an integer int *px; // x is an integer int x; // px gets the address of x // or px points to x px = &x; // x gets the contents of // whatever x points to x = *px; 20

21 4.3: Box-and-arrow diagrams Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe 21

22 Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; boxarrow.c printf("&x: %p; x: %d\n", &x, x); printf("&arr[0]: %p; arr[0]: %d\n", &arr[0], arr[0]); printf("&arr[2]: %p; arr[2]: %d\n", &arr[2], arr[2]); printf("&p: %p; p: %p; *p: %d\n", &p, p, *p); return 0; address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 3 0x7fffbe90f868 arr[2] 4 0x7fffbe90f858 p 0x7fffbe90f864 22

23 Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; boxarrow.c printf("&x: %p; x: %d\n", &x, x); printf("&arr[0]: %p; arr[0]: %d\n", &arr[0], arr[0]); printf("&arr[2]: %p; arr[2]: %d\n", &arr[2], arr[2]); printf("&p: %p; p: %p; *p: %d\n", &p, p, *p); return 0; address name value 0x7fffbe90f868 arr[2] 4 0x7fffbe90f864 arr[1] 3 0x7fffbe90f860 arr[0] 2 0x7fffbe90f858 p 0x7fffbe90f864 0x7fffbe90f854 x 1 23 main() stack frame

24 Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; boxarrow.c printf("&x: %p; x: %d\n", &x, x); printf("&arr[0]: %p; arr[0]: %d\n", &arr[0], arr[0]); printf("&arr[2]: %p; arr[2]: %d\n", &arr[2], arr[2]); printf("&p: %p; p: %p; *p: %d\n", &p, p, *p); return 0; address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 3 0x7fffbe90f868 arr[2] 4 0x7fffbe90f858 p 0x7fffbe90f864 24

25 Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; int **dp = &p; boxarrow2.c *(*dp) += 1; p += 1; *(*dp) += 1; return 0; address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 4 0x7fffbe90f868 arr[2] 4 0x7fffbe90f848 dp 0x7fffbe90f858 0x7fffbe90f858 p 0x7fffbe90f864 25

26 Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; int **dp = &p; boxarrow2.c *(*dp) += 1; p += 1; *(*dp) += 1; return 0; address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 4 0x7fffbe90f868 arr[2] 4 0x7fffbe90f848 dp 0x7fffbe90f858 0x7fffbe90f858 p 0x7fffbe90f868 26

27 Box and arrow diagrams #include <stdio.h> int main(int argc, char **argv) int x = 1; int arr[3] = 2, 3, 4; int *p = &arr[1]; int **dp = &p; *(*dp) += 1; p += 1; *(*dp) += 1; return 0; boxarrow2.c Pointers are typed: int *int_ptr; char *char_ptr; int **ptr_ptr; Pointer arithmetic follows pointer type address name value 0x7fffbe90f854 x 1 0x7fffbe90f860 arr[0] 2 0x7fffbe90f864 arr[1] 4 0x7fffbe90f848 0x7fffbe90f868 arr[2] 5 dp 0x7fffbe90f858 0x7fffbe90f858 p 0x7fffbe90f868 27

28 4.4: Pointer arithmetic Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe 28

29 Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) return 0; pointerarithmetic.c int_ptr: 0x7fff19be2aa0; *int_ptr: 1 29

30 Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) return 0; pointerarithmetic.c int_ptr: 0x7fff19be2aa0; *int_ptr: 1 30

31 Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) return 0; pointerarithmetic.c int_ptr: 0x7fff19be2aa0; *int_ptr: 1 int_ptr: 0x7fff19be2aa4; *int_ptr: 2 31

32 Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) char_ptr return 0; pointerarithmetic.c int_ptr: 0x7fff19be2aa0; *int_ptr: 1 int_ptr: 0x7fff19be2aa4; *int_ptr: 2 int_ptr: 0x7fff19be2abc; *int_ptr:

33 Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) char_ptr return 0; pointerarithmetic.c char_ptr: 0x7fff19be2aa0; *char_ptr: 1 33

34 Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) char_ptr return 0; pointerarithmetic.c char_ptr: 0x7fff19be2aa0; *char_ptr: 1 char_ptr: 0x7fff19be2aa1; *char_ptr: 0 char_ptr: 0x7fff19be2aa3; *char_ptr: 0 34

35 Pointer arithmetic #include <stdio.h> int main(int argc, char **argv) int arr[3] = 1, 2, 3; int *int_ptr = &arr[0]; char *char_ptr = (char *) int_ptr; // more "type casting" later printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr += 1; printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); int_ptr -= 2; // uh oh printf("int_ptr: %p; *int_ptr: %d\n", int_ptr, *int_ptr); printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 1; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); char_ptr += 2; printf("char_ptr: %p; *char_ptr: %d\n", char_ptr, *char_ptr); arr[2] arr[1] arr[0] char_ptr int_ptr Stack (x86, 64-bit, little endian) char_ptr return 0; pointerarithmetic.c char_ptr: 0x7fff19be2aa0; *char_ptr: 1 char_ptr: 0x7fff19be2aa1; *char_ptr: 0 char_ptr: 0x7fff19be2aa3; *char_ptr: 0 35

36 How much memory does a value take up? Depends on machine and compiler! Use: sizeof(type) or sizeof(value) Evaluates at compile time to size in bytes e.g. int nr = 1919; int size = sizeof(nr); 36

37 Summary No checks on where pointers point to! You can add an integer n to a pointer p. p now points to n objects further on Or back, in n is negative New value of p (not *p!) depends on type of *p sizeof() This looks a bit like array indexing 37

38 4.5: Arrays and pointers Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe 38

39 Arrays and pointers An array name in an expression is treated as a pointer to the first element of the array int a[10]; assert(a == &(a[0])); int get_1(int i) int *p = a; return p[i]; void set_1(int i, int v) int *p = a; p[i] = v; int get_2(int i) int *p = a; return *(p+i); void set_2(int i, int v) int *p = a; *(p+i) = v; int get_3(int i) int *p = a+i; return *p; void set_3(int i, int v) int *p = a+i; *p = v; AS

40 except when 1. The array s address is taken with & int a[10]; assert( &a == a ); 2. The array is a string literal initializer 3. The array is an operand of sizeof() int a[10]; assert( sizeof(a) == 10*sizeof(int)); assert( sizeof(&a[0]) == sizeof(int *)); 40

41 In fact A[i] is always rewritten *(A+i) in the compiler int a[10]; assert(a == &(a[0])); assert(a[5] == 5[a]); int get_2(int i) int *p = a; return i[p]; void set_2(int i, int v) int *p = a; i[p] = v; AS

42 An array name as a function parameter is a pointer The following are all precisely equivalent: int arrfun( int *myarray ) int arrfun( int myarray[] ) int arrfun( int myarray[42] ) Functions are also converted to pointers like this! 42

43 and can be called in any of these ways: int some_int; int *some_ptr; int some_array[10]; arrfun( &some_int ); arrfun( some_ptr ); arrfun( some_array ); arrfun( &some_array[i] ); all of which turn into pointers. 43

44 You can t rename an array Compile-time error: int array1[42], array2[42]; main(int argc, char *argv[]) array1 = array2; return 0; But this is OK (it s a pointer): void arrfun(int array[]) array = array2; 44

45 4.6: Passing by reference Computer Architecture and Systems Programming , Herbstsemester 2016 Timothy Roscoe 45

46 Pass-by-value C passes function arguments by value Callee gets a copy of the argument If callee modifies an argument, caller s copy isn t modified #include <stdio.h> void swap(int a, int b) int tmp = a; a = b; b = tmp; int main(int argc, char *argv[]) int a = 42, b = -7; swap(a, b); printf("a: %d, b: %d\n", a, b); return 0; brokenswap.c 46

47 Pass-by-reference You can use pointers to pass by reference Callee still receives a copy of the argument But it s now a pointer Pointer s value points to the variable in scope of the caller Callee can therefore modify a variable in the scope of the caller #include <stdio.h> void swap(int *a, int *b) int tmp = *a; *a = *b; *b = tmp; int main(int argc, char *argv[]) int a = 42, b = -7; swap(&a, &b); printf("a: %d, b: %d\n", a, b); return 0; swap.c 47

48 A simple strcpy() char *strcpy(char *dest, char *src) char *r = dest; while(*dest++ = *src++); return r; strcpy.c Lots going on here! Strings are arrays of characters terminated by null bytes Assigment is an expression, not a statement Non-zero values evaluate to true, zero evaluates to false Post-increment operators bind more tightly than pointer dereference A semicolon is statement terminator, not a separator This is also a dangerous function: use strncpy() instead. 48

49 C Pointer Declarations int *p int *p[13] int *(p[13]) int **p int (*p)[13] int *f() int (*f)() int (*(*f())[13])() p is a pointer to int p is an array[13] of pointer to int p is an array[13] of pointer to int p is a pointer to a pointer to an int p is a pointer to an array[13] of int f is a function returning a pointer to int f is a pointer to a function returning int f is a function returning ptr to an array[13] of pointers to functions returning int int (*(*x[3])())[5] x is an array[3] of pointers to functions returning pointers to array[5] of ints 49

CMPSC 311- Introduction to Systems Programming Module: Arrays and Pointers

CMPSC 311- Introduction to Systems Programming Module: Arrays and Pointers CMPSC 311- Introduction to Systems Programming Module: Arrays and Pointers Professor Patrick McDaniel Fall 2014 Course notes Exam moved to Oct 1, 2014 Assignment #3 introduction section This Thur (9/25)

More information

Pointers, Pointers, Pointers

Pointers, Pointers, Pointers Pointers, Pointers, Pointers CSE 333 Autumn 2018 Instructor: Hal Perkins Teaching Assistants: Tarkan Al-Kazily Renshu Gu Trais McGaha Harshita Neti Thai Pham Forrest Timour Soumya Vasisht Yifan Xu Administriia

More information

CSE 333 Lecture 3 - arrays, memory, pointers

CSE 333 Lecture 3 - arrays, memory, pointers CSE 333 Lecture 3 - arrays, memory, pointers Steve Gribble Department of Computer Science & Engineering University of Washington Administrivia HW 0.5 (a 4-question survey) - out today, due Monday HW 1.0

More information

Pointers, Pointers, Pointers

Pointers, Pointers, Pointers Pointers, Pointers, Pointers CSE 333 Summer 2018 Instructor: Hal Perkins Teaching Assistants: Renshu Gu William Kim Soumya Vasisht Administrivia v Exercise 2 out today and due Monday morning v Exercise

More information

CSE 333 Lecture 2 - arrays, memory, pointers

CSE 333 Lecture 2 - arrays, memory, pointers CSE 333 Lecture 2 - arrays, memory, pointers Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia 1 ex0 was due 30 minutes ago! Solution posted after class -

More information

An Experience Like No Other. Stack Discipline Aug. 30, 2006

An Experience Like No Other. Stack Discipline Aug. 30, 2006 15-410 An Experience Like No Other Discipline Aug. 30, 2006 Bruce Maggs Dave Eckhardt Slides originally stolen from 15-213 15-410, F 06 Synchronization Registration If you're here but not registered, please

More information

Stack Discipline Jan. 19, 2018

Stack Discipline Jan. 19, 2018 15-410 An Experience Like No Other Discipline Jan. 19, 2018 Dave Eckhardt Brian Railing Slides originally stolen from 15-213 1 15-410, S 18 Synchronization Registration The wait list will probably be done

More information

Intermediate Programming, Spring 2017*

Intermediate 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 information

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36 Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar

More information

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to

More information

Systems I. Machine-Level Programming V: Procedures

Systems I. Machine-Level Programming V: Procedures Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp

More information

CS 61c: Great Ideas in Computer Architecture

CS 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 information

Memory and Arrays. CSE 333 Spring Instructor: Justin Hsia

Memory and Arrays. CSE 333 Spring Instructor: Justin Hsia Memory and Arrays CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon Huang

More information

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017 Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

More information

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in

More information

Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers in C. Alan L. Cox Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including

More information

SYSC 2006 C Winter 2012

SYSC 2006 C Winter 2012 SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization

More information

Lecture 04 Introduction to pointers

Lecture 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 information

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1) Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and

More information

Understanding Pointers

Understanding Pointers Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer

More information

CS107 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 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 information

Systems Programming and Computer Architecture ( )

Systems Programming and Computer Architecture ( ) Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 AS 2016 Dynamic Memory Allocation 1 5: Dynamic memory

More information

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc CS61C L4 C Pointers (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 C Strings, Arrays, & Malloc Albert Chae Instructor 2008-06-26 Review: C Strings A string in C is just an array

More information

C - Func(on, Pointer, Array. Zhaoguo Wang

C - Func(on, Pointer, Array. Zhaoguo Wang C - Func(on, Pointer, Array Zhaoguo Wang A gigantic main loop https://github.com/qemu/qemu/blob/master/vl.c Function Readability Reusability Function int add(int a, int b) { int r = a + b; return r; name

More information

ntroduction to C CS 2022: ntroduction to C nstructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 1 ntroduction to C CS 2022, Fall 2011, Lecture 1 History of C Writing code in

More information

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1 Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1 Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic

More information

Kurt Schmidt. October 30, 2018

Kurt 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 information

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers Instructor: Justin Hsia 6/20/2012 Summer 2012 Lecture #3 1 Review of Last Lecture C Basics Variables, Functions, Flow Control,

More information

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 { Memory A bit is a binary digit, either 0 or 1. A byte is eight bits, and can thus represent 256 unique values, such as 00000000 and 10010110. Computer scientists often think in terms of hexadecimal, rather

More information

A brief introduction to C programming for Java programmers

A brief introduction to C programming for Java programmers A brief introduction to C programming for Java programmers Sven Gestegård Robertz September 2017 There are many similarities between Java and C. The syntax in Java is basically

More information

Memory, Arrays & Pointers

Memory, 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 information

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 5 C Structs & Memory Mangement 1/27/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L05 C Structs (1) C String Standard Functions

More information

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Programming. Pointers, Multi-dimensional Arrays and Memory Management Programming Pointers, Multi-dimensional Arrays and Memory Management Summary } Computer Memory } Pointers } Declaration, assignment, arithmetic and operators } Casting and printing pointers } Relationship

More information

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014. Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example

More information

CS C Primer. Tyler Szepesi. January 16, 2013

CS C Primer. Tyler Szepesi. January 16, 2013 January 16, 2013 Topics 1 Why C? 2 Data Types 3 Memory 4 Files 5 Endianness 6 Resources Why C? C is exteremely flexible and gives control to the programmer Allows users to break rigid rules, which are

More information

CS 11 C track: lecture 5

CS 11 C track: lecture 5 CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap Pointers (from last week) Address: location where data stored

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia CS61C L05 C Structures, Memory Management (1) 2005-01-28 The

More information

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016 CS 31: Intro to Systems Pointers and Memory Martin Gagne Swarthmore College February 16, 2016 So we declared a pointer How do we make it point to something? 1. Assign it the address of an existing variable

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet High-performance computing and programming Intro to C on Unix/Linux IT Uppsala universitet What is C? An old imperative language that remains rooted close to the hardware C is relatively small and easy

More information

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Structs & Memory Management 9/5/2007 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Structs (1) C String Standard Functions

More information

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. 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 information

Lecture 2: C Programm

Lecture 2: C Programm 0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. DECLARING POINTERS POINTERS A pointer represents both

More information

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables Graded assignment 0 will be handed out in section Assignment 1 Not that bad Check your work (run it through the compiler) Factorial Program Prints out ENTERING, LEAVING, and other pointers unsigned char

More information

Processes. Johan Montelius KTH

Processes. Johan Montelius KTH Processes Johan Montelius KTH 2017 1 / 47 A process What is a process?... a computation a program i.e. a sequence of operations a set of data structures a set of registers means to interact with other

More information

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018 CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make

More information

A process. the stack

A process. the stack A process Processes Johan Montelius What is a process?... a computation KTH 2017 a program i.e. a sequence of operations a set of data structures a set of registers means to interact with other processes

More information

System Software Assignment 1 Runtime Support for Procedures

System Software Assignment 1 Runtime Support for Procedures System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such

More information

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 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 information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Winter 2009 Lecture 7 Introduction to C: The C-Level of Abstraction CSE 303 Winter 2009, Lecture 7 1 Welcome to C Compared to Java, in rough

More information

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***

More information

ECE 15B COMPUTER ORGANIZATION

ECE 15B COMPUTER ORGANIZATION ECE 15B COMPUTER ORGANIZATION Lecture 13 Strings, Lists & Stacks Announcements HW #3 Due next Friday, May 15 at 5:00 PM in HFH Project #2 Due May 29 at 5:00 PM Project #3 Assigned next Thursday, May 19

More information

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and

More information

EM108 Software Development for Engineers

EM108 Software Development for Engineers EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and

More information

CSE 333 Lecture 2 Memory

CSE 333 Lecture 2 Memory CSE 333 Lecture 2 Memory John Zahorjan Department of Computer Science & Engineering University of Washington Today s goals - some terminology - review of memory resources - reserving memory - type checking

More information

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1 Q: Which of the assignment

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Pointers Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 1

More information

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib. 1 The previous lecture Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations Pointer basics 1 Common library functions [Appendix of K+R]

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013 Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example

More information

CS61, Fall 2012 Section 2 Notes

CS61, Fall 2012 Section 2 Notes CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors

More information

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2 CMPT 125 Arrays Arrays and pointers Loops and performance Array comparison Strings John Edgar 2 Python a sequence of data access elements with [index] index from [0] to [len-1] dynamic length heterogeneous

More information

CPSC 213, Winter 2016, Term 2 Final Exam Solution Date: April 19, 2017; Instructor: Mike Feeley and Alan Wagner

CPSC 213, Winter 2016, Term 2 Final Exam Solution Date: April 19, 2017; Instructor: Mike Feeley and Alan Wagner CPSC 213, Winter 2016, Term 2 Final Exam Solution Date: April 19, 2017; Instructor: Mike Feeley and Alan Wagner 1 (4 marks) Variables and Memory. Consider the following code running on 32-bit, big-endian

More information

Dynamic Allocation in C

Dynamic Allocation in C Dynamic Allocation in C C Pointers and Arrays 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically,

More information

Dynamic Memory Allocation. Zhaoguo Wang

Dynamic Memory Allocation. Zhaoguo Wang Dynamic Memory Allocation Zhaoguo Wang Why dynamic memory allocation? Do not know the size until the program runs (at runtime). #define MAXN 15213 int array[maxn]; int main(void) { int i, n; scanf("%d",

More information

CSE 333 Lecture 4 - malloc, free, struct, typedef

CSE 333 Lecture 4 - malloc, free, struct, typedef CSE 333 Lecture 4 - malloc, free, struct, typedef Administrivia HW1 is due on Friday, January 25th, 11:15am - see course overview web page for the late policy Be sure to check out the course discussion

More information

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( ) Systems Group Department of Computer Science ETH Zürich Tutorial 1: Introduction to C Computer Architecture and Systems Programming (252-0061-00) Herbstsemester 2012 Goal Quick introduction to C Enough

More information

Language comparison. C has pointers. Java has references. C++ has pointers and references

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to. Refresher When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to. i.e. char *ptr1 = malloc(1); ptr1 + 1; // adds 1 to pointer

More information

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor

More information

ELEC 377 C Programming Tutorial. ELEC Operating Systems

ELEC 377 C Programming Tutorial. ELEC Operating Systems ELE 377 Programming Tutorial Outline! Short Introduction! History & Memory Model of! ommon Errors I have seen over the years! Work through a linked list example on the board! - uses everything I talk about

More information

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming Jagannath Institute of Management Sciences Lajpat Nagar BCA II Sem C Programming UNIT I Pointers: Introduction to Pointers, Pointer Notation,Decalaration and Initialization, Accessing variable through

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Puntatori Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 5, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction to C October

More information

CS 107 Lecture 5: Arrays. and Pointers in C. Monday, January 22, Stanford University. Computer Science Department

CS 107 Lecture 5: Arrays. and Pointers in C. Monday, January 22, Stanford University. Computer Science Department CS 107 Address Value Lecture 5: Arrays 8 and Pointers in C 0x128 3 0x120 Monday, January 22, 2018 9 0x118 Computer Systems Winter 2018-4 Stanford University 0x110 Computer Science Department 2 Reading:

More information

CSCI-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 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 information

In Java we have the keyword null, which is the value of an uninitialized reference type

In Java we have the keyword null, which is the value of an uninitialized reference type + More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

More information

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.

More information

Lecture 07 Debugging Programs with GDB

Lecture 07 Debugging Programs with GDB Lecture 07 Debugging Programs with GDB In this lecture What is debugging Most Common Type of errors Process of debugging Examples Further readings Exercises What is Debugging Debugging is the process of

More information

FUNCTIONS POINTERS. Pointers. Functions

FUNCTIONS POINTERS. Pointers. Functions Functions Pointers FUNCTIONS C allows a block of code to be separated from the rest of the program and named. These blocks of code or modules are called functions. Functions can be passed information thru

More information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

Pointers. Pointers. Pointers (cont) CS 217

Pointers. Pointers. Pointers (cont) CS 217 Pointers CS 217 Pointers Variables whose values are the addresses of variables Operations address of (reference) & indirection (dereference) * arithmetic +, - Declaration mimics use char *p; *p is a char,

More information

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C Lecture 1 C primer What we will cover A crash course in the basics of C You should read the K&R C book for lots more details Various details will be exemplified later in the course Outline Overview comparison

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

Lecture 3: C Programm

Lecture 3: C Programm 0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory

More information

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

So far, system calls have had easy syntax. Integer, character string, and structure arguments. Pointers Page 1 So far, system calls have had easy syntax Wednesday, September 30, 2015 10:45 AM Integer, character string, and structure arguments. But this is not always true. Today, we begin to explore

More information

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver CS 61C: Great Ideas in Computer Architecture C Pointers Instructors: Vladimir Stojanovic & Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/sp16 1 Agenda Pointers Arrays in C 2 Address vs. Value Consider

More information

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal Decimal Representation Binary Representation Can interpret decimal number 4705 as: 4 10 3 + 7 10 2 + 0 10 1 + 5 10 0 The base or radix is 10 Digits 0 9 Place values: 1000 100 10 1 10 3 10 2 10 1 10 0 Write

More information

Decimal Representation

Decimal Representation Decimal Representation Can interpret decimal number 4705 as: 4 10 3 + 7 10 2 + 0 10 1 + 5 10 0 The base or radix is 10 Digits 0 9 Place values: 1000 100 10 1 10 3 10 2 10 1 10 0 Write number as 4705 10

More information

Lab 1: Introduction to C Programming

Lab 1: Introduction to C Programming CS342 Computer Security Handout # 2 Prof. Lyn Turbak September 13, 2010 Wellesley College Lab 1: Introduction to C Programming Reading: Hacking, 0x210 0x240 Overview Later in the course, we will study

More information

Pointers. Pointer References

Pointers. Pointer References Pointers Pointers are variables whose values are the addresses of other variables Basic operations address of (reference) indirection (dereference) Suppose x and y are integers, p is a pointer to an integer:

More information

Lecture 03 Bits, Bytes and Data Types

Lecture 03 Bits, Bytes and Data Types Lecture 03 Bits, Bytes and Data Types Computer Languages A computer language is a language that is used to communicate with a machine. Like all languages, computer languages have syntax (form) and semantics

More information

Exercise Session 2 Systems Programming and Computer Architecture

Exercise Session 2 Systems Programming and Computer Architecture Systems Group Department of Computer Science ETH Zürich Exercise Session 2 Systems Programming and Computer Architecture Herbstsemester 216 Agenda Linux vs. Windows Working with SVN Exercise 1: bitcount()

More information

CSE 333. Lecture 4 - malloc, free, struct, typedef. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CSE 333. Lecture 4 - malloc, free, struct, typedef. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington CSE 333 Lecture 4 - malloc, free, struct, typedef Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington Administrivia 1 New exercise out today, due Friday morning

More information

Exercise Session 3 Systems Programming and Computer Architecture

Exercise Session 3 Systems Programming and Computer Architecture Systems Group Department of Computer Science ETH Zürich Exercise Session 3 Systems Programming and Computer Architecture Herbstsemester 2016 Agenda Review of Exercise 2 More on C-Programming Outlook to

More information

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019 Hacking in C Pointers Radboud University, Nijmegen, The Netherlands Spring 2019 Allocation of multiple variables Consider the program main(){ char x; int i; short s; char y;... } What will the layout of

More information

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016 Pointer Arithmetic and Lexical Scoping CS449 Spring 2016 Review Pitfall 1 from previous lecture void foo(char *s) { s = "World"; int main() { char *str = "Hello"; foo(str); printf("%s\n", str); return

More information

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables) Memory Allocation Memory What is memory? Storage for variables, data, code etc. How is memory organized? Text (Code) Data (Constants) BSS (Global and static variables) Text Data BSS Heap Stack (Local variables)

More information

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5 Data Types and Computer Storage Arrays and Pointers K&R, chapter 5 Fundamental Data Types Most fundamental types are numeric (integer): - char - signed or unsigned - short int - signed or unsigned - int

More information