Archive

Archive for March, 2009

Arrays and sizeof()

March 29th, 2009 Chris No comments

I consider myself pretty savvy in C/C++ land.  However, I’ve just been surprised.  I just found an interesting little caveat of pointer arithmetic regarding the unary operator sizeof().

sizeof(), for those who may not be familiar with it, is a unary operator (even though it looks like a function call) that returns, creatively enough, the size of it’s operand, in bytes.  On a given architecture, with a given compiler, a sizeof() on any POD type, will be constant.  On my 686 with GCC 4.3.2, sizeof(char) will always return 1, because on said platform, a char always occupies exactly 1 byte of memory.  sizeof(int) returns 4.  sizeof(void*) returns 4.  And on and on.

However, just now, I found out that if you give sizeof() an array name, the behavior is not what I expected.  In the C family, array names are nothing more than constant pointers to the first element of the array.  Thus, I would expect that passing a const char* (an old school string) I would get back the size of an address on my system, i.e., 4 bytes.  This is simply untrue.  In fact, when I used sizeof() on my null-terminated character array, it reported the string size!

Looking into the matter, I’m now told (by various manual pages and language specifications) that, when passed an array, sizeof() returns the total alloted storage for the entire array.  So, a sizeof() on a string like “foobar” would return a 7.  6 bytes for the visible characters, plus 1 for the null.  And this is for any array, not just an array of char.

If you didn’t know, now you do.  And if you did, then forgive me as I catch up to the crowd.

Categories: Uncategorized Tags:

Visual Studio 2008

March 20th, 2009 Chris No comments

In the general goings-on of my college career, I was approached for a job.  A straightforward web admin/programming gig for a grant-funded research project here on campus for a project that needs a webfront.

A long story made short, there is a client application being written for another piece of the project in C#.  And, because the website needs to communicate with that client, I (some would say foolishly) agreed to write a class to handle those transactions, since I (obviously) know how the front-end website and associated services are configured.

So on I went to install Microsoft Visual Studio 2008 Professional Edition.  It was free due to a wonderful (even if for all the wrong reasons) project set up by Microsoft called DreamSpark.  If you can prove you’re a current student (High School or College), then you can download and be granted student licenses for all sorts of up-to-date Microsoft products, including various versions of Visual Studio, SQL Server, Microsoft Server, Virtual PC, etc etc etc.

vs2008

Now, personally, I don’t care a lot for IDE’s.  Historically a Unix and web programmer, I do everything I need inside of Gedit (or emacs or vim, when the need arises), including source editing, building makefiles, etc.  I’m the guy that uses his “plain little text editor,” and writes every line of code, compiler statement, and linker directive out by hand.

I have to say though. On behalf of an Open Source aficionado, I’m actually beginning to enjoy the experience. I’ve started a (very small scale) personal project with C#, and I really only have a few high-level complaints. Firstly, the application is so mind-bogglingly complex, it can sometimes take minutes to find a simple option or directive in the sprawling menus and drop-downs. Secondly, the documentation isn’t wonderful. MSDN. Neither the Library (installed locally) nor the web presence for the same are very easily searchable or navigable, and they do not present their information in the most user-friendly fashion.

Aside from that, if you’re on the fence about picking up some Microsoft development experience, I’d actually (and I don’t like to hear myself admit it) recommend Visual Studio 2008 – assuming, of course, you’re already familiar with low-level programming, compiling, linking, application design, code layout, and all those things. Because if you’re new to the game, and Visual Studio is your first foray into software development, I don’t think you’ll really be able to get a firm grasp on the fundamentals, as it [Visual Studio] tends to abstract, hide, and/or distort so many aspects of what’s actually going on. I’m a firm believer that if you don’t understand how something works, you’ll never be able to fully take advantage of it.

Categories: Uncategorized Tags:

Custom Gnome Keyboard Shortcuts

March 8th, 2009 Chris No comments

I’m a keyboard junkie.  If I can manage to feng-shui my hands into such a position that I can elicit action from my computer without reaching for the mouse, I’m all over it.  Part of it is because to use the mouse means, at some point, a transition BACK to two-hand typing.  I find that the time it takes to either glance away from the screen – or feel my way back to the home row – can actually disrupt my train of thought.  I’m also the guy that uses a manual can opener, because when the power goes out, I don’t want to be stuck with imperishable food that I have to hack into.  Maybe I’m just weird.

I jest (sort of) – but I actually have been in situations where GUIs blew up for no good reason, and I was only left with command-line access to the server over SSH – quick fingers and a solid knowledge of the “old-school” config files saved the company hours of site downtime.

I digress.

Either way, I love keyboard shortcuts, and I’ve found it’s actually very easy to create all the custom keyboard shortcuts you want under the Gnome Desktop.  First, you’ll need a very handy tool (if you don’t already have it) called the gconf-editor.  It’s a right useful little GUI into the heart of the Gnome Configuration files.

From the default gconf-editor screen, browse into apps -> metacity.  First, you’ve got to tell Gnome which keys you want to be pressing.  Enter “global_keybindings” and find “run_command_X” (replace X with the number of your choice.  I’ll arbitrarily choose 4 in this example).  Double click “run_command_4″, and you’ll get prompted to “Edit Key”.  Here, enter your preferred key combination.  Note: You won’t actually be pushing those keys, you have to tell Gnome what they are.  For example, if you want to open Pidgin with the key combo <Control><Alt>p, then in this dialog you’ll have to enter, literally, the text “<Control><Alt>p”.

global_keybindings11

So now Gnome will capture <Control><Alt>p.  But it won’t do anything with it… yet.  Head from “global_keybindings” to “keybinding_commands”.  Double click the corresponding “command_X” entry (in this example, “command_4″) and once again, the “Edit Key” window pops up.  This time, you need to tell Gnome what to do when you press the keys bound to command_4.  In our example here, you’d just type “pidgin” and be done with it.  Really, you can put anything in here you want.

keybinding_commands2

And voila.  Now, when you press <Control><Alt>p, Gnome will go and launch your favorite purple instant messaging client.

Of course, you’ll replace my example text with your own keyboard shortcuts, bound commands, and tales of culinary paranoia.

Categories: Uncategorized Tags: