Short Notes On Dynamic Memory Allocation, Pointer and Data Structure
Short Notes On Dynamic Memory Allocation, Pointer and Data Structure
Short Notes On Dynamic Memory Allocation, Pointer and Data Structure
*/
Func(int array_size)
{
double k, a[100], *b, *c;
b = (double *) malloc(array_size * sizeof(double)); /* allocation in C*/
c = new double[array_size]; /* allocation in C++ */
}
The size of the problem often can not be determined at compile time.
Dynamic memory allocation is to allocate memory at run time.
Dynamically allocated memory must be referred to by pointers.
Stack vs Heap
When a program is loaded into
memory:
Machine code is loaded into text
segment
Stack segment allocate memory
for automatic variables within
functions
Heap segment is for dynamic
memory allocation
3
Pointers
A variable can be viewed as a specific block of memory in
the computer memory which can be accessed by the
identifier (the name of the variable).
int k; /* the compiler sets aside 4 bytes of memory (on a PC) to hold the value
of the integer. It also sets up a symbol table. In that table it adds the symbol k
and the relative address in memory where those 4 bytes were set aside. */
k = 8; /*at run time when this statement is executed, the value 8 will be
placed in that memory location reserved for the storage of the value of k. */
With k, there are two associated values. One is the value of the
integer, 8, stored. The other is the value or address of the memory
location.
The variable for holding an address is a pointer variable.
int *ptr; /*we also give pointer a type which refers to the type of data stored
at the address that we will store in the pointer*/
4
Example 1
Func() /* C++ version */
{
double *ptr;
ptr = new double;
*ptr = -2.5;
}
Func_C() /* C version */
{
double *ptr;
ptr = (double *) malloc(sizeof(double));
.
}
Illustration
Name
Type
Contents
ptr
double pointer
0x3D3B38
Address
0x22FB66
0x3D3B38
0x3D3B39
-2.5
7
Example 2
Func() /* C++ version */
{
double *ptr, a[100];
ptr = new double[10]; /* in C, use: ptr = (double *)malloc(sizeof(double)*10); */
for(int i = 0; i < 10; i++)
ptr[i] = -1.0*i;
a[0] = *ptr;
a[1] = *(ptr+1); a[2] = *(ptr+2);
}
Type
Contents
Address
Illustration Name
ptr
double array
0x3D3B38
0x22FB66
pointer
Memory heap (free storage we can use)
0x3D3B38
0.0
0x3D3B39
-1.0
Example 3
Static array of dynamically allocated vectors
Func() /* allocate a contiguous memory which we can use for 20 30 matrix */
{
double *matrix[20];
int i, j;
for(i = 0; i < 20; i++)
matrix[i] = (double *) malloc(sizeof(double)*30);
for(i = 0; i < 20; i++)
{
for(j = 0; j < 30; j++)
matrix[i][j] = (double)rand()/RAND_MAX;
}
}
9
Example 4
Dynamic array of dynamically allocated vectors
Func() /* allocate a contiguous memory which we can use for 20 30 matrix */
{
double **matrix;
int i, j;
matrix = (double **) malloc(20*sizeof(double*));
for(i = 0; i < 20; i++)
matrix[i] = (double *) malloc(sizeof(double)*30);
for(i = 0; i < 20; i++)
{
for(j = 0; j < 30; j++)
matrix[i][j] = (double)rand()/RAND_MAX;
}
}
10
Example 5
Another way to allocate dynamic array of dynamically allocated vectors
Func() /* allocate a contiguous memory which we can use for 20 30 matrix */
{
double **matrix;
int i, j;
matrix = (double **) malloc(20*sizeof(double*));
matrix[0] = (double*)malloc(20*30*sizeof(double));
Pass by value
1.
2.
#include<iostream>
void foo(int);
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
When foo() is called, variable y is created, and the value of 5, 6 or 7 is copied into y.
Variable y is then destroyed when foo() ends.
13
#include<iostream>
void foo2(int*);
using namespace std;
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
Passing by address means passing the address of the argument variable. The function parameter
must be a pointer. The function can then dereference the pointer to access or change the value being
pointed to.
1. It allows us to have the function change the value of the argument.
2. Because a copy of the argument is not made, it is fast, even when used with large structures or
classes.
3. Multiple values can be returned from a function.
14
Pass by reference
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
#include<iostream>
void foo3(int&);
using namespace std;
void foo3(int &y) // y is now a reference
{
cout << "y = " << y << endl;
y = 6;
cout << "y = " << y << endl;
} // y is destroyed here
int main()
{
int x = 5;
cout << "x = " << x << endl;
foo3(x);
cout << "x = " << x << endl;
return 0;
}
Since a reference to a variable is treated exactly the same as the variable itself, any
changes made to the reference are passed through to the argument.
15
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
#include <iostream>
int nFive = 5;
int nSix = 6;
void SetToSix(int *pTempPtr);
using namespace std;
int main()
{
int *pPtr = &nFive;
cout << *pPtr;
SetToSix(pPtr);
cout << *pPtr;
return 0;
}
// pTempPtr copies the value of pPtr!
void SetToSix(int *pTempPtr)
{
pTempPtr = &nSix;
cout << *pTempPtr;
}
16
17
newNode->prev = location->prev;
newNode->next = location;
location->prev->next=newNode;
location->prev = newNode;
18
node* temp;
1. temp = location->prev;
2. temp->next =location->next;
3. (temp->next)->prev = temp;
4. free(location);
19
header
trailer
trailer
Initialization:
node header, trailer;
1. header.next = &trailer;
2. trailer.prev = &header;
20
21
Stacks
A stack is a container of objects that are
inserted and removed according to the lastin first-out (LIFO) principle. In the
pushdown stacks only two operations are
allowed: push the item into the stack, and
pop the item out of the stack.
template <class T>
class stack {
T* v;
T* p;
int sz;
public:
stack (int s)
{v = p = new T[sz = s];}
~stack()
{delete[] v;}
void push (T a) { *p = a; p++;}
T pop()
{return *--p;}
int size() const {return p-v;}
};
stack <char> sc(200); // stack of characters
Remark:
The template <class T> prefix
specifies that a template is being
declared and that an argument T
of type type will be used in the
declaration. After its introduction,
T is used exactly like other type
names. The scope of T extends to
the end of the declaration that
template <class T> prefixes.
22
23
Hash Table
A hash is a data structure used to implement
an associative array, a structure that can map
keys to values. A hash table uses a hash
function to compute an index into an array of
buckets or slots, from which the correct value
can be found.
See also http://xlinux.nist.gov/dads/HTML/hashtab.html
24
C++ Template
C++ templates (or parameterized types) enable users to define a family
of functions or classes that can operate on different types of
information. See also http://www.cplusplus.com/doc/oldtutorial/templates/
// min for ints
int min( int a, int b ) {
return ( a < b ) ? a : b;
}
25