






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of data objects, pointers, and dynamic memory allocation in c++. It covers the use of pointers to access data objects, dynamic allocation and de-allocation, and the implementation of linked lists for flexible data structures. Examples and code snippets are provided to illustrate these concepts.
Typology: Study notes
1 / 46
This page cannot be seen from the preview
Don't miss anything!







































L10-Pointers & Linked Structures
Read: Chpt. 9 & 13
Recall that every data object is stored in some location in memory.
Example:
void func() { int x; int y = 10; … }
Hence, every variable in a program has two attributes: Location: Address of memory Content of the location: Value
Accessing a Data Object :
A New Referencing Mechanism : One may use address of memory location (pointer variable) to access a data object without an assigned symbolic name for the variable.
Pointer: The memory address/location of a variable.
Pointer Variables: A variable that stores the actual memory address of a data object.
Remarks:
Declaring a Pointer Variable: Use a “*” following the type (or preceding the identifier which is to be the pointer variable):
Syntax: type_name pointer_name; type_name pointer_name; type_name * pointer_name;
Assignment Operator: The assignment operator = is used to assign the value of one pointer to another.
Example: int v, *p1, *p2;
v = 168; p1 = &v; // p1 is a pointer pointing to variable v *p1 = 268; // the variable pointed to by p1 holds 268
cout << “v = ” << v << endl;
p2 = p1; // v, p1, p2 reference the same variable *p2 = 368; cout << “v = ” << v << endl;
Output: v = 268 v = 368
Remarks:
NULL Pointer Value: Null is a special constant pointer value that can be assigned to a pointer variable of any type.
Example: int* p = NULL; double* q = NULL;
Q: How do we make pointers point to things?
Using &-Operator: int *p, v; v = 168; p = &v;
p? v
p v
Dynamic Allocation of Variables: To create new unnamed instances of variables during run-time.
Example: void dynamicallyAllocate() { double* p = new double; p = 10.7; int val = new int(7); cout << “Values: ” << *p << ‘ ’ << *val << endl; }
heap
Allocating & De-allocating Dynamic variables: Basic Memory Management:
Example: int *p, *q; p = new int(168); q = p; delete p; cout << *q; // What happens?
The pointers p and q are dangling pointers.
Scope considerations:
Example:
double* calculate(double a, double b) { // Return a pointer to a dynamically allocated // variable holding the product a*b. // Caller responsible for deleting the // dynamically allocated memory!
double* p = new double(a*b); return p; }
void aCaller() { double* p = calculate(3.7, -2.6); cout << “The answer is: ” << *p << endl; delete p; }
Defining Your Own Types:
Example: typedef int exam; typedef double average; … exam numOfExam; average examAve;
Note that numOfExam is of exam type and hence also is of int type. Similarly, examAve is of average type and hence also is of double type.
Remarks:
Recall that in using arrays:
Possible Solution: Linked data structures
A linked list is a collection of objects being “linked” together with pointers such that
Data Structure: Node structure:
Linked list:
Empty List: head = NULL.
Remarks:
NULL
Item next
head
typedef Node* NodePtr;
NodePtr head;
Simple Insertion and Deletion in Linked List:
Allocating a Node Dynamically: ListNode *p; p = new ListNode;
p
Accessing Node Member: (*p).item = 268;
Remarks:
Using “ − >” Operator:
Item next