lec09-pointers2

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

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

1 CSC 1111 Introduction to Computing using C++ Pointers as Parameters Dynamic Memory Management Pointers & Arrays

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

View Full Document
2 Outlines Pointers as Parameters Dynamic Memory Management Relationship between Pointers and Arrays Dynamic Memory Management and Arrays Dynamic Memory Management and Objects Destructors
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

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

View Full Document
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);
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

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

View Full Document
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
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

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

View Full Document
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.
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.

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

View Full Document
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 .
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

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

View Full Document
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
Ask a homework question - tutors are online