Pointers and Dynamic Arrays

advertisement
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
memory address 990.
•
Pointer Variables
•
Syntax
Type_Name *var_name;
–
•
Examples
int *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;
–
example_ptr = &i;
–
cout << i << endl;
–
cout << *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
●
●
●
●
●
●
●
int i = 42;
int *p1;
int *p2;
p1 = &i;
p2 = p1;
cout << *p1 << endl;
cout << *p2 << endl;
Pointer Assignment
●
●
●
●
●
●
●
●
●
int i = 42;
int j = 80;
int *p1;
int *p2;
p1 = &i;
p2 = &j;
*p2 = *p1;
cout << *p1 << endl;
cout << *p2 << endl;
NULL Pointer
•
•
NULL – constant defined in cstdlib
Means pointing to nothing
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);
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 heap
bad_alloc exception is thrown
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;
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 changed
Example
void confuz (int* n)
{
*n = 2;
}
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);
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
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 array
private:
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 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
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
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;
}
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
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
STL string class
•
•
Added in later versions of C++
Designed to address weaknesses of C version
Download