06-pointers-post3up

06-pointers-post3up - Pointers and memory management...

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

View Full Document Right Arrow Icon
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 Winter 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 Winter 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 Winter 2009 06: Pointers and memory management 3
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 Winter 2009 06: Pointers and memory management 4 0 0 3 4 ? ? ... 572 576 580 584 588 592 address contents p1 572 identifer location (return addr) 0 0 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 CS 136 Winter 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 Winter 2009 06: Pointers and memory management 6
Background image of page 2
We can assign a useful value to a pointer with the “address-of” operator . int i = 1, j = 2, * p; p = &i; 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 Winter 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; } CS 136 Winter 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 Next: * pti = 241; CS 136 Winter 2009 06: Pointers and memory management 9
Background image of page 3

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

View Full DocumentRight Arrow Icon
* pti = 241; 241 136 392 400 ... 392 396 400 404 ... address contents j 392 identifer location k 396 pti 400 ptpti 404 Next: * ptpti = &k; CS 136 Winter 2009 06: Pointers and memory management 10 * ptpti = &k; 241 136 396 400 ...
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

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

Page1 / 14

06-pointers-post3up - Pointers and memory management...

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

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