System Programming Practical Session 7 C++ Memory Handling.

27
System Programming Practical Session 7 C++ Memory Handling

description

Memory Model int add(int first, int second){ int result; result = first + second; return result; } int main(){ int a = 5, b = 3,sum; sum = add(a,b); } heap stack

Transcript of System Programming Practical Session 7 C++ Memory Handling.

Page 1: System Programming Practical Session 7 C++ Memory Handling.

System Programming

Practical Session 7

C++ Memory Handling

Page 2: System Programming Practical Session 7 C++ Memory Handling.

Memory ModelThere are two "kinds" of memory available to a process:

1. Stack Rather small temporary storage for function-local data.

2. Heap Big storage for data.

• Note that every variable you declare inside a function lives on the stack.

• Once you leave the scope of the function, the stack variables are discarded.

Page 3: System Programming Practical Session 7 C++ Memory Handling.

Memory Model

int add(int first, int second){

int result;

result = first + second;

return result;

}

int main(){

int a = 5, b = 3,sum;

sum = add(a,b);

}

heap

stack

Page 4: System Programming Practical Session 7 C++ Memory Handling.

Memory Model

int add(int first, int second){

int result;

result = first + second;

return result;

}

int main(){

int a = 5, b = 3,sum;

sum = add(a,b);

}

heap

stacksum

ba

35

Page 5: System Programming Practical Session 7 C++ Memory Handling.

Memory Model

int add(int first, int second){

int result;

result = first + second;

return result;

}

int main(){

int a = 5, b = 3,sum;

sum = add(a,b);

}

heap

stack

835

resultsecond

first

35

sumba

Page 6: System Programming Practical Session 7 C++ Memory Handling.

Memory Model

int add(int first, int second){

int result;

result = first + second;

return result;

}

int main(){

int a = 5, b = 3,sum;

sum = add(a,b);

}

heap

stack

835

sumba

Page 7: System Programming Practical Session 7 C++ Memory Handling.

Pointers

Pointers are variables whose values are memory locations -

addresses of some data

Java references1. String  s1  =  new  String("a string");

2. String  s2  = s1;

3. s1 =  null;

Page 8: System Programming Practical Session 7 C++ Memory Handling.

C++ pointers//Declaring a pointer:

1. int *p_j;

2. float *p_f;

//Accessing the values of a pointer:

3. int j = 5; 

4. p_j = &j;  

5. std::cout << p_j << std::endl;  

6. std::cout << *p_j << std::endl; 

// output: 0x22ccc4// 5

Page 9: System Programming Practical Session 7 C++ Memory Handling.

Example of bad assigning to pointers

1. int *p_i = (int *) 923451; 

2. std::cout << p_i << std::endl;  

3. std::cout << *p_i << std::endl;                   

C++ pointers

Page 10: System Programming Practical Session 7 C++ Memory Handling.

The operators & and *&

The address of the given variable.

*

-The variable of the given address.

- In declaration: means that this variable is a pointer.

int j;

int * p_j;

p_j = &j;

*p_j = 10;

Page 11: System Programming Practical Session 7 C++ Memory Handling.

Comparing pointers1. int j = 5; 

2. int i = 5;

3. int *p_j = &j;  

4. int *p_j2 = &j;

5. int *p_i = &i;   

6. if (p_j == p_j2) { std::cout << "1 " ; } 

7. if (p_j == p_i)  { std::cout << "2 ";  } 

8. if (p_j != p_i)  { std::cout << "3 " ; } 

9. if (*p_j  ==  *p_i) { std::cout << "4 " ; }

//output: 1 3 4

Page 12: System Programming Practical Session 7 C++ Memory Handling.

Pointers to pointers1. int i = 5; 

2. int *p_i = &i;

3. int **pp_i = &p_i; 

4. std::cout << pp_i << std::endl;  

5. std::cout << *pp_i << std::endl; 

6. std::Cout << **pp_i << std::endl; 

//output: 0x151bb84

// 0x151bb90

// 5

Page 13: System Programming Practical Session 7 C++ Memory Handling.

Pointers Arithmetic

The operations addition and subtraction are defined for pointers.

+ , - , += , -= , ++ , --

1. int j = 5;

2. int *i = &j;

3. std::cout << "i   = " << i << std::endl;

4. i+=1;

5. std::cout << "i+1 = " << i << std::endl;

// output: i = 0x22ccc4 // i+1 = 0x22ccc8

Page 14: System Programming Practical Session 7 C++ Memory Handling.

Arrays

Arrays in C++ are just special pointers.

Declaration: int A[10];

•A memory block big enough for holding 10 integers (10*sizeof(int)) is allocated on the stack

•A holds the address of the start of this block.

•The address A holds can’t be changed:

A = A + 1; // compilation error.

Page 15: System Programming Practical Session 7 C++ Memory Handling.

Arrays

Arrays in C++ are just special pointers.

Declaration: int A[10]; A[0] 1916

A[1] 1920

A[9] 1952

1956

Stack

address

•A memory block big enough for holding 10 integers (10*sizeof(int)) is allocated on the stack

•A holds the address of the start of this block.

•The address A holds can’t be changed:

A = A + 1; // compilation error.

Page 16: System Programming Practical Session 7 C++ Memory Handling.

Arrays

Arrays in C++ are just special pointers.

int j;

int A[10];

A[1] = 9;

j = A[1];

A[0] 1916

A[1] 9 1920

A[9] 1952

j 9 1956

Stack

address

//*(A + 1) = 9;

//j = *(A + 1);

Page 17: System Programming Practical Session 7 C++ Memory Handling.

Use of regular pointers as arrays1. double A[10];

2. int s=0;  

3. double *pi;

4. double j;

5. pi = &j;

6. pi[3] = 5; // BAD! 

7. pi = A;

8. pi[3] = 5; // OK. 

9. pi++;      

10.pi[0] = 1; 

Page 18: System Programming Practical Session 7 C++ Memory Handling.

string representation using char *

A string can be represented as array of chars terminating with 0.

1. char *c = "abcdefg";

2. std::cout << c << std::endl;

3. std::cout << c[3] << std::endl; 

4. std::cout << &(c[3]); 

// output abcdefg

// d

// defg

c[0] ‘a’

c[1] ‘b’

c[2] ‘c’

c[3] ‘d’

c[4] ‘e’

c[5] ‘f’

c[6] ‘g’

c[7] 0

Page 19: System Programming Practical Session 7 C++ Memory Handling.

1. std::string  s = std::string("abcdefg");   

2. const char *cs = s.c_str();             

3. std::string  s2 = std::string(cs);

Converting from string to char * and back

Page 20: System Programming Practical Session 7 C++ Memory Handling.

Using command line arguments#include <iostream>#include <string>using namespace std;int main(int argc, char **argv) { cout << "Program name: " << argv[0] << endl; cout << "Number of paramters: " << (argc - 1) << endl; if (argc > 1) { cout << "Parameters: " << endl; for (int i=1; i<argc; ++i) { cout << "\t" << string(argv[i]) << endl; } } return 0;}

Page 21: System Programming Practical Session 7 C++ Memory Handling.

Memory Handlingnew - allocates memory on the heap, initializes it and returns a pointer to it.

1. int *i = new  int;       

2. int *i2 = new  int(2);   

3. String *s = new  string("I'm on the heap”);

4. if ( 0 == s) { // handle  allocation  error }

5. std::cout << "s=" << s << std::endl;

6. std::cout << "s=" << *s << std::endl;

7. delete  s;  All memory allocated by new must be freed using delete

Page 22: System Programming Practical Session 7 C++ Memory Handling.

Arrays on the heap

A block of memory is allocated using new[ ].

1. int *A = new int[10]; 

2. int *B = A + 1;

3. A[0] = 1;

4. A[1] = 2;

5. B[1] = 3;

6. delete[] A; 

        

Page 23: System Programming Practical Session 7 C++ Memory Handling.

Safe delete

Delete only what has been allocated by new and hasn’t been deleted yet.

//First, ensure that p points to a storage allocated by new.

// Then use the following.

• if (0 != p){

• delete p;

• p=0;

• }

Page 24: System Programming Practical Session 7 C++ Memory Handling.

Pointer Dangers•Uninitialized pointers

1.int *p; 

2.  *p = 3; // bad!

•Dereferencing a null pointer

1.int *p = 0;  

2. *p = 3;         // bad!

•Dereferencing a deleted pointer

1.int *p = new int;

2.  delete p;

3.  *p = 5;              // bad!

Page 25: System Programming Practical Session 7 C++ Memory Handling.

Pointer Dangers

•Dereferencing a dangling pointer

1. int *p, *q;

2.      p = new int;

3.      q = p;           

4.      delete q;     

5.      *p = 3;          // bad!

Page 26: System Programming Practical Session 7 C++ Memory Handling.

Memory leak

int *ar = new int[1000];

……….. //some use of ar, but no delete

ar = new int[235];

Correction:

int *ar = new int[1000];

………..

if (0 != ar)

delete[] ar;

ar = new int[235];

Page 27: System Programming Practical Session 7 C++ Memory Handling.

#include <iostream> #include <string>using namespace std;void f1() { string s("I will die young"); cout << s << endl; } void f2() { string* p_s = new string("I will survive!"); cout << *p_s << endl;} void f3() { string* p_s = new string("I will be murdered"); cout << *p_s << endl; delete p_s; } void f4() { string* p_s = new string("I'll die and take everyone with me"); cout << *p_s << endl; delete p_s; delete p_s; cout << "this line is never reached" << endl; return;}

Concluding Example

int main() { f1() ; f2() ; f3() ; f4() ;}