C++ Data Structures & Memory Management: Pointers, Allocation, Linked Lists, Study notes of Electrical and Electronics Engineering

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

Pre 2010

Uploaded on 03/19/2009

koofers-user-arb-1
koofers-user-arb-1 🇺🇸

10 documents

1 / 46

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

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