Download C++ Data Structures & Memory Management: Pointers, Allocation, Linked Lists and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!
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 :
- Symbolic name
- Address of memory location
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:
- Memory addresses can be used as names for variables.
- Pointers “point” to a variable by telling where the variable is located in memory.
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:
- p2 = p1 // changes the location that p2 is pointing to
- *p2 = *p3; // changes the value at the location that // p2 is pointing to
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?
- Use ‘&’ operator
- Use dynamic allocation
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:
- Memory for dynamic variable is allocated from heap.
- Size of heap varies.
- If new is called and there is no more memory available in the heap, the program will end! Remark: Older compiler may return a pointer value NULL when no more new dynamic variable can be created. Null is a special constant pointer value that can be assigned to a pointer variable of any type.
- When no longer used, memory for dynamic variables should be freed and returned to the heap using delete operator. Once deleted, the dynamic variable will be destroyed; all pointers pointing to this variable are now undefined! The delete Operator: Syntax: delete ptrVar;
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:
- Recall scope describes the region of a program over which a particular variable is “alive” and visible.
- Going out of scope: All memory used for local variables will be de-allocated.
- The rules for dynamically allocated variables are different.
- Dynamically allocated variables must be assigned to locations in a special area of memory called the heap.
- A dynamically allocated variable remains allocated until it is explicitly de-allocated (deleted) via the delete operator.
- Pointers may go out of scope and be deleted, but the things to which they point at will not if they were dynamically allocated.
- “Memory Leak” – Term which describes the case when a programmer allocates one or more variables dynamically, but never explicitly deletes them.
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:
- In C++, a user-defined type name can be created and assigned to a type definition. Once defined, this user-defined type name can then be used to declare variables of the named type.
- We use the keyword typedef to define a new type name. Syntax: typedef known DataType yourTypeName ;
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:
- It is usually less confusing and also more informative to use typedef to define your pwn types.
- It can also be used to simplify variables declaration.
Recall that in using arrays:
- Array has fixed size
- Data must be shifted during insertions and deletions
Possible Solution: Linked data structures
- Linked list is able to grow in size while the program is running.
- Does not require the shifting of items during insertions and deletions
A linked list is a collection of objects being “linked” together with pointers such that
- Each element (node) of the linked list has some data and a pointer to the location in memory where the next element of the list can be found.
- The last element of the list will have a pointer value NULL.
- A linked list can be visualized as items (nodes), drawn as boxes, connected to other items by arrows (pointers).
Data Structure: Node structure:
Linked list:
Empty List: head = NULL.
Remarks:
- The boxes in the previous drawing represent the nodes of a linked list.
- Nodes contain the data item(s) and a pointer that can point to another node of the same type.
- The pointers point to the entire node, not an individual item that might be in the node.
- The arrows in the drawing represent pointers.
- The box labeled head is not a node, but a pointer variable that points to a node.
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:
- p is a pointer variable so *p is the node that p points to.
- The parentheses are necessary because the dot operator “.” has higher precedence than the dereference operator “*”.
Using “ − >” Operator:
- The arrow operator -> combines the actions of the dereferencing operator * and the dot operator to specify a member of a struct or object pointed to by a pointer.
- The statement p −> item = 268; It is the same as: (*p).item = 268;
Item next