0% found this document useful (0 votes)
5 views55 pages

2 Pointers Dynamic Memory Allocation Array Structure

The document covers the concepts of dynamic memory allocation, pointers, arrays, and structures, explaining their significance in programming, particularly in C and C++. It discusses the advantages of pointers, memory management techniques like garbage collection and reference counting, and the differences in memory allocation between stack and heap. Additionally, it provides examples of array operations in Python and algorithms for finding values in arrays.

Uploaded by

francehaha9
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)
5 views55 pages

2 Pointers Dynamic Memory Allocation Array Structure

The document covers the concepts of dynamic memory allocation, pointers, arrays, and structures, explaining their significance in programming, particularly in C and C++. It discusses the advantages of pointers, memory management techniques like garbage collection and reference counting, and the differences in memory allocation between stack and heap. Additionally, it provides examples of array operations in Python and algorithms for finding values in arrays.

Uploaded by

francehaha9
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

Pointers

OBJECTIVES
• Explain the concept of dynamic memory allocation,
arrays, structures and pointers.
• Demonstrate the use of pointers, dynamic memory
allocation, arrays and structures.
• Write a code using the pointers, dynamic memory
allocation, arrays and structures.
Pointer
Pointer is used to points the address of the value
stored anywhere in the computer memory. To obtain
the value stored at the location is known as
dereferencing the pointer.
Why Doesn't Python Support Pointers
Low-level programming languages like C or C++
frequently use pointers to directly handle memory.
They enable effective memory management and
low-level data manipulation.

The low-level complexities of memory


administration are abstracted away in Python, a
high-level language.
Why Doesn't Python Support Pointers
Everything is an object in Python, and objects are
saved in memory.

Pointers tend to create complexity in the code,


where Python mainly focuses on usability rather
than speed. As a result, Python doesn't support
pointer.
• Immutable vs. mutable objects
• Python variables/names
Objects in Python
In Python, everything is an object, even class,
functions, variables, etc. Each object contains at
least three pieces of data.

• Reference count
• Type
• Value
Pointer improves performance for
Pointer Details
Pointer arithmetic: There are four arithmetic
operators that can be used in pointers: ++, --, +, -

Array of pointers: You can define arrays to hold a


number of pointers.

Pointer to pointer: C allows you to have pointer on a


pointer and so on.
Pointer Details
Passing pointers to functions in C: Passing an
argument by reference or by address enable the
passed argument to be changed in the calling function
by the called function.

Return pointer from functions in C: C allows a function


to return a pointer to the local variable, static variable
and dynamically allocated memory as well.
VALID POINTER DECLARATION
int * ip ; // pointer to an integer
double * dp ; // pointer to a double
float * fp ; // pointer to a float
char * ch // pointer to character
Pointer Details
#include <stdio.h>

int main()
{
int myAge = 43; // An int variable
int* ptr = &myAge; // A pointer variable, with the
name ptr, that stores the address of myAge
Pointer Details
// Output the value of myAge (43)
printf("%d\n", myAge);

// Output the memory address of myAge


(0x7ffe5367e044)
printf("%p\n", &myAge);
Pointer Details
// Output the memory address of myAge with the
pointer (0x7ffe5367e044)
printf("%p\n", ptr);

return 0;
}
ADVANTAGES OF POINTER
• Pointer can handle array more efficiently.
• They process data faster
• Reduce storage space
• They give alternate way to access array elements
• Pointers avoid confusion by compiler due to
variables with same name.
DYNAMIC MEMORY
ALLOCATION
Memory Management
Memory management is very important for software
developers to work efficiently with any programming
language.
Garbage Collection
Garbage collection is a process in which the interpreter
frees up the memory when not in use to make it
available for other objects.
Reference Counting
Reference counting works by counting the number of
times an object is referenced by other objects in the
system.

Example:
x = 10
Reference Counting
Verify:
x = 10
y=x

if id(x) == id(y):
print("x and y refer to the same object")
two parts of memory ALLOCATION
• stack memory
• heap memory
STACK MEMORY
• The allocation happens on contiguous blocks of
memory.
• It is the memory that is only needed inside a
particular function or method call.
STACK MEMORY
• The allocation happens on contiguous blocks of
memory.
• It is the memory that is only needed inside a
particular function or method call.
HEAP MEMORY
• The memory is allocated during the execution of
instructions written by programmers.
• It is called heap because it is a pile of memory space
available to programmers to allocated and de-
allocate.
Common Data Structures Utilizing Dynamic Memory Allocation:
• Arrays
• Linked Lists
• Trees
• Graphs
Benefits of Dynamic Memory Allocation in Data Structures
Dynamic memory allocation is especially useful when
working with data structures that have varying sizes
or require dynamic resizing.
Dynamic Memory Allocation and Data Structures
• Efficient Memory Utilization
• Flexibility
• Reduced Memory Waste
Best Practices for Dynamic Memory Allocation
• Error Handling
• Memory Leakage Prevention
• Proper Resource Deallocation
ARRAY
ARRAY
• An array is a data structure used to store multiple
elements.
• Arrays are used by many algorithms.
• Is a collection of items of the same variable type that
are stored at contiguous memory locations
ARRAY DATA STRUCTURE
Array Representation
Applications of Array
Arrays are used in a wide variety of applications,
including:
• Storing data for processing
• Implementing data structures such as stacks and
queues
• Representing data in tables and matrices
• Creating dynamic data structures such as linked lists
and trees
Creating Array in Python
The syntax for creating an array in Python is −
Example
my_array = [7, 12, 9, 4, 11]
print( my_array[0] )

Output: ?
Example
Output
Basic Operations on Python Arrays
• Traverse − Print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index
or by the value.
• Update − Updates an element at the given index.
Accessing Array Element
Example

from array import *


array1 = array('i', [10,20,30,40,50])
print (array1[0])
print (array1[2])

Output: ?
Insertion Operation
In insertion operation, we insert one or more data
elements into an array.
Example
from array import *
array1 = array('i', [10,20,30,40,50])
[Link](1,60)
for x in array1:
print(x)

Output: ?
Deletion Operation
Deletion refers to removing an existing element from
the array and re-organizing all elements of an array.
Example
from array import *
array1 = array('i', [10,20,30,40,50])
[Link](40)
for x in array1:
print(x)

Output: ?
Search Operation
You can perform a search operation on an array to find
an array element based on its value or its index.

Example
from array import *
array1 = array('i', [10,20,30,40,50])
print ([Link](40))

Output: ?
Update Operation
Update operation refers to updating an existing
element from the array at a given index.
Example
from array import *
array1 = array('i', [10,20,30,40,50])
array1[2] = 80
for x in array1:
print(x)

Output: ?
Algorithm: Find The Lowest Value in an Array
Array: [7, 12, 9, 4, 11]

Create the Algorithm


Create the Algorithm
Option 1:
1. Create a variable 'minVal' and set it equal to the
first value of the array.
2. Go through every element in the array.
3. If the current element has a lower value than
'minVal', update 'minVal' to this value.
4. After looking at all the elements in the array, the
'minVal' variable now contains the lowest value.
Create the Algorithm
Option 2:
Variable 'minVal' = array[0]
For each element in the array
If current element < minVal
minVal = current element
Implementation

Algorithm Implementation
Implementation
my_array = [7, 12, 9, 4, 11]
minVal = my_array[0] # Step 1

for i in my_array: # Step 2


if i < minVal: # Step 3
minVal = i

print('Lowest value: ',minVal) # Step 4


PRACTICE ACTIVITY
Create a program to find the
largest number in an array.
PRACTICE ACTIVITY - ANSWER
import array as arr
a = [Link]('i', [10,5,15,4,6,20,9])
print (a)
largest = a[0]
for i in range(1, len(a)):
if a[i]>largest:
largest=a[i]
print ("Largest number:", largest)

Sample Output:
array('i', [10, 5, 15, 4, 6, 20, 9])
Largest number: 20
Create program to store all
even numbers from an array
in another array.
PRACTICE ACTIVITY - ANSWER
import array as arr
a = [Link]('i', [10,5,15,4,6,20,9])
print (a)
b = [Link]('i')
for i in range(len(a)):
if a[i]%2 == 0:
[Link](a[i])
print ("Even numbers:", b)

Sample Output:
array('i', [10, 5, 15, 4, 6, 20, 9])
Even numbers: array('i', [10, 4, 6, 20])
REFERENCES:
[Link]
[Link]
[Link]
[Link]
Thank you!
Course Instructor:
Engr. Laila A. Contreras
Engr. Anthony G. Hernandez

You might also like