lec09-pointers2

lec09-pointers2 - CSC 1111 Introduction to Computing using...

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

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ Pointers as Parameters Dynamic Memory Management Pointers & Arrays
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Outlines Pointers as Parameters Dynamic Memory Management Relationship between Pointers and Arrays Dynamic Memory Management and Arrays Dynamic Memory Management and Objects Destructors
Background image of page 2
3 Review Exercise int x = 7, y = 11, z = 5; int *p1, *p2; p1 = p2 = &x; *p1 = 0; z = *p2; p1 = &z; p2 = &y; p1 = p2; cout << x << " " << y << " " << z << " " << *p1 << " " << *p2; p1 p2 y x z
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Pointers as Parameters Pointers are passed by value by default However, passing pointers allow us to emulate the effect of "pass by reference". void foo(int * p) { *p = 0; } // In main() int x = 3, *ptr = &x; foo(ptr); cout << x; // Print 0 We can also pass address of a variable directly as foo(&x);
Background image of page 4
5 void foo(int *p ) { *p = 0; } // In main() int x = 3, *ptr = &x; foo(ptr); cout << x; // Print 0 ptr p 3 x void foo(int *p) { *p = 0; } // In main() int x = 3, *ptr = &x; foo(ptr); cout << x; // Print 0 ptr 3 x
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 void foo(int *p) { *p = 0; } // In main() int x = 3, *ptr = &x; foo(ptr); cout << x; // Print 0 ptr 0 x void foo(int *p) { *p = 0; } // In main() int x = 3, *ptr = &x; foo(ptr); cout << x; // Print 0 ptr p 0 x
Background image of page 6
7 void swap1(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; } void swap2(int *a, int *b) { int *tmp = a; a = b; b = tmp; } int x = 5, y = 2; // Which one will swap value of x and y? swap1(&x, &y); swap2(&x, &y); a b x y tmp a b x y tmp
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Dynamic Memory Management You can explicitly request OS to Allocate space from memory for storing data Release unused memory You cannot just store/access data anywhere you like in the memory.
Background image of page 8
9 Memory allocation (Operator new ) int * p ; p = new int ; *p = 10; Memory View ? 100 101 102 103 104 105 106 107 108 109 110 800 801 802 803 804 805 806 807 808 809 810 p ? Pictorial View p , as a variable, is allocated memory space automatically. Initially, p is pointing to nowhere.
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Memory allocation (Operator new ) int * p ; p = new int ; *p = 10; Memory View ? 100 101 102 103 104 105 106 107 108 109 110 ? 800 801 802 803 804 805 806 807 808 809 810 p ? Pictorial View ? Operator new reserves a memory space that is big enough to store a value of type int .
Background image of page 10
11 Memory allocation (Operator new ) int * p ; p = new int ; *p = 10; Memory View 800 100 101 102 103 104 105 106 107 108 109 110 ? 800
Background image of page 11

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

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

This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

Page1 / 34

lec09-pointers2 - CSC 1111 Introduction to Computing using...

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

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