1 / 27

System Programming

System Programming. Practical Session 7. C++ Memory Handling. Memory Model There are two "kinds" of memory available to a process: Stack Rather small temporary storage for function-local data. Heap Big storage for data.

merv
Download Presentation

System Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Memory Model • There are two "kinds" of memory available to a process: • Stack Rather small temporary storage for function-local data. • 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 3 5

  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 result second first 8 3 5 sum b a 3 5

  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 sum b a 8 3 5

  7. Pointers • Pointers are variables whose values are memory locations - • addresses of some data • Java references • Strings1 =newString("astring"); • String s2 = s1; • s1 =null;

  8. C++ pointers • //Declaring a pointer: • int*p_j; • float*p_f; • //Accessing the values of a pointer: • 3. intj=5; • 4. p_j=&j; • 5. std::cout<<p_j<<std::endl; • 6. std::cout<<*p_j<<std::endl; • // output: 0x22ccc4 • // 5

  9. C++ pointers • Example of bad assigning to pointers • int*p_i=(int*)923451; • std::cout<<p_i<<std::endl; • std::cout<<*p_i<<std::endl;

  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 • intj=5; • inti=5; • int*p_j=&j; • int*p_j2=&j; • int*p_i=&i; • if(p_j==p_j2){std::cout<<"1 ";} • if(p_j==p_i){std::cout<<"2 ";} • if(p_j!=p_i){std::cout<<"3 ";} • if(*p_j == *p_i){std::cout<<"4 ";} //output: 1 3 4

  12. Pointers to pointers • inti=5; • int*p_i=&i; • int**pp_i=&p_i; • std::cout << pp_i << std::endl; • std::cout << *pp_i << std::endl; • std::Cout << **pp_i << std::endl; //output: 0x151bb84 // 0x151bb90 // 5

  13. Pointers Arithmetic The operations addition and subtraction are defined for pointers. + , - , += , -= , ++ , -- • intj=5; • int*i=&j; • std::cout<<"i="<<i<<std::endl; • i+=1; • 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. Stack address Arrays Arrays in C++ are just special pointers. A[0] 1916 A[1] 1920 A[9] 1952 1956 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.

  16. Stack address Arrays Arrays in C++ are just special pointers. A[0] 1916 A[1] 9 1920 A[9] 1952 j 9 1956 int j; int A[10]; A[1] = 9; j = A[1]; //*(A + 1) = 9; //j = *(A + 1);

  17. Use of regular pointers as arrays • doubleA[10]; • ints=0; • double*pi; • doublej; • pi=&j; • pi[3]=5;//BAD! • pi=A; • pi[3]=5;//OK. • pi++; • pi[0]=1;

  18. string representation using char * A string can be represented as array of chars terminating with 0. c[0] ‘a’ c[1] ‘b’ c[2] ‘c’ c[3] ‘d’ c[4] ‘e’ c[5] ‘f’ c[6] ‘g’ c[7] 0 • char*c="abcdefg"; • std::cout<<c<<std::endl; • std::cout<<c[3]<<std::endl; • std::cout<<&(c[3]); • // output abcdefg • // d • // defg

  19. Converting from string to char * and back • std::strings=std::string("abcdefg"); • const char*cs=s.c_str(); • std::strings2=std::string(cs);

  20. 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; }

  21. Memory Handling new - allocates memory on the heap, initializes it and returns a pointer to it. • int*i=new int; • int*i2=newint(2); • String*s=new string("I'montheheap”); • if(0==s){//handle allocation error} • std::cout<<"s="<<s<<std::endl; • std::cout<<"s="<<*s<<std::endl; • deletes; All memory allocated by new must be freed using delete

  22. Arrays on the heap A block of memory is allocated using new[ ]. • int*A=newint[10]; • int*B=A+1; • A[0]=1; • A[1]=2; • B[1]=3; • 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){ • deletep; • p=0; • }

  24. Pointer Dangers • Uninitialized pointers • int*p; • *p=3;//bad! • Dereferencing a null pointer • int*p=0; • *p=3;//bad! • Dereferencing a deleted pointer • int*p=newint; • deletep; • *p=5;//bad!

  25. Pointer Dangers • Dereferencing a dangling pointer • int*p,*q; • p=newint; • q=p; • deleteq; • *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. Concluding Example #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; } int main() { f1(); f2(); f3(); f4(); }

More Related