C++ Overview
CS 250 Data Structures and Algorithms
Prof. Dr. Faisal Shafait
1 / 12
Review C++
Pointers and Arrays
Pointers and arrays are closely related.
1 char amessage [] = "hello"; // array
2 char * pmessage = "hello"; // pointer
Construction of the arrays of pointers is also allowed.
use of arrays of pointers is to store character strings of variable
lengths.
char * months [] = { "Illegal month", "Jan", "Feb", "Mar" };
2 / 12
Review C++
Pointer arithmetic
For any pointer T *p; the following holds:
p[n] is the same as *(p+n);
adding and integer n to a pointer increments it by the n times the
size of the type – and not by n bytes
3 / 12
Review C++
Pointer arithmetic
For any pointer T *p; the following holds:
Increment ++ and decrement −− increase/decrease by one
element
Be sure to only use valid pointers
initialize them
do not use them after the object has been deleted!
catastrophic errors otherwise
4 / 12
Review C++
Dynamic memory allocation
When one defines a variable in C++,
the compiler allocates memory for that variable automatically
when the program begins running.
data structure declared this way is called a static structure.
it never changes size for the duration of the run of the program.
Consider,
1 float x[10]; // allocates memory for 10 numbers
Allocation of flexible size,
1 unsigned int n;
2 std::cin >> n;
3 float x[n]; // will not work
The compiler has to know the number!
Solution: dynamic allocation
5 / 12
Review C++
Dynamic memory allocation: new command
When you want to allocate memory while the program is running,
it is done using dynamic memory allocation.
a structure created this way is called a dynamic structure.
In C++, dynamic memory allocation is done using the new command.
takes a type as an argument
allocates enough memory to hold a variable of that type.
and, returns the address of the newly allocated memory.
The returned address should then be stored in a pointer.
MyTypePtr = new MyType;
MyType is some type (int, float, a structure or a class), and MyTypePtr is
a pointer to a MyType.
6 / 12
Review C++
Dynamic memory allocation: new command
Examples:
dynamically allocates memory for a single int, and stores the
address in intPtr.
1 int * intPtr;
2 intPtr = new int;
dynamically allocates memory for a single int, initializes it to the
value 6, and stores the address in intPtr.
1 int * intPtr;
2 intPtr = new int (6);
7 / 12
Review C++
Dynamic memory allocation: arrays
To dynamically allocate an array, specify the array size in square
brackets [] after the type:
1 float * x = new float[n]; // allocate some memory for an array
2 x[0] = 3.0f; // do some work with the array x
3 ...
To delete a dynamically allocated array, you must include a pair of
empty square brackets,
1 delete [] x; // delete the memory for the array.
2 // x[i], * x now undefined!
8 / 12
Review C++
Dynamic memory allocation: delete command
To deallocate memory which has been allocated dynamically, use the
delete command:
1 delete intPtr; // Deallocates memory pointed to by
2 // intPtr.
delete command does NOT delete the variable intPtr itself,
it deallocates the memory whose address is stored in intPtr.
and, makes it available for future allocation
9 / 12
Review C++
Memory leaks
A memory leak occurs when a computer program incorrectly manages
memory allocations.
Illegal memory accesses (segmentation faults)
Stack overflow
Infinite recursions
Double frees
Dangerous use of uninitialized variables
10 / 12
Review C++
Memory leaks
Since memory is user managed, there can be mistakes.
Memory leaks = allocated memory that’s not freed.
Why it matters?
1 #include <stdlib.h>
2
3 int main ()
4 {
5 char * x = (char * ) malloc (100); // Memory Leak!
6
7 return 0;
8 }
Allocation profilers: valgrind (linux), _CrtDumpMemoryLeaks (Visual
Studio).
11 / 12
Review C++
Reading Material
For further reading, please refer to
Data Structures by Weiss: Overview of Chapter 1 & 2
12 / 12