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

Similar documents
just a ((somewhat) safer) dialect.

just a ((somewhat) safer) dialect.

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

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

Memory, Arrays & Pointers

Lecture 04 Introduction to pointers

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

Lecture 8 Dynamic Memory Allocation

Lectures 5-6: Introduction to C

Arrays and Pointers in C. Alan L. Cox

CS 3305 Intro to Threads. Lecture 6

Arrays and Memory Management

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CSC 1600 Memory Layout for Unix Processes"

Fundamental of Programming (C)

Final CSE 131B Spring 2004

Kurt Schmidt. October 30, 2018

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

EL2310 Scientific Programming

Lectures 5-6: Introduction to C

PRINCIPLES OF OPERATING SYSTEMS

COMP 2355 Introduction to Systems Programming

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

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

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

ECE 15B COMPUTER ORGANIZATION

HW1 due Monday by 9:30am Assignment online, submission details to come

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Introduction to pthreads

CS61, Fall 2012 Section 2 Notes

Processes. Johan Montelius KTH

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

Memory Corruption 101 From Primitives to Exploit

A process. the stack

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Supporting Class / C++ Lecture Notes

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

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

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

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

CSE 333 Autumn 2013 Midterm

CS61C : Machine Structures

Dynamic Allocation in C

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

CS61C : Machine Structures

EL2310 Scientific Programming

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

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Princeton University Computer Science 217: Introduction to Programming Systems. Modules and Interfaces

pthreads CS449 Fall 2017

Understanding Pointers

Chapter 1 Getting Started

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Array Initialization

211: Computer Architecture Summer 2016

Threads Tuesday, September 28, :37 AM

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Class Information ANNOUCEMENTS

QUIZ. What is wrong with this code that uses default arguments?

CSE 333 Lecture 2 Memory

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

CSE 303: Concepts and Tools for Software Development

Systems Programming and Computer Architecture ( )

CS 61C: Great Ideas in Computer Architecture Introduction to C

Heap Arrays. Steven R. Bagley

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Memory Allocation. General Questions

Memory Management: The process by which memory is shared, allocated, and released. Not applicable to cache memory.

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

CS61C : Machine Structures

o Code, executable, and process o Main memory vs. virtual memory

ch = argv[i][++j]; /* why does ++j but j++ does not? */

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

Lectures 13 & 14. memory management

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

ELEC 377 C Programming Tutorial. ELEC Operating Systems

CS 231 Data Structures and Algorithms, Fall 2016

CSC C69: OPERATING SYSTEMS

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

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

G52CPP C++ Programming Lecture 9

Introduction to Programming Block Tutorial C/C++

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Dynamic memory allocation (malloc)

Data Representation and Storage. Some definitions (in C)

Transcription:

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 the C types necessary to invoke system calls. These include: pointer and pointer casts. const pointers function pointers typedefs

Pointers Page 2 E.g., consider these library functions: Wednesday, September 30, 2015 4:06 PM void *malloc(size_t size) ; int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg );

Recall basic facts about pointers (C and C++) Monday, September 15, 2014 3:51 PM For a pointers p,q and an item a: p=q means make p point where q points. p+1 is a pointer one cell above p (treating memory as an array). p-1 is a pointer to one cell below p. (ditto) *p refers to the item pointed to by p. &a is the address of an item a &*p p for a pointer p. *&a a for an object a. a[i] *(a+i) a->b (*a).b Pointers Page 3

Diagramming pointers and memory Wednesday, September 30, 2015 9:52 AM A box refers to a memory location. An unboxed item refers to a symbol that does not correspond to a memory location. Example: int a[5]; Example: int *p; p=a; Pointers Page 4

Pointers Page 5 Problems and opportunities with Pointers Monday, September 15, 2014 3:39 PM Pointers and arrays Why is there no bounds checking? Pointer casts Why do we want to be able to change the type of memory? Controlling whether to write to a pointer What is const? How are strings implemented in C? Special pointer types What is void * and what can I do with it? Pointers and ambiguity How does one really read a linux man page?

Pointer caveats Thursday, September 09, 2004 3:36 PM Caveats: for p, q pointers; i,j integers: p+i=pointer, p-i=pointer, p-q=integer (distance between p and q, if p, q are same type), p+q is meaningless One can set a pointer to anything provided that one doesn't access it: p=null; // #define NULL 0 *p=2; // guaranteed seg fault. Pointers Page 6

Pointers Page 7 Basic pointer casts Thursday, September 09, 2004 3:36 PM A pointer cast determines what kind of data a pointer refers to: If p is a pointer, (int *)p is another pointer that points to an array of ints. (struct foo *)p is another pointer that points to an array of things of type struct foo. Pointer casts are a semantic operation The pointer doesn't change. It is simply thought of in a different way. Operations on the pointer change meanings! A pointer cast Redefines pointer reference (to a potentially larger or smaller thing). Redefines pointer addition (to increment or decrement by the size of the thing).

Pointers Page 8 Caveat: alignment Wednesday, September 30, 2015 4:51 PM Pointer operations are only defined if objects are appropriately aligned. This means that the address is some multiple of a power of 2. Most processors will generate a bus error for a reference to an unaligned pointer. long x; short *p; p = (short *)((char *)x)+1); *p=2; // guaranteed bus error! // I violated alignment constraints. // p is an odd address, must be even. // ANSI Standard layout: every primitive type // is aligned on a multiple of its length.

Pointers Page 9 In general Monday, September 15, 2014 6:06 PM There are many cases in which C defines something by function. E.g., at runtime, There are no type descriptors. There is no way to determine the type of a particular memory element. Except to look at how it's used. If it's used as an int, it's probably an int. If it's used as a pointer, it's probably a pointer. These are attributes of the machine code. There is no procedure for determining whether a piece of memory p is a pointer, other than how it is used (by evaluating *p)

Pointers Page 10 Pointer casts Thursday, September 09, 2004 3:36 PM Why pointer casts are important The operating system, unlike a user program, has to deal with data whose type it does not know. Often, a block of memory can have multiple meanings. Basic strategy: treat unknown types as void, or void *, and then later, cast them to types they are. More generally, Whenever it is necessary to use a block of memory in a new way, can cast a pointer to it to the appropriate type. This is the root of how dynamic memory allocation is done. In C++, p = new x; creates a new instance of the object x In C, there is no "new", instead, p = (int *)malloc(sizeof(int)*5) creates an array of 5 ints. malloc returns a pointer of type "void *": type unknown.

Pointers Page 11 Two things about malloc Monday, September 15, 2014 6:09 PM It is smart enough to align memory elements on a boundary divisible by the size of the largest primitive type. -> you can cast it to any type + ANSI standard layout -> you can cast it to any struct type.

Uses of void * Monday, September 15, 2014 3:46 PM void * is C's main information hiding mechanism Tells you where something is. Doesn't tell you how to write or read it. Example: void * malloc (unsigned int size); More advanced example: driver information tables. Referenced as void *. Cast to appropriate types inside driver. OS doesn't know the difference. struct driver_table {... void *data;... } table[100]; Inside driver functions, e.g., write: struct device_data { char buffer[4096];... };... struct device_data * d = (struct device_data *) table[30].data; //... do things with d... Pointers Page 12

Pointers Page 13 dynamic memory allocation Thursday, September 15, 2011 2:29 PM Dynamic memory allocation: allows an application to get new memory to use for its own purposes. Global variable Arbitrary size Arbitrary lifetime malloc grants access, free revokes it.

C versus C++ Thursday, September 16, 2010 12:48 PM C C++ Java int *p=(int *)malloc(10 *sizeof(int)); int *p=new int[10]; int p[] = new int[10]; free(p); delete p; No equivalent new calls malloc! delete calls free! Pointers Page 14

Pointers Page 15 Malloc caveats Thursday, September 09, 2004 5:47 PM Malloc caveats Data returned is a pointer to storage. Data is uninitialized; contains seemingly random values (impractical to predict in advance) No constructors; you manually initialize data. Everything is done by changing type of data after malloc (by pointer casts) Why no initialization? Many data structures don't need to be initialized (Example: large hash tables) Without initialization, malloc executes in O(1) in most cases (constant time) With initialization, it executes in O(n) (n=number of bytes) Why bother to initialize if you don't always need it? Common practice: encapsulate initialization struct foo { int i; double d; } struct foo *foo_create() { struct foo *m = (struct foo *) malloc(sizeof(struct foo)); m->i=0; m->d=0;

Pointers Page 16 return m; } void foo_delete(struct foo *f) { free(f); } main() { struct foo *f = foo_create(); foo_delete(f); }

Pointers Page 17 Using free() Thursday, September 09, 2004 5:47 PM Must explicitly free() storage so it can be reused. free(p) does not change p! Subsequent allocations can reuse memory. This can cause a pointer alias and havoc can ensue. Typical horror story: int *p=(int *)malloc(sizeof(int)); free(p); int *q=(int *)malloc(sizeof(int)); // reuse p's data! *p=5; // change *q!

The big deal Thursday, September 17, 2009 5:14 PM When an OS gives your process memory to use, it does not know the types you assign to it. That information is not just unknown, but also unknowable. C is not reflective (unlike Java)! Pointers Page 18

Pointers Page 19 const Thursday, September 15, 2011 2:50 PM const If you have a pointer to a thing, you can write to it. The const declaration allows you to decide you aren't going to write to a pointer. Example void foo(const int *p) { printf("%d\n",*p); } Rules for const Non-const implicitly casts to const. E.g., int *p; const int *q; q=p; // works const does not cast to non-const. E.g., const int *p; int *q; q=p; // compiler error position of const in the declaration describes exactly what is constant and what remains mutable: const int *p; // *p is const int * const p; // p is const, *p remains mutable! const int * const p; // both are const To be nerdy about this: const int * const * const r; means that r is const *r is const **r is const

Pointers Page 20 Note the pattern: the thing immediately after the const is the thing that is constant. The point: many subroutines have const arguments that are automagically cast from non-const to const to guarantee that the subroutine won't write on the arguments. But if I want to be really antisocial, I can write: int a; int *p = &a; const int *q = p; *((int *)q) = 7; // violate the agreement!

Pointers Page 21 Why use const? Thursday, September 15, 2011 2:59 PM Const is a social contract The function that declares and argument as const promises not to touch it. By convention, a function that takes a non-const argument does touch it. It is considered sloppy to write a function that doesn't touch a non-const argument.

Pointers Page 22 Example: strcpy Thursday, September 15, 2011 3:01 PM #include <string.h> char *strcpy(char *dest, const char *src); says that: we are going to change dest we are not going to change src How this is typically implemented: char *strcpy(register char *dest, register const char *src) { char *ret = dest; while (*src) *dest++=*src++; *dest='\0'; return ret; } Explanation: register char *dest: store this in a register, not a memory location while (*src): until you get to a '\0' *dest++=*src++: copy from src to dest, then increment

Pointers Page 23 Ambiguity Thursday, September 16, 2010 12:25 PM The linux manual pages are actually rather difficult to read. The main problem is ambiguity: the declarations of functions do not fully describe their requirements Consider, e.g., the following declaration: char *strcpy(char *, const char *); What exactly does that mean? A pointer argument p is ambiguous, and always has two regular meanings: A pointer to a single thing *p A pointer to an array of things p[i] (other more strange interpretations are also possible, but uncommon) For this example, We don't know whether the things are arrays. We don't know whether the things are strings (a particular kind of array).

Pointers Page 24 Ambiguity continued Thursday, September 09, 2004 6:16 PM Ambiguity When you get a pointer p you do not know: whether it points to an array or a single thing? Equivalently, whether p[i] makes sense, or just *p? appropriate range for i for p[i] to make sense? Equivalently, what length of array it points to. Example: int *foo(int *a) { return a+3; } main() { int b[5]; int *p=foo(b); p[-3] = 2; /* ok! */ p[-5] = 4; /* not ok! */ }

Pointers Page 25 Example: man printf Thursday, September 09, 2004 6:16 PM int printf(const char *fmt, ); Q: Is fmt a pointer to a char or a pointer to a character array? A: Turns out it is an array. Q: How large is fmt? A: There's a \0 at the end. Q: what does const mean? A: agrees not to change *fmt (or, e.g., fmt[i]). printf("%s","hi there"); printf("hi there"); const char *t = "hello"; printf("%s", t); printf(t);

Pointers Page 26 String constants and pointers Thursday, September 09, 2004 6:16 PM A string constant is a pointer to pre-allocated storage "hello" is const char * not writable readable as an array "hello"[0] is 'h' terminated by a null character "hello"[5] is '\0' = 0. stored in the text segment. protected by hardware memory protection. try to write => bus error

Pointers Page 27 End of lecture on 09/25/2017 Monday, September 25, 2017 5:37 PM

Pointers Page 28 Pointer ambiguities Thursday, September 09, 2004 6:16 PM Pointer ambiguities arise In manual pages. In declarations. In header files.

Pointers Page 29 How bad can it get? Thursday, September 09, 2004 6:16 PM int **p; // can mean a pointer to nothing a pointer to a pointer to a thing. a pointer to an array of pointers to things. a pointer to a pointer to an array of things. a pointer to an array of pointers to arrays of things.

Structure of argv Thursday, September 09, 2004 6:16 PM Pointers Page 30

Declaration of main: Thursday, September 09, 2004 6:16 PM int main(int argc, char **argv, char **envp) { } Pointers Page 31

What about envp? Thursday, September 17, 2009 5:24 PM What is the structure of envp? Pointers Page 32