{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec09-pointers2

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

This preview shows pages 1–13. 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 801 802 803 804 805 806 807 808 809 810 p Pictorial View ? Operator new returns the address of the reserved space, which is then assigned to p .

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

View Full Document
12 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 10 800 801 802 803 804 805 806 807 808 809 810 p
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern