Presentation. Programming
Submitted by
Saad Lodhi
Nabiha Rana
Dua Safdar
Muqaddas Hanif
Submitted to Mam Faiza
Pointer
A pointer is a variable in programming
languages, particularly C and C++, that stores
the memory address of another variable.
Pointers are a powerful feature of these
languages because they provide direct
access to memory and allow for efficient
manipulation of data structures and dynamic
memory allocation.
Functions of pointer
1.Direct Memory Access: Pointers enable direct
manipulation of memory, which can improve
performance for certain operations by
eliminating the need to copy data
1.Dynamic Memory Allocation: Pointers
are essential for allocating memory
dynamically using functions like
malloc and free in C, and new and
delete in C++.
Functions of pointer
Data Structures: Pointers are used to
implement complex data structures
such as linked lists, trees, and graphs.
3Function Arguments: Pointers can be
used to pass large amounts of data to
functions efficiently by passing the
address of the data rather than the data
itself.
3
Functions of pointer
Array Management: Pointers provide efficient
ways to iterate through arrays and manipulate
array elements.
Pointer Arithmetic: Allows for navigation
through memory blocks, which is useful
for various algorithms and operations.
Types of pointer
Null Pointer: A pointer that is initialized
to NULL or 0. It does not point to any
valid memory location.int *ptr = NULL;
Void Pointer (Generic
Pointer): A special type of
pointer that can point to any
data type. It is often used
for generic functions.void
*ptr;
Dangling Pointer: A pointer that points
to a memory location that has been
freed or deallocated. Using a dangling
pointer can lead to undefined
behavior.int *ptr = (int
*)malloc(sizeof(int));
free(ptr); // ptr is now a dangling pointer
Wild Pointer: An uninitialized pointer
that can contain any arbitrary address.
Using wild pointers can result in
unpredictable behavior.int *ptr; // ptr is a
wild pointer
1
2
3
4
Usage and deceleration
Pointer Declarationint *ptr; // Declares a
pointer to an integer
char *ptr; // Declares a pointer to a
character
•Pointer Initializationint x = 10;
•int *ptr = &x; // ptr now holds the
address of x
•Dereferencing a PointerDereferencing a
pointer means accessing the value stored
at the memory address the pointer is
pointing to.int x = 10;
•int *ptr = &x;
•printf(“%d”, *ptr); // Outputs 10
Pointer ArithmeticPointer arithmetic
involves operations like addition and
subtraction on pointers, typically used for
iterating through arrays.int arr[] = {10, 20,
30, 40, 50};
int *ptr = arr;
printf(“%d”, *(ptr + 2)); // Outputs 30
Conclusion
Pointers are a fundamental aspect of C and C++
programming, providing powerful capabilities for
memory management, data structure manipulation, and
efficient function calling. However, they require careful
handling due to the potential for errors and undefined
behavior. Understanding pointers and their various
types and functions is essential for mastering low-level
programming and system design.
Reference
Reference is a value that enables a program
to indirectly access a particular piece of data
stored in memory. Rather than containing
the actual data itself, a reference contains
information about the location of the data,
which can be an object, variable, array, or
function.
Characteristics of reference
•Indirect Access: References allow
access to data indirectly by storing the
memory address where the data is
located.
Characteristics of reference
•Abstraction: References abstract away the direct
manipulation of memory addresses, making code
safer and easier to read.
•Immutable Binding: In many languages, once
a reference is set to point to a particular data
object, it cannot be changed to point to a
different object.
Reference types of programming language
Java:In Java, all non-primitive types (objects,
arrays) are reference types.String str = “Hello”;
int[] numbers = {1, 2, 3};
•C#:C# uses reference types for classes,
arrays, delegates, and more.Person person
= new Person();
•int[] numbers = {1, 2, 3};
C++:C++ supports both references and
pointers, but references are generally safer and
used for parameter passing and return
values.int x = 10;
int &ref = x; // ref is a reference to x
Example of reference usage
Example of Reference UsageJava Examplepublic class ReferenceExample {
public static void main(String[] args) {
Person person1 = new Person(“Alice”);
Person person2 = person1; // person2 is a reference to the same Person object as person1
person2.setName(“Bob”);
System.out.println(person1.getName()); // Outputs “Bob”
}
}
class Person {
private String name;
public Person(String name) {
Continue….. ..
This.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
In this example:person1 and person2 both
reference the same Person object.Changing the
name via person2 also reflects when accessing the
name via person1, demonstrating shared access to
the object.
Conclusion
ConclusionReferences are a
fundamental concept in programming
that enable efficient, indirect access to
data. They are widely used in object-
oriented programming, parameter
passing, and the implementation of
dynamic data structures.
Understanding how references work
and how to use them effectively is
crucial for developing robust and
efficient software.
Memory allocation
Definition
Memory allocation in programming refers
to the process of reserving a portion of
computer memory for the storage of data
during the execution of a program.
Types of memory allocation
1 SMA
2DMA
Static memory allocation
Definition
•Static memory allocation is a method of
allocating memory at compile time before
the associated program is executed. The
memory allocated using static memory
allocation is fixed and cannot be changed
during the runtime of the program. This
type of allocation is typically used for
global variables, static variables, and
constants.
Advantages of static memory allocation
•Predictability:Since memory is allocated
at compile time, the program’s memory
requirements are known beforehand,
making it easier to manage resources.
•No Fragmentation:Static memory allocation
does not suffer from fragmentation issues,
which are common in dynamic memory
allocation.
•Runtime Efficiency:There is no need
for runtime memory allocation and
deallocation, reducing the overhead
and potential runtime errors.
•Simplicity:Static allocation is simpler to
implement and understand, reducing the
complexity of memory management.
Characteristics of static memory
Compile-Time Allocation:Memory is
allocated during the compilation of the
program.The size and type of memory
allocation are known beforehand, making
it predictable.
•Fixed Size:The size of the allocated memory
block is fixed and cannot be altered at
runtime.This means the programmer must
know the required memory size in advance.
•Efficiency:Because the memory is allocated at
compile time, there is no runtime overhead for
memory allocation and deallocation.This can lead
to more efficient memory usage for certain types
of applications.
Example
•Example of Static Memory AllocationGlobal
VariablesIn C and C++, global variables are
allocated statically.#include <stdio.h>
•
•int globalVar = 10; // Statically allocated global
variable
•
•void display() {
• printf(“Global Variable: %d\n”, globalVar);
•}
•
•int main() {
• display();
• return 0;
•}
Conclusion
•ConclusionStatic memory allocation is a
fundamental concept in programming where
memory is allocated at compile time. It offers
predictability, efficiency, and simplicity, making it
suitable for scenarios where memory requirements
are well-defined and constant. However, its lack of
flexibility and potential for memory waste limit its
applicability in dynamic and complex applications.
Understanding static memory allocation is crucial
for efficient resource management and optimal
program performance in various programming
contexts.
Dynamic memory allocation
Definition
Dynamic memory allocation is a
type of memory allocation which
provides memory during the
time of allocation
Dynamic memory allocation refers to the process of
allocating memory at runtime as opposed to compile time.
This allows programs to request memory as needed and
release it when it is no longer required, enabling efficient
use of memory for varying data sizes and structures.
Dynamic memory allocation is crucial for implementing
flexible data structures like linked lists, trees, and more.
Types of dynamic memory allocation
There are four types of dynamic memory allocation
Malloc Function
Header file for DMA
(Stdlib. h)
Definition:
Melloc function create a single block of give size .
During the time of allocation .
Syntax:
Pre= {data type} malloc (size of)
Example:
Ptr= (intr*) malloc (n*size of(intr)}
Calloc…. …
Definition
•Create bunch of blocks of given size during the time
of allocation
Syntax:
•Ptr= (data type*)collac(sizeof)
Example:
Ptr= (intr*) calloc (n, sizeof{intr})
Realloc… …
Definition:
•Realloc function recreates already
created blocks of given size.
Syntax:
•Ptr: (data type) realloc (old ptr new sizeof)
Example:
Ptr(data type*) realloc (ptr,n*sizeof(intr)}
Free()… …
Definition
Free () function deletes the allocated memory .
•During the time of allocation.
Syntax
Free (pointer)
Example:
Free(ptr)