Showing posts with label computers. Show all posts
Showing posts with label computers. Show all posts

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!

Saturday, February 6, 2010

Synergy (software) review

Well last weekend I found this cool stuff called Synergy, and so I decided I'd write a review on it. It's really a great piece of software if you have more than one computer; it allows you to control multiple computers with one mouse. If you know me, you might know that I have a laptop with it's screen plus a second monitor, and I also have a desktop with a nice monitor, and I'm a bit of a multi-tasker in the sense that as I write this review, I also have a browser open with about three other reviews on this specific software, I've got Trillian open, I've got MedieMonkey open playing music, I've got Notepad++--which is the best notepad replacement ever (next software review?)--with 6 documents open and I've also got Gmail and Google Calendar open. For some reason I have an English paper and two other OpenOffice documents (including this one) and I've got a virus scan running on my laptop.. You know? When I put it in words, it seems like I've got every program installed on my computer open right now, but anyways, the point is: I'm kind of a multitasker.
Well multitasking, as most people is hard without one of two things: Alt-Tab and multiple monitors. The multiple monitors part is different, but it makes things a whole lot easier, because at one time, I can see this document, my calendar, and in a tab a thing that tells me I have one unread message (wonder who that's from.. oh well I'll check it after this) and I also have a quick play/pause button for my music. When I'm writing research papers, it sucks having to alt-tab between browser, read a little, alt-tab back, type a little, etc.--It's nice when I can just turn my head a little, read a little, turn my head BACK, type a little.
Well now that I have three total monitors, it also means I have two keyboards and two mice (mouses?) and half the time, I'll start typing on one keyboard and then I realize I'm typing on THE WRONG KEYBOARD! Yes, once I even shut down the wrong computer using keyboard shortcuts and ended up losing about half an hour's worth of work opening all the applications I was planning to use! <--That's actually an exaggeration. With Synergy I have solutions to every problem every presented in this universe, in fact I considered nicknaming it 42. You know, the answer to the universe and everything... oh whatever it was a joke, anyways, with Synergy, I can move my mouse from my desktop to my laptop AND BACK!!! That part totally blew my mind away. Then I realized I could copy-and-paste from one computer and back (goodbye netpaste, you are no longer needed) and I could move DOCUMENTS from ONE COMPUTER and back!!! Wait let me test that, just a second.. ok you can't move documents from one computer and back, and that is why you use shared folders. But I thought, this is the sickest thing ever to happen to my desktop and laptop, it's like they've become married, they have joined souls and become one. Well now I have seen a problem: I can't use alt-tab to switch between firefox on my laptop and openoffice on my desktop. Well simple, Synergy has shortcuts which let you switch the keyboard/mouse between computers, and problem solved.
Well Synergy is a really great piece of software, it's been very helpful. I'd give it a 5/5 easily. I think you can connect more than just two machines together too, you could probably have 5 or even 10 computers together :D Oh and the cool thing, do you have two computers because you like Linux and Windows? Well no problem, or maybe you like Mac and Linux (such a Windows-hater!) well no problem! Synergy works across multiple operating systems at the same time :) So in conclusion: do you have two computers? Good, get Synergy. Bye.

Wednesday, November 25, 2009

Windows 7: Syncing files between networked computers

Do you have two computers? Okay, if you do, have you ever created a file on one computer then moved it over to the other computer to edit it? Then when you were emailing the completed file, you forgot to copy the file back to your main computer, so you emailed a blank file on accident! I did the same thing once, I didn't exactly turn in a blank file, but I turned in the wrong version, so I had an incomplete project. When I got my grade back on this (it was a computer science project) luckily I wasn't missing a whole lot so I got an 83/100 but it was horrible!

Well the good new is, if you ever find yourself in the same position, and you have Windows 7, I have found a solution for you. My first attempt at a solution was to just use a USB Key, and yes that does work perfectly well but if you are like me, and you use two computers at the same time, then you have a problem because all of your files are on one USB Key so you would need two copies of each file. So I decided I would stick my USB key into my desktop machine, then just share it over the network and map it on my laptop. This also works perfectly well until you run into the next problem: what if you aren't connected to the same network as your desktop and you left your USB key at home? Yes I do realize that I'm either very stupid or a complete nerd at this point with all the problems I've run into, but I'm one of those people that can't live without whatever conveniences are available.

My final solution comes down to this, and I had no idea this was a feature of Windows 7, but if you go to Control Panel, then click the little arrow next to Control Panel in the address bar, the first option should be All Control Panel Items. If you scroll down a little bit you'll find Sync Center. With this you can set up network drives to sync automatically so they can be available offline. Now, if you have two computers, one is running Windows 7, you accidentally leave your USB key at home in your desktop while you are taking your laptop to school, and you need to work on a project you left on your USB key, you can do so without trouble! Hopefully this will benefit more than just me.

Friday, November 20, 2009

using namespace std; in C++

Lately I have been into C++ quite a bit, I've been reading some books and this Fall 2009 semester I'm taking a Computer Science class at Red Rocks, all about C++. The language, and the way that it works cannot function without certain standards, otherwise you get something like HTML where if you forget to include a closing tag (like but no ) then it still works, but if you forget those tags, somewhere down the line you will have a huge problem and you can't figure out WHAT the heck is going on. Well C++ has it's standards, something called ISO http://en.wikipedia.org/wiki/ISO/IEC_14882#Language_standard but this set of coding standards doesn't include certain things that could be vital to your program running correctly or not.

A particular problem that I have found, is the statement “using namespace std;” What this does is opens up the entire scope of code you place it in to the standard library, and it restricts the names you are possibly able to use. For example, did you ever know that copy, list, sort, and max are all std names? Once you include the 'using namespace std;' at the top of your file, you've added all of those functions to the global namespace and you can't ever make a variable or function with that name anywhere in your code. So this doesn't seem so bad, worst case scenario you just go and change those names. It may take you a little while (a long time if your code is long at all) to find out what went wrong, and you might not receive an error message, but you can just go back and use different names. So now you go get a library using a different namespace, called nonstd so at the top of your source file you have 'using namespace std;' and 'using namespace nonstd;' We've already deduced that the std namespace has a function called copy, but this nonstd namespace also has a function called copy. It will be hell to find out what is going wrong.. and further more how will you fix it?

Okay, so it's true that anyone who is able to write their own library and publish it so you can use it will be careful NOT to ever use any names that conflict with the std namespace, but that's what namespaces are for! One useful thing about namespaces is that two different libraries can use the same name without being conflicting. The thing is the names will conflict if you use the 'using namespace std;' declaration in your source file. While I was researching this topic, I found this: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c Here, sbi answered a question, scroll down a little to see it: “It took most of us very few weeks to get to used to write the prefix and after a few more weeks most of us even agreed that it actually made the code more readable.” Here he was talking about a project where it was agreed that using directives would not be used at all, and this is the result of that.

So what is the solution to not using the using-directives? Well the simple way is to type std:: in front of everything, as so: std::cout, std::cin, std::endl, etc. But that takes a little effort. Better than using namespace is using std::cout, which solves a couple of the problems I talked about previously. However, if you do run across two namespaces with the same name, and you need to use both names in your code, then you will have to type the namespace::function.

So as a short conclusion, don't use 'using namespace std;' in your code, it has some bad effects in the long run. In small projects for a beginning C++ class at school it's probably okay since you usually won't write any excessively long source files with lots of naming conflicts.

Friday, October 23, 2009

My adventures in switching from Windows to Ubuntu Part 1

So this weekend I decided I'd finally switch over to Ubuntu for most daily computing tasks and I'll tell you how that's been :)

Thursday night I stuck the CD in and installed Ubuntu 8.04 because it was LTS. Well bad choice because that edition is now over a year old and my monitor and graphics card are... less than a year old, so I was running 800x600 resolution the entire time. Friday afternoon I just went in and installed 9.04 instead since that's the newest currently available. Instantly, 9.04 already had my graphics card and monitor recognized so I could run 1600x900 immediately, and I installed my graphics card drivers in a breeze.

Some things to note, Ubuntu isn't like Windows at all (obvious) but in some ways is better. I really like Windows a lot because I know where everything is at and I have my own little shortcuts set up for optimum speed in my daily computing. There are some minor cracks in my little system though, I run into out-of-date software constantly, and every once in a while there's that darn security issue. I noticed instantly that updating software with Ubuntu is very easy. I didn't even have to open Firefox to download my graphics card drivers! Well I had to open firefox to find out HOW to do it without opening firefox but now I'll be able to update those drivers with a click of a button, as with all the other software I installed.

Next weekend, hopefully I'll be able to get my sound working. I know I should probably (definitely) get a legit sound card but with Ubuntu, you make work what you have with as little money as possible :D