Download presentation
Presentation is loading. Please wait.
Published byNelson White Modified over 9 years ago
1
System Programming Practical Session 7 C++ Memory Handling
2
Memory Model There 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.
3
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
4
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 sum b a 3535
5
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 835835 result second first 3535 sum b a
6
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 835835 sum b a
7
Pointers Pointers are variables whose values are memory locations - addresses of some data Java references 1.String s1 = new String("a string"); 2.String s2 = s1; 3.s1 = null;
8
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
9
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
10
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;
11
Comparing pointers 1.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
12
Pointers to pointers 1.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
13
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
14
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.
15
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.
16
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);
17
Use of regular pointers as arrays 1.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;
18
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
19
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
20
Using command line arguments #include 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; }
21
Memory Handling new - 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
22
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;
23
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; }
24
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!
25
Pointer Dangers Dereferencing a dangling pointer 1. int *p, *q; 2. p = new int; 3. q = p; 4. delete q; 5. *p = 3; // bad!
26
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];
27
#include 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; cout << "this line is never reached" << endl; return; } Concluding Example int main() { f1() ; f2() ; f3() ; f4() ; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.