Parentheses (Is that how the plural form is spelled? It looks weird.) are destroying me today! It's like Left Parenthesis said, "Yo, Right Parenthesis, wanna screw with nfinite today?"
And Right Parenthesis replied, "Hell yeah!"
Ok, so they only screwed with me twice. They even had the nerve to call up Mr. Semicolon to aid them in giving me grief. What's all this about you may be wondering? I had a bad time coding today. I needed to change an implementation of a linked list into a circular buffer. (For those who know programming, you don't need those links, so I'm not trying to be condescending; just trying to educate). We started having memory leaks due to allocation and deallocation of each node. We knew exactly what was happening and why the leaks where occuring, but there was nothing we could do without restructuring a lot. To protect my employer's intellectual property, I shall now generate an extended analogy to the issue we were having - with COOKIES!
Let's say we have a queue of cookies. If we create this queue as a linked list, each time we add a cookie to this queue, we have to make a box to hold it. It's then placed on the conveyor belt and sent on it's way to YOU (yes, you, this is getting personal now. You are getting fed cookies, you should be happy!). Everytime you eat a cookie, you dispose of the box by means of recycling.
Now, sometimes you need to stop this conveyor belt for various reason (maybe you're too full and need a break from cookies... weakling). When this happens, a GIGANTIC MONSTRUOUS EPIC PENDULUM OF COOKIE STOPPING swings down and blocks the cookies. The problem now is that this GIGANTIC MONSTRUOUS EPIC PENDULUM OF COOKIE STOPPING occasionally swipes a box of cookies - resulting in the box to go flying into the depths of nowhere, never to be found. THIS IS BAD! We do not want cookies to get lost! (This was the memory leak that was occuring).
So instead, it was my task to go in and create a circular buffer. You can think of a circular buffer as an escalator (the linked list was the conveyor belt with boxes). For our purposes, the escalator is extremely sanitary. Each step of the escalator is a platform for a cookie. In this way, we don't need to make boxes for the cookies anymore. You are at the top eating the cookies as they come up, and the cookies are getting placed on each step starting at the bottom. The steps can be reused after they are cleaned of their cookies by you. Now, when the GIGANTIC MONSTRUOUS EPIC PENDULUM OF COOKIE STOPPING comes down, the cookie is small enough to avoid being swatted away. (See, the box was getting hit before, but since the cookie was in the box... ok my analogy is starting to break down here... but whatever! just know that there are no more memory/cookie leak problems with this approach).
Now that you understand the importance of ensuring no cookies are lost, I move on to coding the escalator - which is where Mr. and Mrs. Parenthesis start to toy with me. (They must not like cookies...). I hope I don't lose any of you at this point. It's gonna get a little more technical than eating cookies. Again, I am simply going to tailor this code to the cookie example. Take the following code:
That's basically the pertinent stuff. That should be straightforward except for (add_cookie_step+1)&COOKIES
line 12: error: expected a ")"
I could not find a problem with it for the longest time. ALL THE PARENTHESES ARE MATCHED! There are the same number of left as right parentheses! I went to a fellow cookie-meister and he took a look at it and thought it was screwy too! What was the problem? If you found it right away, congrats. It's a lot harder when the code is ~1000 lines long. Apparently, Mrs. Right Parenthesis was having an affair with Mr. Semicolon. COOKIES_MODULO was defined as (MAX_COOKIES-1); This means that, to the compiler, line 12 said:
while ( (add_cookie_step+1)&(MAX
That semicolon in there is a big no-no. It seems it didn't care about the following line though.
remove_cookie_step = (remove_cookie_step+1)&(MAX
Fine, fine, whatever. Problem solved.
I was going to explain the second problem that I had, but I would need to extend my analogy even more. I don't want to do that anymore. Just know that the code did not do what I expected it to do. I forgot to add that there can be multiple escalators. What happened, however, was that you ate only cookies on Escalator #0, but none of the others! "WHYYYY WERE YOU ACTING UP!" I asked myself.
if ( (Cookie->EscalatorNumber)
This line basically asks if the cookie is edible by looking at a designator of EdibleCookies (We can't eat bad cookies. We don't want to eat bad cookies.) Basically some order of operations mumbo jumbo happened. This line needed to be
if ( ((Cookie->EscalatorNumber)
So when I didn't need parentheses, the compiler complained at me. But when I needed them, it didn't say a single thing.
I Hate You Mr. and Mrs. Parenthesis. Oh and Mr. Semicolon too (because I always seem to forget to add you to the end of lines, and then Mr. Compiler throws fifty million errors for the lines that proceed /rant).
Ok, I'm done, I hope you want cookies now. I sure do, but I don't have any. Sad day. Well, maybe Rainy has some cookies. She better, it's like a house-warming party. Since her blog is "reopening" (or something) she needs to give us cookies.
I know I said there would be pictures this post, but this post has gotten long. So I guess ya'll will just have to wait until next time! Stay tuned for sexy video cards, sexy keyboards, and sexy cars.
Stay sexy!
No comments:
Post a Comment