Saturday, February 20, 2010

C++: Forgotten Syntax

Well I do realized this post is later than my usual time (I usually have a post done Thursday or Friday, and schedule to post it at 3:00PM on Saturday.. In case you weren't aware, that's what I do) But I will still post nonetheless. This week I'll be giving a couple tips on C++, although you can't really depend on MY tips unless you yourself are more of a beginner than me. Personally, I've been with C++ for almost a year now, I took a class at a junior college, and I've been going through C++ Primer in my spare time. You can at least trust my opinions because I have more experience than about 50% of the other C++ newbies who learn how to write a hello world program and call it genius.

I want to call attention to a few things in C++ that personally annoy me, the things you and I, and millions of other C++ programmers commonly forget, resulting in a million compile-time errors. First of all, the ever-so-beloved semi-colon. Who would have thought that one small semi-colon has the power to completely change the output of a program? Secondly, brackets. It is common to forget the ending bracket, but how many times have you forgotten both the beginning AND ending brackets? Also, what are the unseen consequences of forgetting a "delete" command?

Well, the most common problem is probably forgetting semi-colons. This is usually quite easy to do, especially after typing out a long statement, but with practice, it'll begin to be similar to a second nature. There's really not much I can say about semi-colons.

In my opinion, the most annoying problem, and second most common (to semi-colons) problem is forgetting brackets. Every once in a while, I think, "You know, I don't need brackets here, it's just one line." But later I go back to that for loop or if statement or whatever, and I add a couple extra lines for some reason, but I forget those damn brackets!


The above picture is a good example of being able to write an if..else statement without the use of brackets. Now notice the difference the number of lines makes:

This code looks right doesn't it? Wrong, it is tricky to tell because of the indention (which I had to add myself, the editor wouldn't for me). A lot of times, if you are sure you are going to be typing in more than one line, and probably most of the time no matter what, you are in a habit of adding brackets, but when you go back to add a line or two, you totally forget about the brackets. Again, with practice, most people get better at remembering these things.

The third, and most important, and least noticeable problem is forgetting to deallocate memory, which is why you really shouldn't allocate pointers and that crap in the first place unless you absolutely have to. Has anyone here every ran a program, and then ran it again later without restarting... and then ran it again later, and again, without restarting? Well after doing so, did you notice your computer was extremely slow? So you open up task manager and kill every last process you can think of, and then you are down to 25 processes yet your memory usage is still at 93%. What's happened is called a memory leak, and in short... using new without using delete causes this. And even larger, using a million news without deletes causes this in a large scale.

Let's suppose you do remember your delete statements, you allocate memory for a bunch of arrays, array1, array2, array3, etc. and then you make absolutely sure you remember to delete them, so you have delete array1; delete array2; delete array3;. Well sorry to break it to you, but that's not gonna cut it. That's going to delete the starting pointer (the pointer pointing to array1[0], and array2[0], and the rest) but it won't delete the entire array. To fix this you must use delete [] array1;

So there you have it, now you will be extra careful to remember those semi-colons and brackets. And hopefully you don't have to think about deallocating too many memory slots.. Besides, allocating them in the first place is already some fairly advanced stuff that you really don't need to be fooling with unless you know what you are doing. Typically I don't even think of allocating memory except for as a last resort... which means I haven't ever actually done it in my own project where I'm not following a guide that tells me to use new and delete!

Have fun!

No comments:

Post a Comment