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

Lecture 13

Uploaded by

bachir20062006
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)
7 views

Lecture 13

Uploaded by

bachir20062006
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/ 26

Data Structure &

Algorithms 1
CHAPTER 6: DYNAMIC MEMORY
ALLOCATION (INTRODUCTION)
Sep – Dec 2023
Introduction

Motivation:

We often don't know how much space we will need to


store things at "compile time" → int array[Max-size]

Dynamic memory allocation is the allocation of


memory at "run time“
Introduction

Differences between Static & Dynamic Memory Allocation:

 Dynamically allocated memory is kept on the memory


heap (also known as the free store)
 Dynamically allocated memory can't have a "name" it
must be referred to
 Declarations are used to statically allocate memory,
the new operator is used to dynamically allocate memory
Introduction to Pointer

What is a pointer?

a memory address!
a variable that store memory address!
Introduction to Pointer

A pointer a pointing to the


memory address associated
with a variable b,
i.e., a contains the memory
address 1008 of the variable b.
Introduction to Pointer

 Pointer variables count

 Normally, variable contains specific value (direct reference) 7


count
 Pointers contain address of variable that has specific value
(indirect reference) 7
countPtr

 Pointer declarations  Pointer initialization


 * indicates variable is pointer  Initialized to 0, NULL, or address
int *myPtr;  0 or NULL points to nothing
declares pointer to int, pointer of type int *
 Multiple pointers require multiple asterisks
int *myPtr1, *myPtr2;
Pointer Operators
& (address operator)
 Returns memory address of its operand
 Example:
int y = 5;
int *yPtr;
yPtr = &y; // yPtr gets address of y
 yPtr “points to” y
y yptr y
5 500000 600000 600000 5
yPtr

address of y
is value of
yptr
Pointer Operators
* (indirection/dereferencing operator)

 Utilized with the asterisk symbol (*)


 Yields a synonym for the object pointed to by its operand
 Example: *yPtr returns y (as yPtr points to y)
 The dereferenced pointer is an lvalue
 Example: *yPtr = 9; //assigns 9 to y
 The operators * and & are inverses of each other
Pointer Operators
2 // Using the & and * operators.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 int main()
9 {
10 int a; // a is an integer
11 int *aPtr; // aPtr is a pointer to an integer
12
13 a = 7;
14 aPtr = &a; // aPtr assigned address of a
15
16 cout << "The address of a is " << &a
17 << "\nThe value of aPtr is " << aPtr;
18
19 cout << "\n\nThe value of a is " << a
20 << "\nThe value of *aPtr is " << *aPtr; * and & are inverses
21 of each other
22 cout << "\n\nShowing that * and & are inverses of "
23 << "each other.\n&*aPtr = " << &*aPtr
24 << "\n*&aPtr = " << *&aPtr << endl;
25
Pointer Operators
26 return 0; // indicates successful termination
27
28 } // end main

The address of a is 0012FED4


The value of aPtr is 0012FED4

The value of a is 7
The value of *aPtr is 7

Showing that * and & are inverses of each other.


&*aPtr = 0012FED4
*&aPtr = 0012FED4 * and & are inverses; same
result when both applied to
aPtr
Mystery Function: What prints out?
void mystery(int a, int& b, int* c) {
a++;
(*c)--;
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
}

int main() { a b c
int a = 4;
int b = 8;
4 8 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
void mystery(int a, int& b, int* c) {
a++;
(*c)--;
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3

int main() { a b c
int a = 4;
int b = 8;
4 8 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -3 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3

int main() { a b c
int a = 4;
int b = 8;
4 8 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -2 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3

int main() { a b c
int a = 4;
int b = 8;
4 8 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -2 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3

int main() { a b c
int a = 4;
int b = 8;
4 7 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -2 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3
-2 11 7
int main() { a b c
int a = 4;
int b = 8;
11 7 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -2 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3
-2 11 7
int main() { a b c 11 7 -3
int a = 4;
int b = 8;
11 7 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Calling Functions by Reference

 3 ways to pass arguments to function


1. Pass-by-value
2. Pass-by-reference with reference arguments
3. Pass-by-reference with pointer arguments

 return can return one value from function

 Arguments passed to function using reference arguments


 Modify original values of arguments
 More than one value “returned”
Calling Functions by Reference

 Pass-by-reference with pointer arguments


 Simulate pass-by-reference
 Use pointers and indirection operator
 Pass address of argument using & operator
 Arrays not passed with & because array name already pointer
* operator used as alias/nickname for variable inside of function
Calling Functions by Reference
2 // Cube a variable using pass-by-reference
3 // with a pointer argument.
4 #include <iostream>
5
6 using std::cout; Prototype indicates parameter
7 using std::endl; is pointer to int
8
9 void cubeByReference( int * ); // prototype
10
11 int main()
12 {
13 int number = 5;
14
15
Apply address operator & to
cout << "The original value of number is " << number;
16 pass address of number to
17 // pass address of number to cubeByReference
cubeByReference
18 cubeByReference( &number );
19
20 cout << "\nThe new value of number is " << number << endl;
21
22 return 0; // indicates successful termination
23
cubeByReference
24 } // end main modified variable
number
Calling Functions by Reference

26 // calculate cube of *nPtr; modifies variable number in main


27 void cubeByReference( int *nPtr )
28 {
29 *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr
30
cubeByReference
31 } // end function cubeByReference receives address of int
variable,
The original value of number is 5
i.e., pointer to an int
The new value of number is 125
Modify and access int
variable using indirection
operator *
Difference between pass-by-
reference with reference and pointer
 Syntax:
 In pass-by-reference with reference arguments, you use the & symbol to
specify a reference parameter,
 In pass-by-reference with pointer arguments, you use the * symbol to specify
a pointer parameter.
 Nullability:
 Pointers can have a null value, meaning they don't point to any valid
memory location.
 References must always refer to a valid object and cannot be null. This
means that passing a null pointer to a function can led to runtime errors if
the function tries to dereference the pointer.
Difference between pass-by-
reference with reference and pointer
 Pointer arithmetic:
 Pointers allow you to perform pointer arithmetic, which can be useful
in some cases, such as iterating over arrays or linked lists.
 References do not support pointer arithmetic.
 Memory management:
 Pointers can be used to manage dynamic memory allocation and
deallocation, which cannot be accomplished with references. For
example, you can use new and delete operators to dynamically
allocate and deallocate memory for a pointer,
 reference must always refer to an existing object.
Pointer Expressions & Arithmetic

Pointer arithmetic
 Increment/decrement pointer (++ or --)
 Add/subtract an integer to/from a pointer( + or += , - or -=)
 Pointers may be subtracted from each other
 Pointer arithmetic meaningless unless performed on pointer to
array
 5 element int array on a machine using 4 byte ints
 vPtr points to first element v[0], which is at location 3000
location
vPtr = 3000
3000 3004 3008 3012 3016
 vPtr += 2; sets vPtr to 3008
v[0] v[1] v[2] v[3] v[4]
vPtr points to v[ 2 ]

pointer variable vPtr


Pointer Expressions & Arithmetic

 Subtracting pointers
 Returns number of elements between two addresses
vPtr2 = v[ 2 ];
vPtr = v[ 0 ];
vPtr2 - vPtr → 2
 Pointer assignment
 Pointer can be assigned to another pointer if both of same type
 If not same type, cast operator must be used
 Exception: pointer to void (type void *)
 Generic pointer, represents any type
 No casting needed to convert pointer to void pointer
 void pointers cannot be dereferenced
Pointer Expressions & Arithmetic

 Pointer comparison
 Use equality and relational operators
 Comparisons meaningless unless pointers point to members
of same array
 Compare addresses stored in pointers
 Example: could show that one pointer points to higher
numbered element of array than other pointer
 Common use to determine whether pointer is 0 (does not
point to anything)

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