C Programming Part Three PDF
Document Details
Uploaded by AvailableHeisenberg
Boston University
Tags
Summary
This document contains lecture notes on C programming. It discusses arrays, strings, and pointer arithmetic. The lecture notes are part of a course at Boston University.
Full Transcript
C Programming PART THREE Arrays, Strings, and Pointer Arithmetic Strangely, these all go hand-in-hand! Administrative Details: Midterm Exam on Thursday! ○ Covering all of Assembly, and L15 (Basic C and functions) What C is on the exam? You will not wr...
C Programming PART THREE Arrays, Strings, and Pointer Arithmetic Strangely, these all go hand-in-hand! Administrative Details: Midterm Exam on Thursday! ○ Covering all of Assembly, and L15 (Basic C and functions) What C is on the exam? You will not write C code, but you may have to read a translation of C assembled and understand the calling convention for functions. Read Piazza for exam logistics! What we covered last time C Pointers! ○ A variable that contains an address! How do we declare a pointer to a character? char* pointer_to_c; ○ Operations: Address of (&) - gets address of variable Indirection (*) - dereferences a pointer Arrays in C This is an example of two statically declared arrays ○ We will get to dynamic allocation, next week! Must declare the size of the array ○ Can be implicit via assignment Must also declare the type Where does this array exist in memory? ○ The stack! sizeof(op) prints the size (in ○ If I declare an array as a global bytes) of either the TYPE of variable, where does that go? VARIABLE op. I.e. sizeof(int) is 4 Array Access Syntax: Subscripting Index an array via the arr[index] syntax. ○ Gets the ith value of the array Array Syntax No bounds checking! What will this print? Why? What will happen when I go out of bounds? That’s undefined behaviour … >:) Why? A picture of our arrays In memory However, this is not arr 0x50 99 guaranteed! 0x54 99 0x58 99 Sometimes out of bounds 0x5c 99 accesses will cause errors, overwrite other arr2 0x60 101 0x64 102 data! 0x68 103 Undefined behavior 0x6c 104 Diving Deeper: C Arrays are similar to pointers! The value of the array variable is equal to the address of the starting element! An expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ implicitly ○ What will *arr do? Subtle difference between array and pointer: ○ Except when using sizeof operator, & operator ○ Subtle detail: arr == &arr == &arr Picture in Memory int arr[] = {1,2,3,4}; … int* p = arr; arr 0x50 1 0x54 2 // equivalent to: 0x58 3 // int *p = &arr; 0x5c 4 … p 0x75 0x50 Arrays as Parameters These two methods sumit2 and sumit, are no different int * A is the same as int A[] The callee can do operations as if A is an array or a pointer! Under the hood the same thing happens: C is pass by value, and the value is the address of the array. NOTE: array loses size information Pointer Arithmetic When you add to a pointer, it increments by the size of the type it is pointing to. Arrays in Memory Let’s use gdb to inspect an array of characters! Let’s use array indexing! p &arr p &arr p &arr p p_arr p p_arr + 1 x /1gd p+1 Pointer Arithmetic, Array Indexing! Notice: *(a + i) == a[i] When i == 1, what is *(a+1)? a = 0x50 a + 1 = 0x54 a 0x50 1 *(0x54) = 2 0x54 2 0x58 3 0x5c 4 When i == 2, what is *(a+2)? a = 0x50 a + 2 = 0x58 *(0x58) = 3 Connection: In assembly, C Strings == Arrays we have been inspecting strings in memory… Subtle Detail: [‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘C’, ‘S’, ‘2’, ‘1’, ‘0’, ‘\0’] Printing arr_msg will result in an address on the stack Printing p_msg will result in an address in the static data section of memory Standard Library for C Strings Next Time… Malloc! (Review Structs) Linked Lists! Structs! Structs as Parameters { int: 40 int: 1999 double: 3.2 C is pass by } value… arguments: { int: 40 41 int: 1999 double: 3.2 } return addr age_up stackframe Pointers to Structs We can declare a pointer to a struct! We can dereference that pointer in order to modify the original struct. Improved Access to Struct: “right arrow selection” Why (*sp).age++? Why not *sp.age++? Instead: sp->age++ sp->age is equivalent to (*sp).age Fixing age_up(); What should our parameters to age_up be? What should our body code do? How should we call age_up? Problems with Pointers What will happen? Pointers As Return Values. What will happen? Summary: Pointers as Return Values What is a Segmentation Fault? A fault raised by the hardware when we attempt to access memory that we should not. Then, switch control to the OS and we follow our fault handling plan. ○ (in this case, kill the program). Do you remember the fault handling plan in the beta processor?