
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























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 char *strcpy(char *, const char *); Consider, e.g., the following declaration: What exactly does that mean? 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) A pointer argument p is ambiguous , and always has two regular meanings: Ambiguity Thursday, September 16, 2010 12:25 PM Docsity.com
โ Look in memory like integers โ^ Job: identify other memory. โ For an object o, &o is the address of o. โ For a pointer p, *p is the thing pointed to by p. Pointers โ p = q means "make p point where q points". โ p = &o means "make p point where o lives". โ (After this, *p is an alias for o) Basic pointer principles Basics of pointers Thursday, September 09, 2004 3:36 PM Docsity.com
int k; Diagram variables as boxes pointing nowhere: int *p=NULL; pointing to a thing: int *q=&k; Diagram variable pointers as Diagram arrays as linear arrangements of boxes: int a[5]; Diagram structures as linear arrangements of things (with possible holes) struct foo { int k; double d; } f; (int *q = &k; is shorthand for int *q; q=&k; ) Pointer diagramming Thursday, September 16, 2010 12:57 PM Docsity.com
An lvalue is something that can be on the left-hand side of = An rvalue is something that can be on the right-hand side of = Normal pointers (int *) are lvalues Array names are rvalues and not lvalues. int a[5]; // a is of type "int * const" ("a" can't be set) You can say *a=7, or a[5]=9, but not a=&q; Compare with const int *k; k=&I; // can write k=, cannot write *k= Lvalues and Rvalues Thursday, September 16, 2010 4:57 PM Docsity.com
Arrays and pointers example Thursday, September 16, 2010 5:01 PM Docsity.com
p+i=pointer, p-i=pointer, p-q=int (distance between p and q), p+q is meaningless Caveats: for p, q pointers; i,j integers: p=NULL; // #define NULL 0 *p=2; // guaranteed seg fault. One can set a pointer to anything provided that one doesn't access it: Pointer caveats Thursday, September 09, 2004 3:36 PM Docsity.com
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. Why pointer casts are important 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. More generally, 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. Pointer casts Thursday, September 09, 2004 3:36 PM Docsity.com
struct foo s; int p[4]; union { } u; What a pointer cast does is to treat the data at the pointer like a union. Unions and casts Thursday, September 09, 2004 3:36 PM Docsity.com
Global variable Arbitrary size Arbitrary lifetime malloc grants access, free revokes it. Dynamic memory allocation: allows an application to get new memory to use for its own purposes. dynamic memory allocation Thursday, September 15, 2011 2:29 PM Docsity.com
C C++ Java int *p=(int *)malloc( *sizeof(int)); int *p=new int[10]; int p[] = new int[10]; free(p); delete p; No equivalent new calls malloc! delete calls free! C versus C++ Thursday, September 16, 2010 12:48 PM Docsity.com
return m; } void foo_delete(struct foo *f) { free(f); } main() { struct foo *f = foo_create(); โฆ foo_delete(f); } Docsity.com
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. int *p=(int *)malloc(sizeof(int)); โฆ free(p); int *q=(int *)malloc(sizeof(int)); // reuse p's data! *p=5; // change *q! Typical horror story: Using free() Thursday, September 09, 2004 5:47 PM Docsity.com
basic problem: not much information is included with a pointer. ๏ท you must infer what to do with the pointer from context and documentation. ๏ท Ambiguity and pointers Ambiguity Thursday, September 09, 2004 6:16 PM Docsity.com
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. When you get a pointer p you do not know:
Ambiguity 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! */ } Ambiguity continued Thursday, September 09, 2004 6:16 PM Docsity.com