{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–13. 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
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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);
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
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right 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.
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.
Image of page 9

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

View Full Document Right 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 .
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 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 .
Image of page 11

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

View Full Document Right Arrow Icon
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
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern