lect8_4

lect8_4 - Pointers and Dynamic Memory Allocation 1 Pointers...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Pointers and Dynamic Memory Allocation 1 Pointers and Dynamic Memory Allocation Since a pointer can be used to access a variable, a program can access and modify variables even if the variables have no identifiers to name them. The operator new can be used to create variables that have no identifiers to serve as their names. These nameless variables are accessed using pointers. For example, the following code segment declares a variable of type int and sets a pointer variable p to hold its address. int *p; p = new int; // a new int variable created. // p contains the address of this variable. *p = 20; // int variable value is set to 20 cout << *p; // outputs 20 This new, nameless variable can be referred to as *p . We can do anything with this nameless variable that we can do with any other variable of type int . For example, the following code segment reads a value of type int from the keyboard into this nameless variable, adds 8 to the value, then outputs this new value. int *p; p = new int; cin >> *p ; // suppose the user inputs 10 *p = *p + 8; cout << *p; // outputs 18 The new operator allocates memory for a new nameless variable and returns its ad- dress. We specify the type for this new variable by writing the type name after the new operator. Variables that are created using the new operator are called dynamic variables because they are created and destroyed while the program is running. We illustrate some simple operations on pointers and dynamic variables using a series of figures.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Pointers and Dynamic Memory Allocation 2 The statement int *p1, *p2; creates two pointers that can point to variables of type int . Note that the pointers p1 and p2 are uninitialized. The statement p1 = new int; creates a dynamic variable of type int . The memory address of this dynamic variable is stored in the pointer p1 . That is, pointer p1 now points to this (unnamed) dynamic variable. This variable can be accessed using the syntax *p1 . Note that the dynamic variable is uninitialized. The following statement sets the value of the dynamic variable pointed to by p1 to 50 . *p1 = 50;
Background image of page 2
Pointers and Dynamic Memory Allocation 3 The following statement sets the value of p2 to the value of p1 . p2 = p1; Now, the pointers p1 and p2 have the same value. That is, both p1 and p2 point to the same variable. The statement
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

lect8_4 - Pointers and Dynamic Memory Allocation 1 Pointers...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online