References      andDynamic Memory   Allocation
OBJECTIVES• To be able to use references as function  parameters.• To be able to return references by  functions• To under...
PASSING REFERENCES TO         FUNCTIONS• 3 methods for passing values to  functions: by value, address,  reference.• When ...
void set(int x, int &ce, int &co){  if((x%2)==0)        ce++;  else       co++;                Passing by reference}      ...
Reference parameter can be preceded with const to prevent a function from changing them inadvertently void fun(const int &...
Both same effect, but references as function     parameter have several advantages• Code is cleaner, no *.• Does not have ...
RETURNING REFERENCES BY        FUNCTIONS• A function may return a reference.• Returning a reference by a function  also pe...
Function returns a reference to the a[n] arrayconst int SIZE = 6;                             element.int & put_val(int a[...
• Less prone error than a direct assignment.• put_val() checks at runtime that the array  boundaries are not exceeded befo...
DYNAMIC MEMORYALLOCATION
STATIC VERSUS DYNAMIC• STATIC MEMORY ALLOCATION - fixed size array to allocate  memory.• An amount of memory allocated - r...
• DYNAMIC MEMORY ALLOCATION solve  this problem.• Allocate memory at run-time.• pointer_var = new data_type(initial value)...
Memory Leakfloat *ptr = new float;   //Allocates 1st block*ptr = 7.9;                //Accesses 1st blockptr = new float; ...
DYNAMIC ARRAY
• Dynamically allocated array - variable size.• Allocate single-dimensional dynamic array:-      Pointer_var = new data_ty...
•Allocate two-dimensional dynamic array:-      data_type **pointer_var;      pointer_var = new data_type *[size];•Dealloca...
Upcoming SlideShare
Loading in...5
×

Chp4(ref dynamic)

167

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
167
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chp4(ref dynamic)

  1. 1. References andDynamic Memory Allocation
  2. 2. OBJECTIVES• To be able to use references as function parameters.• To be able to return references by functions• To understand the difference between static and dynamic memory allocation• To be able to allocate, process and deallocate dynamic arrays
  3. 3. PASSING REFERENCES TO FUNCTIONS• 3 methods for passing values to functions: by value, address, reference.• When passed to functions, references have a clear advantage over pointers.
  4. 4. void set(int x, int &ce, int &co){ if((x%2)==0) ce++; else co++; Passing by reference} - Is like passing the address of variable used asint main(){ argument in function call. int num, - When reference is used within the function, int even = 0; compiler automatically uses odd variable, which int odd = 0; is referenced by co. cin>>num; set(num, even, odd); cout<<“even:”<<even<<“ odd:”<<odd;}
  5. 5. Reference parameter can be preceded with const to prevent a function from changing them inadvertently void fun(const int &cref) { cout<<cref/15; //Okay cref++; //ERROR! Can’t modify a const reference }//Pointers as function parametersvoid set(int x, int *ce, int *co){ if((x%2)==0) *ce++; //A pointer has to be dereferenced else *co++;}set(num, &even, &odd); //Function called
  6. 6. Both same effect, but references as function parameter have several advantages• Code is cleaner, no *.• Does not have to remember to pass address of function argument.• Unlike passing with a pointer, no memory location required.
  7. 7. RETURNING REFERENCES BY FUNCTIONS• A function may return a reference.• Returning a reference by a function also permits the function to be called from left side of assignment operator.
  8. 8. Function returns a reference to the a[n] arrayconst int SIZE = 6; element.int & put_val(int a[], int n){ if(n>=SIZE || n<0){ cout<<“Out of boundaries”; exit(1); } return a[n]; } OUTPUT: array[0] = 0 array[1] = 2 // put_val(array, i) = i *2; array[2] = 4 // array[I] = i *2; array[3] = 6 array[4] = 8int main(){ array[5] = 10 int array[SIZE]; for(int i=0; i<SIZE; i++) put_val(array, i)=i*2; //function call on the left for(int j=0; j<SIZE; j++) cout<<“array[“<<j<<“] = “<<array[j]<<endl;}
  9. 9. • Less prone error than a direct assignment.• put_val() checks at runtime that the array boundaries are not exceeded before it returns a reference to an array element.• Prevents runtime errors such as array overflows/underflows(caused by assigning a value to an array element specified by the index that is outside of the boundaries of the array)
  10. 10. DYNAMIC MEMORYALLOCATION
  11. 11. STATIC VERSUS DYNAMIC• STATIC MEMORY ALLOCATION - fixed size array to allocate memory.• An amount of memory allocated - reserved when the program is loaded into memory.• Could fail if run on comp. system lack enough memory.struct comp_part{ char code[7]; char description[30]; int on_stock; int sold; float price;};comp_part list[100];
  12. 12. • DYNAMIC MEMORY ALLOCATION solve this problem.• Allocate memory at run-time.• pointer_var = new data_type(initial value);• delete pointer_var;• Eg: float *fpt = new float(0.0); if(fpt==0){ //checks for memory allocation error cout<<“Memory allocation error.”; exit(1); }*fpt = 3.45; //uses pointer to access memorycout<<*fpt;delete fpt; //frees memory allocate dynamically
  13. 13. Memory Leakfloat *ptr = new float; //Allocates 1st block*ptr = 7.9; //Accesses 1st blockptr = new float; //Allocates 2nd block*ptr = 5.1; //Accesses 2nd block• 1st memory block not deleted, address lost.• Pointer contain address of 2nd block.• Without delete, causes memory leak.
  14. 14. DYNAMIC ARRAY
  15. 15. • Dynamically allocated array - variable size.• Allocate single-dimensional dynamic array:- Pointer_var = new data_type[size];• Deallocate single-dimensional dynamic array:- Delete []pointer_var• Eg.:-int *ptr;ptr = new int[10];for(int i=0; i<10; i++) ptr[i] = (i+1)*2;delete []ptr;
  16. 16. •Allocate two-dimensional dynamic array:- data_type **pointer_var; pointer_var = new data_type *[size];•Deallocate two-dimensional dynamic array:- as shown in paper.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×