Get Back On The Penguin

2010/03/15

So, I am back in that place where I don’t know what to do. I have a good list going, though, of what is NOT helpful:

So, what might be helpful?

  • Open the source code
  • Find the bug list
  • Try to repro the bug
  • Make a new to-do list
  • Make some tea

OK, tea is pri 1.

>>>

Sweet! Now I’ve done everything on both lists, except make a new to-do list. So:

  • Try to find where “search” is written and called
  • Try to step through code… using my IDE that does not step through code. This may be the first time in my life I’ve used this expression, but: d’oh.

Finding the spot where “search” is called…

>>>

Um… O Gods. What?

>>>

No, seriously… what?!

>>>

Holy mother of God… I feel really, really dumb.

>>>

So! “The strcasecmp() function compares the two strings s1 and s2, ignoring the case of the characters,” says you. And you claim that “The variable optind is the index of the next element to be processed in argv.” And you go so far as imply–no, state quite baldly–that “[t]here are at least two arguments to main: argc and argv. The first of these is a count of the arguments supplied to the program and the second is an array of pointers to the strings which are those arguments.”

I had never actually seen argc and argv used! I thought they were just window dressing.

So! We are checking to see whether the string passed in equals some other string, even if we pass in many many things as arguments. We check one at a time.

>>>

Yes, but that doesn’t explain why all these people on the ‘tubes are running around saying:

if(!strcasecmp(doWhatVar, “Do This Thing”)

{

doThisThing();

}

That seems like… the opposite of what it should do.

Is 0 the same as not something? Oh god. How could I be so ignorant?

>>>

This is the part where I remind myself that I am still learning, don’t expect myself to know everything, etc., etc., etc. And maybe pour myself some beer. Because if I’m going to be ashamed and irrationally afraid that I’ll never get a job, I might as well do it with a tasty beverage.

>>>

Why are there slashes everywhere? Do I know this language at all?

>>>

Oh… slashes are line continuations inside a giant macro. Haven’t used macros before. That explains a lot!

>>>

Now there’s the part where I wonder if the answer is to soldier on and Get Back On The Penguin, as it were, or take a break and come back all fresh-faced and dewy. If it weren’t 11 o’clock at night, I’d go for a run. Maybe instead I’ll do some pushups. And ask my ever-patient roommate for help. And then maybe… maybe sleep.

Advertisements

CPP Reunion

2010/02/02

Wow–I missed coding! I’ve been mucking around with getting stuff to work in Linux for so many days, I forgot how great it is to write for loops and iterate and deal with input and make sexy output and OH but it’s good to be back! I don’t even have an IDE working (and haven’t learned about make files) on the Linux machine, so I haven’t touched code in over a week. I will not make this mistake again!

This is one very positive attribute of working with Linux:  it makes C++ seem warm, fuzzy, and exceptionally well documented. I will never again complain about obscure function calls.

I’m sad to tear myself away–but some friends’ moving date was unexpectedly pushed up, so I will buy some cookies and bus on over to provide some good old manual labor.

Aww, C++. I just want to wrap myself around you and never let go.


Training Wheels

2009/12/10

I’m frustrated today because, yet again, I’m trying to get “add” to work on my Red-Black Tree.

And really, the difficulty has nothing to do with the logic of my algorithm. My pseudo-code pseudo-works. The problem is learning the real, true behaviors of pointers and references, because my recursive call loses half my tree.

I want to swap out one node for another without referencing its parent and without having its parent lose track of it. My dog-eared, broken-spine Data Structures and Algorithm Analysis in C++ told me, in the section on AVL trees, about a little trick with reference pointers to do just that. I remember reading the section, thinking “Ah, that will work!” and closing the book. And now I really, really don’t want to open the book again to double check my understanding. The misbehavior of my function falls directly out of the properties of references, and it fails because I don’t understand them on a fundamental level. Recopying the book’s algorithm will NOT help me grasp reference pointers any better.

This my current issue:

template<class K, class T> void RBTree<K,T>::_rotateCounterClockwise(RBNode<K,T> *&grandParent)
{
RBNode<K,T>*& parent = grandParent->childRight;
parent->childRight->color = RBNode<K,T>::black;
grandParent->childRight = parent->childLeft; //??
parent->childLeft = grandParent;
grandParent = parent;
}

This changes the “parent” to the location of parent->childLeft, which implies that the reference to parent is just looking exactly at the location of grandparent->childRight. Which is what I told it to do… But when I just have a node pointer (instead of a node pointer reference), the grandparent is looking right where it’s supposed to–but the great-grandparent apparently decides to disown it during some part of the process. Hence the losing half my tree.

The frustrating thing is that these are very, very basic mistakes. It’s like a third grader being very confused about why 5 times 3 is the same as 3 times 5. (Although, come to think of it, I don’t think I could prove the transitive property right now, either.) But it has to do with the very basic functioning of small parts, and I will not give up until I get it.

Maybe, instead of trying to guess where to draw my arrows and boxes representing memory locations, I’ll curl up with Stoustrup for a bit, and let him spin some yarns about references.

>>>>>>

Can’t find my Stoustrup book, but have been kicking around new ideas about my code. Maybe the problem is that I’m passing the grandparent node directly, rather than as greatGrandParent->child.

AH it is so tempting to keep stabbing around in the dark! I am going to wipe clean a new white board, write out my memory addresses and step through line by line. I really don’t want to just slap code together until something seems to work; I have to know why it works and THEN build up.

I’m glad we had that talk.

>>>

Oh but now, in order for this to work on my whole tree, I’ll need a null header. And null headers… I’m sorry, but they feel like hacks to me in a way that special-casing the first element does not. But I think… if pointer pointers/ reference pointers work the way I think they do (which is probably not right, given all current evidence!), the root pointer will still point to the right thing. Because it’s just a node pointer, the way the rest of the links are node pointers. No, but if I change to needing great-grandparents, it won’t work anymore!

What was that about clearing a whiteboard and drawing it all out? Let’s start there.

>>>

…and I get distracted by browsing job boards, and an internship posting says “2 years of computer science degree completed–no other degrees allowed”. And I can choose how I react to this! I can be annoyed at the injustice of it all (who says CS majors are the only ones who can code?), or I can move on by focusing on improving my skills. I’m a completely nontraditional applicant, and I can’t expect traditional (or, rather, stereotypical; do we even know people who get jobs anymore through job boards?) avenues of employment to work for me at all.

FOCUS!

>>>

So my code was perfect–except, way at the top, I called “add” on a pointer instead of a pointer reference, so my beautiful references failed all the way down. This doesn’t change the fact that I need some more Stroustrup so I can predict why my references fail and quickly solve the problem–but this is a good lesson. When something fails further down, check back up the stack to make sure you’re passing things right!