Peace For All

August 29, 2006

Power comes at a price

Filed under: Programming — Devlin Bentley @ 2:47 pm

Going through the Python tutorial again:

Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key.

Well OK, that sounds reasonable.  You can’t use an object as a key if it keeps changing, right?

Oh wait, I can in Java.  I define a Hash function, done.

I have only had to use this once, and it was for a rather baroque situation, but it was rather cool that I could do it, and fairly simple as well.  Just pick which non-mutable instance variables of the class that should be used to generate a hash value, and shove them into a hash function.

August 28, 2006

What is the difference between these two methods of calling?

Filed under: Programming, technology — Devlin Bentley @ 10:06 am

Edit: I have added an explation of (the more typical) Call by Value vs. Call by Reference since everybody who comes to this page seems to be looking for it. 🙂

Call by Value VS Call by Ref explanation:

Variables all have a value. For instance:

int a = 5

Here, a has the value of 5.

Now realize something. THERE IS NO VARIABLE CALLED a. The second you hit compile (build), that variable vanishes. What really happen is that when your program is run, 32 bits (4 bytes)[1] of RAM inside the computer is allocated, and the value 5 is stored there. The compiler automatically figures out what address of memory to use.

From the computers point of view, it may very well decide that the memory address 4c00h is where the variable a always goes, and when that above line of code loads, it may looks something like this:

MOV 4C00h 5

4c00h is an address in memory, and 5 is the value that is placed there. mov is the assembly[2] command to move data into memory (amongst other things).

The compiler replaces all your usages of the variable a with the memory address 4c00h. The point here is that variable names are just a label that is there so we don’t have to try and remember memory address numbers.

This is a BIG deal. You have to separate “Variables” from the concept of “Values”. They are two different things. Remember this: A variable is just a way for US, the humans, to LABEL SOMETHING that is stored inside the computer.

You will notice that a relates directly to a a number in this case. Hey, cool. That is easy to understand.

There is no rule saying that variables have to always label just numbers or string. Variable can label darn nearly anything.

Think for a minute. What are functions? They are a label we apply to a bunch of code that we want run. We want that code run, type in the function name, the compiler goes and inserts the instructions needed to make it so that the function is run.

This is also pretty simple. Here, variables just label functions. Not too complicated.

What if we want to label something more complex though? Like say an Array? Arrays can have many values inside of them, and they can take up a lot of memory.

int[5] hoursWorked = [8, 9, 7, 8, 8]

The compiler cannot just go through and replace all occurances of hoursWorked with a single address of memory. Now, hoursWorked is taking up a lot of addresses in memory!

Thankfully, (I am not going to explain this right now. 🙂 ) arrays are setup in memory so that all the values are right next to each other. This means that the compiler goes

MOV 4C01h 8
MOV 4C02h 9
MOV 4C03h 7
MOV 4C04h 8
MOV 4C05h 8

If you ignore the h at the end there (it is just shorthand for something else, if you know what, great! If you don’t don’t worry about it. 😉 ) you can see that the numbers are increasing. 1, 2, 3, 4, 5. This means that if you could look at the values stored in RAM on your computer, you would see the numbers 8, 9, 7, 8, 8, all lined up right next to each other.

Here is the cool part. We ONLY need to have the address of the first element in the array. 4C01h If we know that address, we can find the other ones just by walking forward, remember, they are all right next to each other!

So what does any of this have to do with passing values in functions? Well, quite a bit!

Lets pretend we have the following function:


int calculateSalary( int numberOfDaysWorked, int[] hoursWorkedEachDay )
{
  int totalPay = 0;
  int hourlyPay = 7;  for( int i = 0; i < numberofDaysWorked; i++ )
{
totalPay = totalPay + (hourlyPay * hoursWorkedEachDay[i]);
}

  return totalPay;
}

This function simply goes through the array of how many hours you worked each day, multiplies the hours of each day by how much you earn each hour (in this case, $7), and adds that days pay to the weeks total.  Notice if you only worked 4 days, you pass in numberofDaysWorked as 4, and you only read the first 4 values from the array.

The function most likely will get called like this:

int totalPay = 0;
totalPay = calculateSalary(a, hoursWorked);

I use a and hoursWorked from above.

The variable a is pretty simple.  The compiler can just read whatever value is at the memory address a refers too, in this case the memory address is  4C00h and the value is 5.  The computer just reads the value at memory address 4C00h and passes it to the function calculateSalary.  Cool.  Wenow have

 totalPay = calculateSalary(5, hoursWorked);

What about the array hoursWorked?  The compiler cannot just copy a value here, it is an array!

So what happens is that the memory address of the array is passed instead.  It ends up looking like

totalPay = calculateSalary(5, 4C00h);

4C00h is a reference to where we can find the array at.  If you look up the definition of reference, it says that:

a reference is something that refers or points to something else

en.wikipedia.org/wiki/Reference

Well, that is exactly what 4Cooh does, it POINTS to where the array hoursWorked is at in memory.

In languages like C and C++, you say that something is a pointer by putting the character * in front of it.  Newer languages (such as Java and C#) handle a lot of this for you, and when you pass an array, you don’t have to worry about syntax so much.

Oh, and you now understand pointers too.  🙂  I have work to do, my apologizes for the lack of spell checking.

[1] This is a vast simplification and may not be true on all computers. INTs can be of any number of sizes, PLEASE DO NOT RELY UPON INTS BEING 32BITS IN SIZE. Especially not because I said so. :)[2] Assembly is a human readable represenation of the language that your computer understands, actual machine code. Machine code is all in binary and consists of 0’s and 1’s, and is even harder to understand! And yes, this example is greatly simplified.


Original post is below, you can safely ignore it, someone else explained it to me. 🙂Call by Value (where the value is always an object reference, not the value of the object)

compared to

Call by Reference

They end up being functionally the same, don’t they?

This is in reference to Python, which then says at the bottom of the tutorial page:

call by object reference

THEN WHY did they just say Call by Value?

Call By Val

Call By Ref

Then there is C’s awesomeness of ALWAYS Call By Val, but we’ll pass the value of a memory address along if ya want cuz hey why not?

I am a proponent for Call By Ref whenever you have objects, since when I am doing OO I want abstraction, but when I am doing C I am in a mood for understanding, comprehension, and I can feel the entire machine surrounding me. It is awesome, it is fast, it is efficient, my code has less bugs, and damnit, I WANT that power because it feels good.

August 25, 2006

www.technorati.com verification post

Filed under: Life in general — Devlin Bentley @ 1:14 pm

<a href=”http://www.technorati.com/claim/pdisyd64qu&#8221; rel=”me”>Technorati Profile</a>

Programming outside of OO seems a bit weird now

Filed under: Programming — Devlin Bentley @ 1:05 pm

It seems strange to be in an environment where functions just “exist” floating out there in the ether so to speak.

It does not seem weird in C, because what C gives you, you pretty much expect to have as a programmer.   But other languages have these huge functions that do not seem to be attatched to an object, then another class of functions that are.   Then I as a programmer have to remember which is which.

Not that having a general “Utility” class with a ton of static methods is any better.

August 21, 2006

The Annoying aspect of blogging about acne treatments

Filed under: acne, Life in general — Devlin Bentley @ 12:10 pm

If you check other blogs out there that deal with acne, almost every single one of them is a spam blog that is selling something.

Thus it turns out there are almost NO real blogs about Acne, which makes finding anybody to link to pain!  Without site links going out, track backs and the like, nobody else knows this site exists.  Bleck!

August 18, 2006

Site Update

Filed under: Life in general — Devlin Bentley @ 12:52 pm

WordPress.com finally has some decent variable width themes available, and I have updated the site to use one of them. Not quite the look I am going for, but fixed width sites drive me nuts!

Also I organized the side bars, social stuff on left, navigation on the right. I know it is supposed to be the other way around, but the MeeboMe plugin breaks FireFox if it is on the right side.

Expect two more posts coming soon, if not later today, then sometime during the week end. I am writing them up as drafts and saving them now, creating a backlog of content to be posted.

Oh, and somebody has stolen my content! I am not going to give them the benefit of a link though.

On a separate note, I really need to spread my name around on these pages more, my old Blogspot account (which ironically now has all the features I switched over to WordPress for!) had a decent Google PageRank for various terms related to me.

August 11, 2006

Treating Your Acne 3: The causes of Acne

Filed under: acne, health — Devlin Bentley @ 10:34 am

First, realize that there are still many theories about what causes acne. We know what bacterium is responsible for it, but beyond that, new theories are coming out regularly. With that said, this article will provide you with an overview of what causes acne just in terms of what is happening to your skin, how those things actually happen to your skin is a topic left for the doctors to figure out.

Acne is caused by overactive bacterial growth, the bacteria is very common and exists on everybody’s face. This is why you cannot (with a few exceptions!) “cure” acne; assuming you ever could kill all the acne bacteria on your skin, the second you hugged, kissed, or came near any other person, the bacteria would appear on your skin again immediately.

The question you want to know the answer to of course is “Why is this bacteria growing so fast on my face?” Well, it has to do with oil production. Amongst other things. Honestly, theories exist, and they are finding more reasons every day. In general, oily skin and a combination of other factors specific to you and your skin, makes your face (or other body parts) more predisposed to acne than that of other people.

That is really all there is too it. What causes your face to produce all that oil is the topic of much discussion, and my next article will deal with how you can properly treat your face to avoid sending messages to your body that shout out “make more oil!”

August 7, 2006

Treating Your Acne Part 2: An Overview of Topical Solutions

Filed under: acne, health — Devlin Bentley @ 4:07 pm

This entry provides a brief overview of how topical acne treatments work.

There are two ways to directly treat acne:  antibiotics to kill acne bacteria, or topical chemical solutions.

Because acne is actually present on everyone’s face normally there is no chance of “curing” it (with a few exceptions), so antibiotics have to be continuously used until you outgrow acne.  Since Long term antibiotic usage can have nasty side effects, many people are just not comfortable using antibiotics for acne.  Because of this, topical chemical solutions are by the most commonly chosen acne treatment.

The next obvious question is: how do topical solutions work? Well, they work by using one or more of the following chemicals: salicylic acid, glycolic acid, or the bactericidal chemical benzoyl peroxide.

As mentioned previously, acne is caused by the overactive growth of a bacterium that is very common and exists on everybody’s face. Topical treatments for acne tend to deal with removing oil, reducing the clogging effects that oil has on your pours, or killing the acne bacteria. Salicylic and glycolic acid work to unclog your pours and are often combined with other chemicals that help to remove excess facial oil, effectively giving the acne bacteria less “food” to eat off of. Benzoyl peroxide treatments focus on killing the bacteria directly.

If you have mild acne, either one of the two above approaches may work, but for more serious forms of acne, you really need to treat all three sources (oil, clogged pours, and the bacteria) at the same time. That is the topic of discussion for a future entry about Proactive Solution.

Blog at WordPress.com.