CS301-Lec18 handout - CS301 Data Structures Lecture No. 18...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 18 ___________________________________________________________________ Data Structures Lecture No. 18 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 4.3.3 Summary Reference Variables const keyword Tips Reference Variables In the last lecture we were discussing about reference variables, we saw three examples; call by value, call by reference and call by pointer. We saw the use of stack when a function is called by value, by reference or by pointer. The arguments passed to the function and local variables are pushed on to the stack. There is one important point to note that in this course, we are using C/C++ but the usage of stack is similar in most of the computer languages like FORTRAN and Java . The syntax we are using here is C++ specific, like we are sending a parameter by pointer using sign. In Java, the native data types like int , float are passed by value and the objects are passed by reference. In FORTRAN, every parameter is passed by reference. In PASCAL, you can pass a parameter by value or by reference like C++. You might have heard of ALGOL, this language had provided another way of passing parameter called call by name . These kinds of topics are covered in subjects like Study of Computer Languages or Compiler’s Theory . It is recommended while you are doing your degree, you study other computer languages and compare them from different aspects. Java is quite popular now a day, quite similar in syntax to C++. May be as a next language, you can study that and compare its different aspects with C/C++. The concepts like how a program is loaded into memory to become a process, how the functions are called and the role of stack etc are similar in all major languages. we have discussed when the variables are passed by reference then behind the scene what goes on inside the stack. There are few important things to take care of while using reference variables: One should be careful about transient objects that are stored by reference in data structures. We know that the local variables of a function are created on call stack. Those variables Page 1 of 8
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 18 ___________________________________________________________________ are created inside the function, remains in memory until the control is inside the function and destroyed when the function exits. Activation record comprise of function call parameters, return address and local variables. The activation record remains inside stack until the function is executing and it is destroyed once the control is returned from the function. Let’s see the following code that stores and retrieves objects in a queue: void loadCustomer( Queue & q) { Customer c1(“irfan”); Customer c2(“sohail”); q.enqueue( c1 ); q.enqueue( c2 ); } Above given is a small function , which accepts a parameter of type Queue by reference. Inside the function body, firstly, we are creating
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 8

CS301-Lec18 handout - CS301 Data Structures Lecture No. 18...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online