0% found this document useful (0 votes)
1 views

C++ Pointers

The document provides an overview of C++ pointers, explaining their definition, usage, and related operations such as dereferencing and pointer arithmetic. It also covers dynamic memory allocation using 'new' and 'delete' operators, emphasizing the importance of managing memory manually to avoid leaks. Additionally, it highlights how array names function as pointers and the significance of pointer expressions in accessing array elements.

Uploaded by

jgr102005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

C++ Pointers

The document provides an overview of C++ pointers, explaining their definition, usage, and related operations such as dereferencing and pointer arithmetic. It also covers dynamic memory allocation using 'new' and 'delete' operators, emphasizing the importance of managing memory manually to avoid leaks. Additionally, it highlights how array names function as pointers and the significance of pointer expressions in accessing array elements.

Uploaded by

jgr102005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

C++ Pointers

Definition

 Pointers store address of variables or a memory location.


 // General syntax
datatype *var_name;
 // An example pointer "ptr" that holds
// address of an integer variable or holds
// address of a memory whose value(s) can
// be accessed as integer values through "ptr"
int *ptr;
 To access the address of a variable to a pointer, we use the unary
operator & (ampersand) that returns the address of that variable.
 For example, &x gives us the address of variable x.
Example

// The output of this program can be different


// in different runs. Note that the program
// prints the address of a variable and a variable
// can be assigned different addresses in different runs
int main()
{
int x;
// Prints address of x
cout << &x;
return 0;
}
One more operator is the unary * (Asterisk) which is used for two things
:
 To declare a pointer variable: When a pointer variable is declared in
C++, there must be a * before its name.
 To access the value stored in the address we use the unary operator
(*) that returns the value of the variable located at the address
specified by its operand. This is also called Dereferencing.
 Check the example in the next slide.
int main()
{
int Var = 10;
// A pointer variable that holds address of var.
int *ptr = &Var;
// This line prints value at address stored in ptr.
// Value stored is value of variable "var"
cout << "Value of Var = "<< *ptr << endl;
// The output of this line may be different in different
// runs even on same machine.
cout << "Address of Var = " << ptr << endl;
// We can also use ptr as lvalue (Left hand
// side of assignment)
*ptr = 20; // Value at address is now 20
// This prints 20
cout << "After doing *ptr = 20, *ptr is "<< *ptr << endl;
}
Pointer Expressions and Pointer
Arithmetic
A limited set of arithmetic operations can be performed on pointers. A
pointer may be:

 incremented ( ++ )
 decremented ( -- )
 an integer may be added to a pointer ( + or += )
 an integer may be subtracted from a pointer ( – or -= )
Pointer arithmetic is meaningless unless performed on an array.
Note : Pointers contain addresses. Adding two addresses makes no
sense, because there is no idea what it would point to. Subtracting two
addresses lets you compute the offset between these two addresses.
int main()
{
int v[3] = { 10, 100, 200 };
// Declare pointer variable
int* ptr;
// Assign the address of v[0] to ptr
ptr = v;
for (int i = 0; i < 3; i++)
{
cout << "Value of *ptr = " << *ptr << endl;
cout << "Value of ptr = " << ptr << endl;
// Increment pointer ptr by 1
ptr++;
}
}
Array Name as Pointers

 An array name acts like a pointer constant. The value of this pointer
constant is the address of the first element.
 For example, if we have an array named val
then val and &val[0] can be used interchangeably.
new and delete Operators For Dynamic
Memory

 Dynamic memory allocation in C/C++ refers to performing memory


allocation manually by a programmer. Dynamically allocated memory
is allocated on Heap, and non-static and local variables get memory
allocated on Stack.
 For normal variables like “int a”, “char str[10]”, etc, memory is
automatically allocated and deallocated. For dynamically allocated
memory like “int *p = new int[10]”, it is the programmer’s
responsibility to deallocate memory when no longer needed. If the
programmer doesn’t deallocate memory, it causes a memory leak
(memory is not deallocated until the program terminates).
 The new operator denotes a request for memory allocation on the
Free Store. If sufficient memory is available, a new operator initializes
the memory and returns the address of the newly allocated and
initialized memory to the pointer variable.
 Syntax to use new operator
pointer-variable = new data-type;
int *p = NULL;
p = new int;
The statement:
int *p = new int[10]
Dynamically allocates memory for 10 integers continuously of type int
and returns a pointer to the first element of the sequence, which is
assigned top (a pointer).
p[0] refers to the first element, p[1] refers to the second element, and so
on.
 If enough memory is not available in the heap to allocate, the new
request indicates failure by throwing an exception of type
std::bad_alloc, unless “nothrow” is used with the new operator, in
which case it returns a NULL pointer. Therefore, it may be a good idea
to check for the pointer variable produced by the new before using its
program.
int *p = new(nothrow) int;
if (!p)
{
cout << "Memory allocation failed\n";
}
delete operator

Since it is the programmer’s responsibility to deallocate dynamically


allocated memory, programmers are provided delete operator in C++
language:
delete p;
To free the dynamically allocated array pointed by pointer-variable, use
the following form of delete:
delete[] p;

You might also like