/
Pointers and Dynamic Pointers and Dynamic

Pointers and Dynamic - PowerPoint Presentation

danika-pritchard
danika-pritchard . @danika-pritchard
Follow
342 views
Uploaded On 2019-12-22

Pointers and Dynamic - PPT Presentation

Pointers and Dynamic Arrays Pointers and Dynamic Memory Pointer is the memory address of a variable Memory address at byte level Example The integer i is located at m emory address 990 Pointer Variables ID: 771203

pointer int amp ptr int pointer ptr amp copy dynamic operator memory endl bag array cout parameters pointers address

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Pointers and Dynamic" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Pointers and Dynamic Arrays

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

Pointer Variables Syntax Type_Name *var_name;Examplesint *cursor;char *c1_ptr, *c2_ptr;

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

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

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

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

NULL Pointer NULL – constant defined in cstdlibMeans pointing to nothing

Dynamic Variables Not declared Created during execution of a programMemory is allocated using an new operator in a special memory location called the heapExamples:int *n;n = new int;*n = 5;// Can be combined in 1 statementint *n = new int(5 );

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

Failure of new Operator new operator fails if insufficient memory is available in the heapbad_alloc exception is thrown

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

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

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

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

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

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

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

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 classNeed 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 pointersbag(const bag& source);bag y(x); //initialize y as a copy of x

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

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

Dynamic Class Design Some of the member data are pointers Some member functions allocate & deallocate dynamic memoryAssignment operator needs to be overloadedCopy constructor must be constructedDestructor must be constructed

C strings Null character (‘\0’) terminated arrays of characterschar s[n] – can hold at most n-1 charactersEmpty string char quiet[20]=“”;strcpystrcmpstrcatstrlen

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