4.4. The Bucket Analogy

The Bucket Analogy will help you remember what lvalues and rvalues are all about and reinforce your understanding of what a data definition is. Suppose you write the statement

int val;

You will recall that Visual Studio checks the syntax of this statement and then searches the symbol table to make sure val has not already been defined at the current scope. Assuming those checks are passed and Intellisense is happy, Visual Studio sends a message to the Windows Memory Manager asking for 4 bytes of free memory for the int you wish to define. Assuming all goes well, the Windows Memory Manager sends back the memory address at which val will be stored. Let's assume that the memory address is 750,000. That memory address is then entered into the lvalue column in the symbol table.

The Bucket Analogy assumes that you have a pile of empty buckets lying around. The buckets come in different sizes. Some are big enough to hold one byte of data, some can hold two bytes of data, and some buckets are big enough to hold 16 bytes of data. Because you want to define an int data type, you select a bucket that is big enough to hold an int data type. As you learned in Chapter 3, an int takes 4 bytes of memory, so the bucket you select is a 4-byte bucket.

Now, paint the variable's name, val in this example, on the bucket so you can distinguish it from other buckets that might be stored in memory. Now, pretend you can physically take the bucket inside your computer and place it ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.