cs31 lecture 10

If the variable is not defined in the current sub

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: examples 41 Scopes in C++ A variable is accessible only from within its scope Scope: region (typically a block enclosed with {} ) where the variable exists Examples: for (int i = 0; i < N; ++i) sum += i; foo = i; // <- invalid statement: i is not defined 42 The intuition behind scopes A program can be seen as a hierarchy of regions A variable declared in a region can be used only from within this region and all its subregion From a (sub-)region, the closest definition of the variable applies. If the variable is not defined in the current sub-region, then a definition is searched for in the parent region, etc. 43 Example: foo.cpp // region 1 starts int foo() { // region 1.1 starts. int i = 42; { // region 1.1.1 starts. Parents: 1.1, and 1 float i = 0; // region 1.1.1 ends } { // region 1.1.2 starts. Parents: 1.1, and 1 // region 1.1.2 ends } // region 1 ends. } // region 1 ends 44 The case of for loops The <init> clause of the for loop belongs to the scope of the for loop Be careful: a for loop is a nested scope: for (int i = 42; i < 100; ++i) sum += i; Is translated into: { // scope 1 starts int i = 42; while (i < 100) { // scope 1.1 starts sum += i; ++i; } 45 Typical problems Re-defining a variable that was previously defined (not an error: it is a valid C++ behavior) Trying to use a variable not defined in the current scope Defining a missing variable in the wrong scope 46 Arrays Arrays are a collection of data elements organized in a specific order Roughly: a set of variables of the same type that can be accessed in an homogeneous way Vectors (see lect. 5) are a special case of array Arrays are the core data structure to program useful things Matrices, images Data structures (stack, queue, graph, tree, etc.) 47 Content vs. Address of a memory slot Central concept: the value in a memory cell can be: An actual 32-bit number The address of another memory cell Intuition: M = [ 2 | 3 | 0x9 | 2 | 3 ] 0 1 2 3 4 M[0] contains 2 The value at address 0 is 2 M = [ 2 | 3 | 0x9 | 2 | 3 | 0x0 ] 0 1 2 3 4 5 48 Address of a memory slot M[5] contains the address of the slot 0 To get the value of slot 5, do M[5] (it is an address) To get the value in slot 0, do M[M[5]] (it is a value) To get the address of M[5], do 5 (often noted 0x5) These are the concepts of pointers and 49 Pass-by-reference Central idea: instead of copying the value of the argument, simply copy the address where the arguments are stored Major differences with pass-by-value Modifying the value of the arguments inside the function changes their value at the call site Whatever the type of data, only 32 bits (or 64 bits) are copied In C++, append the & symbol to the type of the argument to mark an argument as 50 The example revisited int foo(int& a, int& b) { int c = a + b; return c; } int main() { int tmp_a = 2; int tmp_b = 3; return foo(tmp_a, tmp_b); } Memory at the beginning of the function call: M = [ 2 | 3 | 0x9 | 0x0 | 0x1 ] 0 1 2 3 4 M[3] is the first argument, M[M[3]] is its value 51 Observations M = [ 2 | 3 | 0x9 | 0x0 | 0x1 ] 0 1 2 3 4 In C++, for this example, the variable int& a is linked to M[3] When assigning/reading a, the compiler automatically implemented M[M[3]] Ex: a = a + 1; // translates into M[M[3]] = M[M[3]] + 1; In the pass-by-value case, for int a, the compiler automatically implements M[3] 52 More observations In general, there can be an unlimited number of references to the same memory slot: M = [ 2 | 3 | 0x9 | 0x0 | 0x0 | 0x0 | 0x0 ] 0 1 2 3 4 5 6 Example in C++: void foo(int& a, int& b, int& c) { } int main() { int bar = 42; foo(bar, bar, bar); } 53 More observations The concept of pointer (in C, C++) differs slightly from the concept of reference In C++, you must pass explicitly a variable as an argument of a function which is passby-reference E.g., foo(0, 1, 2) is forbidden in the previous example In C (and C++), you can pass a numerical address as an argument of a function which is of pointer type 54 Examples 55 More on references A reference to a variable can be declared within a function It is a standard type qualifier Be careful: when a function terminates, the (temporary) memory slots allocated to it are “destroyed” A reference can be passed as argument, and returned by the function Remember: arrays are always passed-byreference 56 Midterm Grades will be posted after lecture Exams will be available in Professor Pouchet’s office Thursdays 11:30am12:30pm 57...
View Full Document

Ask a homework question - tutors are online