Lec12PointersDynamicAlloc_2per.pdf

Lec12PointersDynamicAlloc_2per.pdf - The University Of...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 12 Savitch Ch. 10 Pointers Addresses of Variables Dynamic Memory Allocation EECS 402 EECS 402 Andrew M Morgan 2 Introduction To Pointers A pointer in C++ holds the value of a memory address A pointer's type is said to be a pointer to whatever type should be in the memory address it is pointing to Just saying that a variable is a pointer is not enough information! Generic syntax for declaring a pointer: dataType *pointerVarName; Specific examples int *iPtr; //Declares a pointer called "iPtr" that will //point to a memory location holding an //integer value float *fPtr; //Declares a pointer called "fPtr" that will //contain an address, which is an address of //a memory location containing a float value
Image of page 1

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

View Full Document Right Arrow Icon
2 EECS 402 EECS 402 Andrew M Morgan 3 The Operator & There is an operator, & When this symbol is used as an operator on a variable it has a different meaning than the other two uses we have discussed It is unrelated to the "logical and", which is && It is unrelated to the use of & in regards to reference parameters The operator & is usually called the "address of" operator It returns the memory address that the variable it operates on is stored at in memory Since the result is an address, it can be assigned to a pointer EECS 402 EECS 402 Andrew M Morgan 4 Using The "Address Of" Operator 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1000 i iPtr 6 int i = 6; //Declares an int, stored //in memory somewhere //In this example, it is //stored at address 1000 int *iPtr; //Declares a pointer. The //contents of this variable //will point to an integer //value. The pointer itself //must be stored in memory, and //in this example, is stored at //memory location 1004 iPtr = &i; //Sets the iPtr variable to //contain the address of the //variable i in memory
Image of page 2
3 EECS 402 EECS 402 Andrew M Morgan 5 The Operator * Like the &, the * operator has another meaning as well The * operator is usually referred to as the "dereference operator" The * operator operates on a pointer value The meaning is "Use the value that this pointer points to, rather than the value contained in the pointer itself" If a pointer is of type "int *" and the dereference operator operated on the pointer, the result is a value of type "int" Dereferenced pointers can be used as L-values or R-values When used as an L-value (on the left side of an assignment), the pointer is unaffected, but the memory that it points to is changed When a pointer that is pointing to memory you are not allowed to access is dereferenced, the result is a program crash via a "segmentation fault" EECS 402 EECS 402 Andrew M Morgan 6 Using The Dereference Operator 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1000 i iPtr 6 int i = 6; //Declares integer called i int *iPtr; //Declares a pointer to an int iPtr = &i; //Sets the iPtr variable to //contain the "address of" the //variable i in memory cout << "i: " << i << endl; cout << "i: " << *iPtr << endl; *iPtr = 4; //Changes the memory being //pointed to by iPtr to contain //the value 4 cout << "i: " << i << endl; cout << "i: " << *iPtr << endl; 4 i: 6 i: 6 i: 4 i: 4
Image of page 3

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

View Full Document Right Arrow Icon
4 EECS 402 EECS 402 Andrew M Morgan 7 Arrays And Pointers
Image of page 4
Image of page 5
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