Categories
Just Shelley

Bad Knee

Recovered from the Wayback Machine.

Walked to Pier 39 tonight. Weather was very pleasant, and crowd was fun. I got some silly socks for my brother and his family for Christmas.

Walking back, my knee gave out completely and I literally couldn’t walk at first. I managed to hop to a wall to sit until the pain stopped a bit. I grabbed the streetcar and was finally able to make it home, though going was slow.

I hurt my knee when I tripped over computer cables while working at what is now the defunct skyfish.com. The orthopedic surgeon I saw recently thinks I may have broken my knee at the time and have damage now. I’m supposed to try therapy for several weeks to see if we can avoid surgery, but the insurance company is being extremely difficult. As an FYI, If your company has Liberty Mutual for worker’s compensation insurance, all I can say is good luck if you get hurt on the job. Cheap assholes.

Categories
Just Shelley

While in San Francisco

Recovered from the Wayback Machine.

I just noticed that a lot of folks running weblogs have the flu or bad colds this week. Tis the season I guess. To all the sickies out there, get well.

I’m feeling pretty good myself. Friday was a beautiful day and looks like the weather will be nice next week. Bad knee or not, I’m getting off my butt and am heading into the wilds surrounding San Francisco. I’m in an antsy mood and need to cut loose a bit! It’s either go out of town and do some hiking, or hit some of the bars in SOMA or Mission. Of the two choices, I’ll be safer in the back country.

Categories
Just Shelley

A Seventies kind of thing

Drugs – Sugar Bowl Park, where rich family members came out to play Frisbee and the rest us came out to pop a little speed. Don’t let those Just Say No ads fool you folks; the only time we acknowledged that this country had a drug problem is when all those rich kids started getting high. Before that it was all Sugar Bowl.

Trip over the mountain – met these two little old ladies in a white van going across the pass from Reno to Sacramento. One lady was the mother of the other which seemed al-most un-believable to such young eyes when I realize how OLD the daughter was. And the mother had a wooden leg. Now, if you are going to cross a mountain pass in a snow blizzard this has got to be a one of kind way of doing it.

Berkley — There it was, the Mecca for all the flower children all over the world. All of us lined up for probably about 5 miles with cardboard signs saying “Seattle” or “New York” or “London”. Now, I can give you Seattle and New York, but London? Did these people really think that someone would be on their way to London on an expressway outside Berkley? It was a gimmick. We were learning even then. No wonder we grew up to what we are today.

Ketchup – a favorite way of attracting attention at demonstrations was to wait until a camera was near, try and get some poor fool of a cop to come near you, and splat – hit yourself in the head with a little bag of ketchup that you keep in the palm of your hand.

Pad – Cheap living room in a cheap apartment in a dirt cheap city with beads on the door and a mattress of the floor. Boones Farm wine in the fridge, and a water pipe on the table. Nothing more than a place on the way to somewhere.

Commune – Group love, group dishes, group dinner, group getting stoned, group love, group showers, group trips, group hugs, group work. Oh, and group love.

Sunrise – Through a halo with dancing colors and walking with Blue and friends to a cheap diner that stays open all night.

Rock – Far out man, groove to the tunes. Let it all hang out. Dance to the music, baby. Dance.

Categories
Technology Writing

Creating C# Applications: Chapter 4

One of the most innovative features of C# is the concept of managed and unmanaged code. It is this that truly differentiates this language from its more commonly used cousins, C++ and Java.

What is unmanaged code? Well, put simply, unmanaged code is any C# code that uses pointers. Conversely, managed code could be considered to be any use of C# that doesn’t include the use of pointers. However, the concept goes beyond these simple statements.

In C++ if you want to work with a block of data, such as a large text string (anything that isn’t a simple scalar value), you must allocate the resources for the string, initialize and use the resource, and then free or deallocate it manually when you’re finished. To pass a reference to the string in a function you wouldn’t need to pass the entire string, but only a pointer to the string’s location in memory, its address. Using pointers is efficient, but one of the problems with manual allocation of resources and the use of pointers is that the developer may forget to free the resource, causing a loss of the resource commonly referred to as a memory leak. After many iterations of the code, the application’s performance can degrade, and may even stop performing.

Java eliminated this problem by eliminating the need to manually allocate and deallocate resources. The Java Virtual Machine (VM) provides the resources necessary to support the application and also provides garbage collection (defined in more detail in the next section) to clean up and free the resources after the application is finished. This approach prevents problems of lost resources but also prevents the use of pointers as the support for garbage collection and pointers are mutually exclusive.

C# eliminates the problem of having to choose between the use of pointers and the associated problems of resource loss, and automatic garbage collection but without support for pointers. The language does this by providing support for both garbage collection and pointers — if you follow rules carefully defined within the language that allows both of these seemingly incompatible capabilities.

In addition to the support for managed and unmanaged code, C# also has other unique concepts available to it based on the runtime language support provided by the Common Language Runtime (CLR), such as the WeakReference wrapper class, useful with larger objects and collections.

First, though, let’s take a closer look in the next section at why the use of pointers and garbage collection are so contradictory. If you already know this, you can skip to the section titled “Managed and Unmanaged code,” following.

Garbage Collection and Pointers

As stated earlier, in a programming language that doesn’t provide automatic management of resources, you must allocate and initialize the resource and then deallocate it when finished. However, when garbage collection is supported, you don’t have to manage the resources as the garbage collection mechanism does this for you.

Within the CLR environment, storage space for a new object is pulled from a managed heap. As a new object is created, it’s allocated space within the heap and an address is assigned to the object that references the starting address of its location in the heap. The object’s given the next available space within the heap that can contain it completely.

The amount of space in the heap assigned to the object depends on the structure of the object. A pointer is used to traverse the heap until a contiguous block of space large enough to fit the object is found.

Garbage collection is used to find objects that are no longer being used and to free up the heap space the object is occupying. In the Common Language Infrastructure (CLI) architecture, the garbage collection mechanism is triggered when the pointer used to search for an available heap location travels past the boundaries of the heap.

During the garbage collection process, the collection mechanism visits each address on the heap, checks to see if the object is still being referenced (can be reached and is therefore accessible by existing process) and then freeing up the space if the object is no longer within the scope of the existing process or application scope. The mechanism also optimizes the heap by shifting the addresses (the pointers) of existing objects to provide contiguous blocks of free space for new objects as they are allocated.

This last statement actually explains why manual creation of pointers and garbage collection are not compatible within the same development environment. You can’t create an object and reference it via a pointer to an address that could then be modified by an automatic garbage collection mechanism, thereby making your reference — the pointer — meaningless.

In addition, in this process the garbage collection mechanism must know how much space an object takes up — its structure — in order to manage it efficiently. In C++, it isn’t unusual to cast a pointer from one object/structure type to another, making garbage collection virtually impossible as the mechanism wouldn’t be able to determine the exact structure of the object, and therefore how much space is needed or occupied.

The problem of using pointers and garbage collection in one language is solved through the use of managed and unmanaged code, discussed next.

Categories
Technology Writing

Creating C# Applications: Chapter 3

When I work with more than one programming language at a time, such as Java and Perl or VB and C#, the biggest problem I have keeping the languages untangled is remembering to use a semi-colon (;) or not when ending the statement. No matter how hard I try to keep my language syntax separate, I always include the semi-colon when I shouldn’t (such as in VB), or not include it when I should (such as in C++ or Java or C#).

Regardless of the sophistication of a programming language, you must first become proficient with the operators and special characters supported in the language before advancing on its more escoteric elements, and this chapter provides coverage of C#’s operators and special character set — including the infamous semi-colon.

Arithmetic and Concatenation Characters

The simplest of the operators for C# are the arithmetic operators. The addition operator (‘+’) is used to add two numbers together, as it’s used in most other programming languages, such as C++. In addition, the + operator is an example of an overloaded operator within C#. What this means is that you can use it for adding numbers and you can also use it to add two strings together (concatenation), or add a string and a number. An overloaded operator is one that can work with different parameters, or parameter types. C# extends this capability by also translating parameters to compatible types — such as converting an integer to a string for concatenation when adding a string and an integer.

To demonstrate this capability, in the following code, the addition operator is used to add two numbers and two strings and print out the results of both operations.

using System;

class ArithTest 
{
   public static void Main() 
   {
        int i = 6;
	     string s1 = "Value";
        string s2 = " is ";

        // add two numbers
        i = i + 4;
        
        // add two strings
        s1 = s1 + s2;

        // add a string and a number
	     s2 = s1 + i;

        // output values
        Console.WriteLine(i);
        Console.WriteLine(s1);
        Console.WriteLine(s2);
      
   }
}

The result from running this application is:

10
Value is
Value is 10

The plus sign can’t convert automatically between the two operands if the result is ambiguous, or disallowed for implicit type conversion. If you try to add a double to an integer, as shown in the following line of code:

i = i + 4.15

An error results when you compile the application because you can’t implicitly convert a double to an integer.

The addition operator can also be used to perform delegate concatenation, covered in more detail in Chapter 10 “Delegates and Events”.

The addition operator is both a binary operator, meaning that there are two operands for an expression containing the operator, and a unary operator. A unary operator applies to only one operand. Applying the addition operator as a unary operator to an operand just returns the value of the operand:

nVal = i+;

The subtraction operator (‘-‘) is used to subtract one number from another, or to negate the value of an operand, as demonstrated in the following code:

using System;

class ArithTest 
{
   public static void Main() 
   {
        int i = 6;

        // subtract smaller value
        Console.WriteLine(i - 2);

        // subtract larger value
        Console.WriteLine(i - 7);

        // unary
        Console.WriteLine(-i);
      
   }
}

The following values are output when this application is run:

4
-1
-6

The subtraction operator, as with the other arithmetic operators, works with all numeric data types, such as integers, doubles, and decimals.

The multiplication operator (‘*’) multiplies two operands together, while the division operator (‘/’) divides the first number by the second. The modulus operator (‘%’) returns only the remainder after the first operand is divided by the second.

In the following code, each of the operators just described is used between two different operands, both of which are type double, and the results are printed out:

 
using System;

class ArithTest 
{
   public static void Main() 
   {
        double d1 = 4.15;
        double d2 = 1.23;

        //  multiply
        Console.WriteLine(d1 * d2);

        // divide
        Console.WriteLine(d1 / d2);

        // modulus
        Console.WriteLine(d1 % d2);
      
   }
}

The output of this application is:

5.1045
3.3739837398374
0.46 

The multiplication operator is another example of an overloaded operator; it’s also used when declaring pointers, and de-referencing these pointers to access the pointer values as you’ll see in the section “Membership, Array, and Casting Operators” later in this chapter.

With all arithmetic operations, if an overflow occurs because the result is too large for the allowed range of the data type, an OverflowException is thrown if one of the following conditions is met:

  • The checked compiler option is used and
  • The data types of the operands are integers (int, long, or ulong)
  • Or the operands are decimals

Use the checked compiler option when compiling the application in order to generate an OverflowException with integers. This compiler option turns on arithmetic checking:

csc /checked test.cs

In the following C# application, the OverflowException is captured and the error message printed out:

 
using System;

class ArithTest 
{
   public static void Main() 
   {
        int i = 2147148000;
        int i2 = 32000;

        try 
        {
           //  multiply
           i = i * i2;
           Console.WriteLine(i);
        }
        catch (OverflowException e)
        {
           Console.WriteLine("{0}", e);
        }
      
   }
}

When the application is compiled without the checked option, the following output results:

2144165888

Since integer arithmetic checking isn’t turned on, the significant high-order bits are discarded, and the resulting value is returned. When checking is turned on with the check compiler option, the following exception message is printed out:

System.OverflowException: An exception of type 
   System.OverflowException was thrown.
   at ArithTest.Main() 

The OverflowException will occur with all decimal values when the value is too large for the decimal format.

Float and double values don’t generate an overflow exception. Following the IEEE 754 arithmetic specification, values too large for the variable are set to infinity. If a value can’t be defined as a numeric, it is given the constant NaN — meaning Not a Number.

Arithmetic and concatenation operators are useful for converting values, but have no impact on an application’s process. In addition, each of the variables modified by the operators in this section first had to be assigned. Program flow controlled through the use of logical and condition operators and the assignment operators are discussed next.