chap11 - Chapter 11 Chapter Pointers and dynamic memory Mechanisms for developing flexible list representations Key Concepts Key L-values R-values

chap11 - Chapter 11 Chapter Pointers and dynamic memory...

This preview shows page 1 - 13 out of 60 pages.

Chapter 11 Chapter 11 Pointers and dynamic memory Mechanisms for developing flexible list representations
Image of page 1
Key Concepts: Key Concepts: L-values R-values pointer types null address dereference operator * indirect members selector -> address operator & pointer assignment indirect assignment pointers as parameters pointers to pointers constant pointers pointers to constants arrays and pointers pointers to function dynamic objects free store operators new and delete exception handling dangling pointers memory leak destructors copy constructor member assignment this pointer qualifier const
Image of page 2
Usefulness The acquired memories of most programs can not be determined in compiling phase of program, but at run time Necessary for dynamic objects Objects whose memory is acquired during program execution as the result of a specific program request Dynamic objects can survive the execution of the function in which they are acquired Dynamic objects enable variable-sized lists, for example, the vector class
Image of page 3
Memory Memory and its address address
Image of page 4
Memory Memory and its address address Main board Graphics adapter Sound blaster Hard disk driver Main memory CPU
Image of page 5
Memory Memory and its address address System Bus CPU Memory Output device Input device
Image of page 6
Memory Memory and its address address System bus transfers the data information, address information and control information Data bus transfers the data information Address bus transfers the address information Control bus transfers the control information 32 bits address bus has an ability of addressing memory space of 4GB From 0x00000000 to 0xFFFFFFFF
Image of page 7
Memory Memory and its address address 0x00000001 0x00000000 0x00000002 0x00000003 0x7FFFFFFF 0xFFFFFFFD 0xFFFFFFFE 0xFFFFFFFF NULL address . . . . . . . . . Main memory
Image of page 8
L-value and R-value L-value expressions Represent objects that can be evaluated and modified R-value expressions Represent objects that can only be evaluated Consider int a; vector<int> b(3); int c[3]; a = 1; // a: lvalue c[0] = 2*a + b[0]; // c[0], a, b[0]: lvalues Observation Not all lvalues are the names of objects
Image of page 9
Pointer basics Pointer Object whose value represents the location of another object In C++ there are pointer types for each type of object Pointers to int objects Pointers to char objects Pointers to RectangleShape objects Even pointers to pointers Pointers to pointers to int objects Normally a pointer is an unsigned integer , its size is variable with the operation system and compiler A 32 bits pointer occupies itself 4 bytes of memory A 16 bits pointer occupies itself 2 bytes of memory
Image of page 10
Syntax TypeName * PointerName = & ObjectName Indicates to take the address of the object Indicates to take the address of the object Indicates pointer object Indicates pointer object Examples of initialized pointers int i = 1; char c = 'y'; int *ptr = &i; // ptr is a pointer to int i char *t = &c; // t is a pointer to a char c Examples of uninitialized pointers int *iPtr; // iPtr is a pointer to an int char *s ; // s is a pointer to a char Rational *rPtr; // rPtr is a pointer to a // Rational
Image of page 11
Indirection Operator * An asterisk has two uses with regard to pointers
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 60 pages?

  • Winter '03
  • Pro.Lei
  • C++ Programming, Pointer, main memory, Garbage collection

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

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes