Docsity

Notes on Pointers and Dynamic Arrays - Programming I | EECS 168, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
Pointers & Dynamic Arrays
Read: Chpt. 9
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
Example:
int y = 10;
y:
Accessing a Data Object:
• Symbolic name
• Address of memory location
10
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Notes on Pointers and Dynamic Arrays - Programming I | EECS 168 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

Pointers & Dynamic Arrays

Read: Chpt. 9

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

Example: int y = 10;

y:

Accessing a Data Object :

  • Symbolic name
  • Address of memory location

A New Referencing Mechanism : One may use address of memory location (pointer variable) alone to access a data object. We won’t even need an assigned symbolic name for the variable.

Pointer: The memory address 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: 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 and parameters passing.

Array and Pointer Variables: In C++, an array variable is actually a (const) pointer variable pointing to the first indexed array variable.

Example: int a[10]; typedef int* IntPtr; IntPtr p;

Since a is a pointer variable that points to a[0], the following statement causes p to point to the same location as a. p = a; // This is legal!

It is illegal to assign a pointer value to an array variable. a = p; // This is illegal!

Pointer variable p can also be used as if it were an array variable.

Example: p = a; for (index = 0; index < 10; index++) cout << p[index] << " ";

Recall that in using an array in C++, the array size must be determined when the program is written.

Q: What if the size is unknown to the programmer? Estimate too large → Memory is wasted! Estimate too low → Program wouldn’t work!

Remedy: Using vectors or dynamic arrays.

Dynamic Array: Dynamic array variables are array variables that are created while a program is running.

Remarks:

  • Dynamic array variables are dynamic variables created using new operator.
  • They are created while the program is executing.
  • The size of a dynamic array needs not be determined during compilation time. It can be determined during run-time.

Creating Dynamic Arrays using Pointer Type: typedef typeName* yourTypeName; yourTypeName arrayExample; arrayExample = new type_name[array_size];

Example: int numExam = 3; typedef double* avePtr; avePtr examAve; examAve = new double[numExam];

Remark: This will create an array examAve of size 3 with base type double. If numExam is an integer variable or an input, then the size of examAve will be determined during run-time!

Destroying Dynamic Arrays using delete Operator: delete [ ] arrayPtr;

Example: delete [ ] examAve;

Warning: If the bracket is omitted, only the first array variable pointed at by examAve will be deleted.

delete examAve; // delete only the first array element.

Example: The following program reads in a number of integers, store them in a dynamic array, sort them, and then output the integers in sorted order.