06-pointers-post

06-pointers-post - Pointers and memory management Readings:...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Pointers and memory management Readings: Readings from CP:AMA, as detailed in the document available on the Web site. Structures in C Syntax and semantics of pointers Memory allocation and deallocation Lists in C CS 136 Fall 2009 06: Pointers and memory management 1 Structures in C struct posn { int x; int y; }; Each declared variable of type struct posn consists of two consecutive words of memory. For local variables, these are in the stack frame associated with a function call. For global variables, they are in some fixed location, available for the duration of the program. CS 136 Fall 2009 06: Pointers and memory management 2 struct posn normalize( struct posn p1, struct posn p2) { struct posn ans; ans.x = p1.x - p2.x; ans.y = p1.y - p2.y; return ans; } float dist( struct posn p1, struct posn p2) { struct posn d; d = normalize(p1, p2); return sqrt(d.x * d.x + d.y * d.y); } CS 136 Fall 2009 06: Pointers and memory management 3 int main() { struct posn p1 = {0,0}; struct posn p2 = {3,4}; float ans; ans = dist(p1, p2); printf("%f\n", ans); return 0; } The next slide shows the situation in memory before line 2 of dist is executed. CS 136 Fall 2009 06: Pointers and memory management 4 3 4 ? ? ... 572 576 580 584 588 592 address contents p1 572 identifer location (return addr) 3 4 stack Frame For main 596 600 604 608 612 ? 616 stack Frame For dist p2 580 d 588 p1 600 identifer location p2 608 ans 616 (return to OS) 620 CS 136 Fall 2009 06: Pointers and memory management 5 Pointers in C A pointer is a memory address. We can declare a variable of type pointer to an int . int * p; One word is allocated to hold the value of the pointer p . The contents of that word will be interpreted as a memory address whose contents are an integer. p points to that integer. p is of type int * . CS 136 Fall 2009 06: Pointers and memory management 6 We can assign a useful value to a pointer with the address-of operator & . int i = 1, j = 2, * p; p = &i; In the declaration above, the * together with the int forms the type int * . * can also be used as an operator in expressions and statements. Used this way, * acts like the inverse of & . The expression * p acts like the name of the integer to which p points. We can use it as an lvalue or as an rvalue. * p = j + 1; j = * p + 2; CS 136 Fall 2009 06: Pointers and memory management 7 int main( void ) { int j = 2666, k = 136; int * pti = &j; int ** ptpti = &pti; * pti = 241; * ptpti = &k; j = ** ptpti; ** ptpti = 251; j = * pti; return 0; } CS 136 Fall 2009 06: Pointers and memory management 8 After initialization of j , k , pti , ptpti : 2666 136 392 400 ... 392 396 400 404 ... address contents j 392 identifer location k 396 pti 400 ptpti 404 It will be useful at this point to make a table of what we know....
View Full Document

This note was uploaded on 10/30/2011 for the course CS 136 taught by Professor Becker during the Spring '08 term at Waterloo.

Page1 / 49

06-pointers-post - Pointers and memory management Readings:...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online