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: 554109
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.
Slide1
Pointers and Dynamic
ArraysSlide2
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.Slide3
Pointer Variables
Syntax
Type_Name *var_name;Examplesint *cursor;char *c1_ptr, *c2_ptr;Slide4
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
;Slide5
More Example
int
*example_ptr;int i;i = 42;example_ptr = &i;*example_ptr = 0;cout << i << endl
;
cout
<<
*
example_ptr
<<
endl
;Slide6
Pointer Assignment
i
nt i = 42;int *p1;int *p2;p1 = &i;p2 = p1;cout << *p1 << endl;cout << *p2 << endl;Slide7
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
;Slide8Slide9
NULL Pointer
NULL – constant defined in
cstdlibMeans pointing to nothingSlide10
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
);Slide11
Dynamic Arrays
Dynamically allocate entire array
int *a = new int[20];Returns a pointer to the first element in the arraySlide12Slide13
Failure of
new Operator
new operator fails if insufficient memory is available in the heapbad_alloc exception is thrownSlide14
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;Slide15
Define Pointer Types
Example
typedef int* int_pointer;int_pointer i_ptr;Slide16
Pointers as Parameters
Passing pointers as value parameters
Pointer cannot be changed – object pointed to can be changedExamplevoid confuz (int* n){ *n = 2;}Slide17
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);Slide18
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 modifiedSlide19
Pointer Reference Parameters
Pointer parameter can be modified since passed by reference
Example:void confuz (int*& n){ n = new int (5);}Slide20
Dynamic
Bag
Implementing the bag class with a dynamic arrayprivate:value_type *data; size_type used; size_type capacity; Slide21
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 xSlide22
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 xSlide23
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; }Slide24
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 constructedSlide25
C strings
Null
character (‘\0’) terminated arrays of characterschar s[n] – can hold at most n-1 charactersEmpty string char quiet[20]=“”;strcpystrcmpstrcatstrlenSlide26
STL
string class
Added in later versions of C++Designed to address weaknesses of C version