0% found this document useful (0 votes)
2 views30 pages

CPP Pointers

The document provides an introduction to pointers in programming, explaining the relationship between variables, memory addresses, and pointers. It covers key concepts such as pointer operators, dynamic memory allocation using 'new' and 'delete', and the importance of secure programming practices. Additionally, it discusses pointer arithmetic and relational operations involving pointers.

Uploaded by

Heng Dalux
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views30 pages

CPP Pointers

The document provides an introduction to pointers in programming, explaining the relationship between variables, memory addresses, and pointers. It covers key concepts such as pointer operators, dynamic memory allocation using 'new' and 'delete', and the importance of secure programming practices. Additionally, it discusses pointer arithmetic and relational operations involving pointers.

Uploaded by

Heng Dalux
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

INTRODUCTION TO POINTERS

Basic Concepts
VARIABLES & MEMORY ADDRESSES

All variables have an address


VARIABLES

• Variables have
• Name
• Address or location in memory
• Content

counter 123 0x000000ab


VARIABLE CONCEPTS

• Machine code does not use variable names, it uses the variable’s address
• The compiler maps the name to an address
• In a program, the name can represent the address or the content stored at that address
• Name used as an address
• counter = 5;
• Name used as the content
• balance = counter * 10;
• cout << counter << endl;
M E M O RY
M E TA P H O R
MEMORY VIEWED AS AN ARRAY

.
.
.
316

320

324

328

.
.
.
POINTER VARIABLES

• Pointers are variables that hold or store the memory addresses of other
variables or data
• An address is a location in main memory that cannot change
• C++ provides several operators that operate on pointers
POINTER OPERATORS

Working with pointers and


variable addresses
IMPORTANT OPERATOR CONCEPTS

• There are a limited number of characters on the keyboard, forcing computer


languages to reuse some characters
• Operators that have multiple meanings are said to be overloaded
• Overloaded operators whose meaning depends on where they are used are said
to be context sensitive
• As you study the pointer operators, take note of
• The symbol or characters forming each operator
• Where the operators are used
• The meaning and behavior of each operator, which is often tied to the operator’s name
POINTER OPERATORS

Operator Name Example


* Pointer Definition int* i;
* Dereference, Indirection *i = 123;
cout << *i<< endl;
& Address of int *p;
p = &i;
new New
delete Delete delete p;

-> Arrow
POINTER OPERATOR EXAMPLES

i 0x0a000010
int i;
POINTER OPERATOR EXAMPLES

i 0x0a000010
int i;
int* p;

p 0x0a000014
POINTER OPERATOR EXAMPLES

i 123 0x0a000010
int i;
int* p;

i = 123;

p 0x0a000014
POINTER OPERATOR EXAMPLES

i 123 0x0a000010
int i;
int* p;

i = 123;
p = &i;
p 0x0a000010 0x0a000014
DEREFERENCING A POINTER

The Dereference Operator


The Indirection Operator
DEREFERENCING A POINTER

int i; // restaurant
i 0x0a000010
DEREFERENCING A POINTER

int i; // restaurant
int* p; // Tom’s house
i 0x0a000010

p 0x0a000014
DEREFERENCING A POINTER

int i; // restaurant
int* p; // Tom’s house
i 123 0x0a000010
i = 123;

p 0x0a000014
DEREFERENCING A POINTER

int i; // restaurant
int* p; // Tom’s house
i 123 0x0a000010
i = 123;
p = &i;

p 0x0a000010 0x0a000014
DEREFERENCING A POINTER

int i; // restaurant
int* p; // Tom’s house
i 123 0x0a000010
i = 123;
p = &i

cout << *p << endl;


p 0x0a000010 0x0a000014
DYNAMIC MEMORY: NEW & DELETE

Allocating and Deallocating Memory


THE NEW OPERATOR

1. Allocates memory from the heap


2. Calls the constructor if the memory is being allocated for an instance of a
class
3. Returns the address of the allocated memory
ALLOCATING MEMORY

C++ JAVA

char* c = new char; Character c = new Character();


double* scores = new double[size]; double[] scores = new double[size];
Person* p = new Person; Person p = new Person();
DEALLOCATING MEMORY

C++ JAVA
delete c; // deallocates a single char
delete[] scores; // deallocates an array
delete p; // deallocates one object
POINTER OPERATIONS

Arithmetic With Pointers

Delroy A. Brinkerhoff
RELATIONAL OPERATIONS

• int* i1; • int* i1 = nullptr;


• int* i2; • int* i2 = 0;
• while (i1 == nullptr) . . .
• if (i1 == i2) . . . . • while (i2 != 0) . . . .
• if (i1 != i2) . . . .
• nullptr replaces NULL and 0
SECURE PROGRAMMING

• Important pointer facts: • Steps to minimize errors and enhance


• Pointer variables are not automatically security:
initialized • Initialize pointers: Person* p = nullptr;
• Using a null pointer causes an error: e.g., • Test the value stored in a pointer before
you can’t access a member if the pointer using it – the underlying problem
doesn’t point to an object determines the test and the action
• Using uninitialized pointers causes difficult • if (p == nullptr)
to find errors and are a security threat
• if (p != nullptr)
POINTER ARITHMETIC, PART 1

char data[] = { p1 A 0x00ffaa00


'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H' B 0x00ffaa01
}; C 0x00ffaa02
D 0x00ffaa03
char* p1 = data;
char* p2 = p1 + 4; p2 E 0x00ffaa04
F 0x00ffaa05
p2 points to 'E'
G 0x00ffaa06
H 0x00ffaa07
POINTER ARITHMETIC, PART 2

int data[] = {
1, 2, 3, 4, 5, 6, 7, 8 p1 1 0x00ffaa00
}; 2 0x00ffaa04
int* p1 = data;
3 0x00ffaa08
int* p2 = p1 + 4; 4 0x00ffaa0c
p2 5 0x00ffaa10
p2 points to 5
6 0x00ffaa14
7 0x00ffaa18
8 0x00ffaa1c
POINTER ARITHMETIC, PART 3

int data[] = {
1, 2, 3, 4, 5, 6, 7, 8 p1 1 0x00ffaa00
}; 2 0x00ffaa04
int* p1 = data;
3 0x00ffaa08
int* p2 = p1 + 4; 4 0x00ffaa0c
p2 5 0x00ffaa10
p2 – p1 is 4 6 0x00ffaa14
7 0x00ffaa18
8 0x00ffaa1c

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy