Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers and Dynamic Arrays

Similar presentations


Presentation on theme: "Pointers and Dynamic Arrays"— Presentation transcript:

1 Pointers and Dynamic Arrays

2 Pointers and Dynamic Memory
Pointer: is the memory address of a variable Memory address: at byte level Example: The integer i is located at memory address 990.

3 Pointer Variables Syntax Examples Type_Name *var_name; int *cursor;
char *c1_ptr, *c2_ptr;

4 Address Operator Examples Dereferencing operator int *example_ptr;
int i; Example_ptr = &i; //the address of i is put into the pointer variable example_ptr Dereferencing operator i = 42; example_ptr = &i; cout << i << endl; cout << *example_ptr << endl;

5 More Example int *example_ptr; int i; i = 42; example_ptr = &i;
cout << i << endl; cout << *example_ptr << endl;

6 Pointer Assignment int i = 42; int *p1; int *p2; p1 = &i; p2 = p1;
cout << *p1 << endl; cout << *p2 << endl;

7 Pointer Assignment int i = 42; int j = 80; int *p1; int *p2; p1 = &i;
p2 = &j; *p2 = *p1; cout << *p1 << endl; cout << *p2 << endl;

8

9 NULL Pointer NULL – constant defined in cstdlib
Means pointing to nothing

10 Dynamic Variables Not declared Created during execution of a program
Memory is allocated using an new operator in a special memory location called the heap Examples: int *n; n = new int; *n = 5; // Can be combined in 1 statement int *n = new int(5);

11 Dynamic Arrays Dynamically allocate entire array int *a = new int[20];
Returns a pointer to the first element in the array

12

13 Failure of new Operator
new operator fails if insufficient memory is available in the heap bad_alloc exception is thrown

14 Delete Operator When dynamic variable is no longer needed – need to release its memory delete operator Example int *n = new int(5); …… delete n; double* a = new double[6]; delete [] a;

15 Define Pointer Types Example typedef int* int_pointer;
int_pointer i_ptr;

16 Pointers as Parameters
Passing pointers as value parameters Pointer cannot be changed – object pointed to can be changed Example void confuz (int* n) { *n = 2; }

17 Arrays as Parameters Array name is treated as a pointer to first element in array Size of array is not passed in as part of the array – must be done separately Example: void make_it_all_42(int a[], size_t n) { for (size_t i = 0; i < n; i++) a[i] = 42; } int *numbers = new int[10]; make_it_all_42(numbers, 10);

18 const Parameters void confuz (const int* n);
Integer being pointed to by n cannot be modified void confuz (const int a[], size_t n); No element of array a can be modified

19 Pointer Reference Parameters
Pointer parameter can be modified since passed by reference Example: void confuz (int*& n) { n = new int (5); }

20 Dynamic Bag Implementing the bag class with a dynamic array private:
value_type *data; size_type used; size_type capacity;

21 Copy Constructor A copy constructor is a constructor with exactly one argument, and the data type of the argument is the same as the constructor’s class Need to make sure that the object and its copy are independent from each other (deep copy) For member data that are pointers – copy objects being pointed to (recursively) Shallow copy – only copy pointers bag(const bag& source); bag y(x); //initialize y as a copy of x

22 Overloading Assignment Operator
Need to use same copying concept as with copy constructors void operator =(const bag& source); bag y = x; //initialize y as a copy of x

23 Destructor Deallocate object's dynamic memory
Name – class name preceded by ~ No parameters & no return type Automatically called when object is destroyed bag::~bag( ) { delete [ ] data; }

24 Dynamic Class Design Some of the member data are pointers
Some member functions allocate & deallocate dynamic memory Assignment operator needs to be overloaded Copy constructor must be constructed Destructor must be constructed

25 C strings Null character (‘\0’) terminated arrays of characters
char s[n] – can hold at most n-1 characters Empty string char quiet[20]=“”; strcpy strcmp strcat strlen

26 STL string class Added in later versions of C++
Designed to address weaknesses of C version


Download ppt "Pointers and Dynamic Arrays"

Similar presentations


Ads by Google