0% found this document useful (0 votes)
15 views35 pages

CHP 12 Pointer and Dynamic Values

The document covers pointers and dynamic variables in C++, emphasizing the importance of understanding memory allocation and pointer usage. It explains how to declare pointers, assign values, and utilize dynamic memory with the new and delete operators. Additionally, it highlights the advantages of dynamic arrays and pointer arithmetic in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views35 pages

CHP 12 Pointer and Dynamic Values

The document covers pointers and dynamic variables in C++, emphasizing the importance of understanding memory allocation and pointer usage. It explains how to declare pointers, assign values, and utilize dynamic memory with the new and delete operators. Additionally, it highlights the advantages of dynamic arrays and pointer arithmetic in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

SWE 2314

Pointers and Dynamic Variables

 Read pages 811-839 in Malik, Chapter


12.
Skipping Chapters
 We’re skipping from Chapter 8 to
Chapter 12, but will come back and
cover Chapters 9 and 10 in future
weeks.

C++ Programming: From Problem


Analysis to Program Design, Seventh 2
Edition
Review from Unit 10: Computer
Memory
• Computer memory is organized as a
sequence of billions of memory cells.
• Each memory cell has an address (or
location) and holds a byte-sized unit of
information.
Review from Unit 10: Where in
Memory Are Variables Stored?
• The compiler decides which memory
cells to use for the variables in your
program. These cells will probably be
different every time you compile the
program.
Review from Unit 10: Why Should
You Care?
• Most of the time you don’t care which
cells the compiler chooses, but
sometimes you do.
• Therefore, C++ gives you a way to find
out the address at which the compiler
has stored a variable….
Review from Unit 10: Address-of
Operator
• The address-of operator & returns the
address of a variable.

This address will


probably be different
every time you
recompile the program.
Review from Unit 10: Most Variables
Occupy Several Bytes
• How many memory cells
does a variable occupy?
It depends on the variable’s
data type.
• For example, on most systems
an int variable occupies four memory
cells (four bytes of memory).
• The sizeof operator tells you how
many cells the variables of a
particular data type occupy:
cout << sizeof(int) << endl;
Review from Unit 10: Most Variables
Occupy Several Bytes (cont’d.)
• On the previous slide we used the
sizeof operator to find out how many
cells the variables of a particular data
type occupy:
cout << sizeof(int) << endl;
• We can also use it to find out how many
cells a specific variable occupies:
int myNum;
cout << sizeof(myNum) << endl;
Review from Unit 10: Most Variables
Occupy Several Bytes (cont’d.)
• In an example above, we saw that num1
was stored in memory at address
3275108.

• Since num1 is an int variable, it needs


four memory cells, and actually occupies
the cells at addresses 3275108,
3275109, 3275110, and 3275111.
Review: Partial Hierarchy of Data
Types
Pointer variables are
different from other
variables because they
hold addresses, not data.
Data Types

Simple Data Structured Data


Pointers
Types Types

Integral
Floating-Point
(int, bool, Enumeration Arrays structs Classes
(double, …)
char, …)
Pointer Variables
• A pointer variable is a variable that contains a
memory address (usually the address of
another variable or of an array).
• Depending on the datatype of the thing whose
address the pointer contains, we call the
pointer a pointer-to-int, or a pointer-to-
double, and so on.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 11


Why Use Pointers?
• Experienced C++ programmers use
pointers to do many different kinds of
things. Some of these uses are very
clever and tricky.
• In this course, we’re interested in
pointers mainly because they let the user
choose the size of an array while the
program is running, rather than making
us hard-code the array’s size in our code.
(They let us set the array size at run-
time rather than at compile-time.)
Pointers Can Be Dangerous
• Because pointers let you do clever, tricky
things that you could not do without
pointers, they are one of the more
dangerous tools in the C++’s
programmer’s toolkit.
• Many programming languages do not
have pointers.
• You’ll sometimes hear the oversimplified
statement that “Java is C++ without
pointers.”
Declaring Pointer Variables
• Syntax to declare a pointer variable:

• Examples:
int *myPtr1; //Declares a pointer-to-int.
char *myPtr2; //Declares a pointer-to-
char.
• In these examples, the names of the pointers
we’re declaring are myPtr1 and myPtr2, not
*myPtr1 and *myPtr2.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 14
We’re Not Multiplying Anything
• The previous examples:
int *myPtr1; //Declares a pointer-to-int.
char *myPtr2; //Declares a pointer-to-
char.
• Note that * is also the multiplication operator, but
we’re using the symbol * here in a way that has
nothing to do with multiplication.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15


Where Do You Put the * ?
• When you declare a pointer variable, you can put
the * next to the datatype, or next to the pointer
name, or by itself between them.
– The following declarations are all legal and equivalent
to each other:
int* myPtr1; // * next to datatype.
int *myPtr1; // * next to pointer name.
int * myPtr1; // * by itself.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16


Assigning a Value to a Pointer
• Recall that the address-of operator & returns
an address. This makes it useful for assigning
values to pointer variables.
• Example:
int x=34; //Declare and initialize an int.
int *p; //Declare a pointer-to-int.
p = &x; //Now p holds x’s address.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17


How to Think About This
• The previous example:
int x=34; //Declare and initialize an int.
int *p; //Declare a pointer-to-int.
p = &x; //Now p holds x’s address.
• In general we don’t care what the actual address
is. We just care that p now “points to” x.

p x
34

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 18


Dereferencing Operator (*)
• When used as a unary operator on a pointer, *
(which is called the dereferencing operator) refers
to the object to which the pointer points.
• When you see *p in a statement other than a
declaration, read it as “the thing pointed to by p.”
• Example:
cout << *p; //Displays the value of
//the thing pointed to by p.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 19


Example: Dereferencing Operator (*)

int x=34; //Declare & initialize an int.


int *p; //Declare a pointer-to-int.
p = &x; //Now p holds x’s address.
p x
34
cout << *p << endl; //Displays 34.

*p = 68; //Assign a new value to x.


cout << x << endl; //Displays 68.
Assignment Operations using Pointer
Variables
• The value of one pointer can be assigned to
another pointer of the same type, using the usual
assignment operator =.
• When you do this, you end up with two pointers
pointing to the same variable. See example on
next slide.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 21


Example: Assignment Operations
using Pointer Variables

int x=34; //Declare & initialize an int.


int *p1; //Declare a pointer-to-int.
int *p2; //Another pointer-to-int.

p1 = &x; //Now p1 points to x.


p2 = p1; //Now p2 also points to x.
p1 x
34
p2
Dynamic Variables
• A dynamic variable is a variable whose
memory is allocated while the program is
running.
– This is in contrast to normal variables, whose
memory is allocated when the program is
compiled. Example--in the following code, the
compiler allocates memory for myNum before the
program starts running:
int myNum;

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23


Dynamic Variables (cont’d.)
• To work with dynamic variables, we use
pointers and two operators:
– The new operator lets us create dynamic
variables.
– The delete operator lets us destroy dynamic
variables when we no longer need them and wish
to free up their memory.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 24


The new Operator
• The new operator has two forms:

– intExp is any expression evaluating to a positive


integer
• new allocates memory for a variable or array
of the designated type and returns the
address of this memory, which you will almost
always want to assign to a pointer variable.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25
Example: The new Operator
int *p; //Declare a pointer-to-int.
p = new int;

 The second statement above does two things:


 It creates an unnamed int variable during
program execution somewhere in memory.
 It stores the newly created variable’s address

in the pointer p. So p now points to the


unnamed int variable.
p
The new Operator (cont’d.)
• A dynamic variable’s value cannot be accessed
directly because the variable has no name.
Rather we must use a pointer to access its
value. Extending the previous example:
int *p; //Declare a pointer-to-int.
p = new int;
*p = 34; p
34

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 27


Memory Leaks and the delete
Operator
• A so-called memory leak occurs when a piece of
previously allocated memory is no longer being used
but has not been “released” by your program.
– To avoid memory leaks, when a dynamic variable is no
longer needed, destroy it to free up its memory.
– Use the delete operator to do this.
• Syntax:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 28


Example of a Memory Leak
 In the code below, Line 23 creates a memory
leak. After it executes, we have an unnamed
variable holding the value 20 that is not
pointed to by any pointer.

To avoid the leak,


do delete q;
before Line 23.
Dynamic Arrays
• A dynamic array is an array whose memory is
allocated while the program is running.
• Example:
int *p;
p = new int[5]; p

*p = 25; stores 25 into the first array element

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 30


Arithmetic Operations on Pointer
Variables
• You can perform some arithmetic operations on
pointers:
– You can use ++ or -- to increment or decrement a
pointer variable’s value .
– You can add or subtract integer values to or from a
pointer variable.
– You can subtract one pointer variable’s value from
another pointer variable’s value.
• These are most useful when you’re using pointers
in combination with arrays. (See next slide.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 31
Arithmetic Operations on Pointer
Variables (Cont’d.)
• We can use the ++ operator to step us through
the elements in a dynamic array.
• Example:
int *p;
p = new int[5]; p

*p = 25; stores 25 into the first array element


p++; //Point to next array element.
*p = 35; stores 35 into the second array element

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 32


Pointer Arithmetic versus Ordinary
Arithmetic
• Arithmetic operators work differently on pointers
from how they work on ordinary variables.
• In previous example, note that p++ adds 4, not 1,
to the value of p.
int *p; p
p = new int[5];
*p = 25; stores 25 into the first array element
p++; //Point to next array element.
*p = 35; stores 35 into the second array element

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 33


Dynamic Arrays (cont’d.)
• As an alternative to arithmetic operations on
pointers, you can use array subscript notation
with a pointer to access the array elements.
• In the example on the previous slide, we can do:
p[0] = 25;
p[1] = 35;
– Stores 25 and 35 into the first and second array
elements, respectively. We’re treating p as if it were
the array’s name, even though it’s actually the name
of a pointer to the array.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 34
Setting an Array’s Size at Run-Time
 The big advantage of dynamic arrays is that
you can wait until run-time to decide how big
an array should be, rather than having to hard-
code the array’s size in your code.
 Example:
int size;
int *p;
cout << "How big is your array? ";
cin >> size;
p = new int[size]; //Can’t do this with
//static arrays!

You might also like