All posts by Nathan


So getting back into C++ programming, I’ve been allocating plenty of dynamic memory (using new  and new[] ) .  However, I’ve only been using delete , never delete[] .  If fact, I’ll be honest, I forgot about delete[] , but that’s OK, I didn’t need the [] form anyway.  Why you ask?

Take this example:

Now, almost every example you see online will actually have delete[] buf; , this is correct, but unnecessary.  Why?  The only difference between delete and delete[] is item deconstruction.

When you call delete, all the memory you allocated with new[] is still erased. The problem is, only the first element (the one being directly pointed to) is deconstructed. None of the other members of your array are deconstructed. In my example this is OK, because the array was an array of “char”s, you can’t deconstruct a char in C++, it’s not an object.


When is delete[] useful?

When you allocate an array of objects that themselves allocate more memory. delete[]  makes sure that each entry in the allocated array is properly deconstructed. Example:


Can I just call delete[] all the time?

Sure, well, almost.   delete[]  works on a specific type of pointer.  When you call myclass * myptr = new myclass[5] , you are telling the system to reserve you sizeof(myclass)*5  bytes of memory.  This size is all that is stored in the memory allocator.  When you call delete[] myptr; , the memory allocator goes through the following process:

  1. How much memory was reserved starting at the address stored in myptr (answer: sizeof(myclass) * 5);
  2. Start at offset = 0.
  3. Call the myclass.deconstructor at the address myptr + offset.
  4. Increase offset by sizeof(myclass)
  5. If offset < total allocated memory, goto 3.  Else, continue;
  6. Delete/Free all memory reserved (sizeof(myclasS) * 5 bytes) starting at address storef in myptr;.

However, if you tried to call delete[] (void *) myptr;  all calls to sizeof(myclass) above will be replaced with sizeof(void), which returns no size (no object can be of type void). Therefore, none of the objects in the array will be deconstructed.  Calling delete[] (void *)ptr;  is the same as calling delete (void *)ptr; .

Similarly, if you recast the pointer to a different type, delete[] will not know the proper size of each object, and any attempts to deconstruct those “objects” will most likely result in errors/exceptions.  Example:

Of course, casting pointers in C++ is always dangerous.  The compiler and system do not keep track of allocated memory by type, only size.  Therefore, you should always be careful when having to cast a pointer.

Note: There are acceptable times to cast a pointer (when reading in bytes that are actually multi-byte values, say casting an int* as a char*), but often times, this can also be handled with Unions.

Anyway, that’s my “quick” overview of the delete and delete[] operations in C++.

Ok, so many of the jobs I am applying for now want C++ experience.  Problem is, none of the companies I have worked for in the past 8 years wanted me to write in C++.  The industry used to use Ada, and many legacy programs still have all their code in Ada.  Not only that, but the companies that use C++ often use model-based development suites, meaning they don’t write any actual code, the compiler does it all for them.

Anyway, I am quite familiar with C++.  It was the first real language I ever programmed in (not counting Quick Basic).  I took 3 years of C++ in high school.  I did all sorts of things in the days before Java, Python, Perl, .NET, etc…  Now days, I’m not even sure people know what C++ means.  They probably use so many libraries, they really only use C++ for the Syntax and wide array and industry tested (and free) compilers.

Long story a little bit shorter, I have started to upload some of my old C++ code to my SVN server.  The page describing it all is on my website here: C++ Code.

That is all.

So I figured I’d start playing around with micro-controllers again (ok, never really played around so much as took classes in college).  Anyway, my old HC12 micro-controller is stuck in storage, and it’s not supported anymore (it was old before I graduated college).  I decided that I would try-out the new Raspberry Pi since I found it in stock a couple months ago.  It’s a nifty little computer, but not really a micro-controller.  It lacks real-time support (from the “default” Linux-based OS) and even sub-millisecond control (which sucks for sensors and whatnot).  I then decided I should pair the Pi with an Arduino, a true real-time micro-controller.  Anyway, I plan to hook up some stuff around my house to the Arduino, and then control the Arduino over the internet via the Pi.  The only real reason for the Pi in this scenario is to avoid the high power-consumption of a regular PC.

Soo… I’ve been playing around with both devices and the various ICs and electrical components I have left over from college.  Bought myself some relays, etc.. so I can control things like lamps.  Uploaded my code to my SVN server for two reasons, backup, and the ability to move it easily between my PC and the Pi (since I can program the Arduino directly from the Pi, I can avoid my computer when desirable). Nothing exciting yet, I wired up a couple 7-segment displays to an I2C 16-port expander, got a Ping))) sensor measuring distance, and wired up a 4-button debounced switch panel, so now I just got to think of something to do with all this stuff.

So lately I’ve been working on updating a website I use to keep track of my movies.  The wife and I have collected over 1000 movies and TV show seasons, and it’s hard to remember what we have and don’t have, especially when we see a movie we like on sale.  So anyway, some years ago I created a website to keep track of these things.  At first, and for the last few years, it was very simple.  Each movie was a single row in a table, and the actors and genres and whatnot were just fields in that table.  Each movie basically lived in it’s own world, one movie might star “Samuel L Jackson”, and another could star “Samuel L. Jackson”.  Since it was just for my wife and I, we lived with these limitations, we could just be careful about how we entered movies.

Of course, early entries were done entirely by importing CSV files into the database directly, but eventually I added a web interface to add and edit movies in the database (since my wife is not very SQL knowledgeable).  Of course, this page was very limited.  You could enter whatever you wanted into text fields (title, starring, comments) and it had pre-determined check marks for genres and media (actually, I added media after we bought our first Blu-ray player, before then, everything was DVD).   When we were dating we had an “owner” field, but we’ve since abandoned that.

Anyway, my mother-in-law was commenting about their movie collection (maybe 500-600, and decent collection for sure).  My wife mentioned out database, and since it would only involve copying the source code to another directory and duplicating the structure of one MySQL table, I agreed to setup a website for the in-laws.  Five minutes after sitting down at my computer, and I had a website all ready to go, just needed the list of movies.  However, I examined the website, and determined it lacked panache.  Actually, it lacked usability, and I set myself to the task of re-working the website to make it more friendly.  The last such update, some months ago, was to pass around a validation key so that the password didn’t have to be re-typed when entering multiple movies at once, not a technological accomplishment by any means.

It began with a focus on the database itself.  It seemed pointless to me to have to re-enter the couple-hundred movies my in-laws and I surely have in common.   Especially so when my movies are already cataloged by genre and actor (where appropriate, seriously, can you name one actor from “Killer Klowns from Outer Space” without IMDB or Wikipedia?).  I split the single movie table into actors, media, genres, and movies, and created relational tables between them.  This, of course, made the SQL statements much more complex, where before it was simply “SELECT * FROM movie WHERE actor LIKE ‘Tim Robbins”, the code now involves multiple joins and grouping statements.  Still, once I optimized the query, it didn’t seem to take any longer than before.

Once the database was setup, I worked on improving the interface.  The page to add/edit movies, as stated before, was minimal.  You could enter any actor name you wanted, and title, and as long as the password was correct, you could insert that data into the database.  Now, it checks the title you have entered against the database (using AJAX) and offers suggestions (example:  “Tron Legacy”  –> “Tron: Legacy”, actually, we don’t own that, but anyway…).   It also has a browse-able list of actor name, and click on the name adds it to the movie.  No more typos leading to duplicated actors.  Anyway, it was much more user friendly now, but these changes could only be demonstrated when adding a new movie.  My next task was to update the display pages.

Well, actually, updating the display pages had to be done after I updated the database, since my tables were now different, but it looked exactly the same, which isn’t very fun.  I decided to clean-up the clutter (and reduce my database hits) by removing the actors and genres from the main display page.  Some movies (*cough* Ocean’s 13 *cough*)  had a lot of actors and were cluttering up the screen.  However, in order to determine the difference between “Alice and Wonderland” and “Alice in Wonderland” (one the animated Disney classic, the other the live-action movie directed by Tim Burton), I still needed to display the genres and actors.  I wrote-up a little AJAX query, and soon enough, clicking on a movie title pops-up a box with all the movie details.

Finally, I figured I wanted a faster way to find a movie than by clicking on the starting letter and scrolling through the list.  I implemented a final AJAX query and now have a search field on every page.  This way, if I’m at the store and want to know if I already own “The Dark Knight”, I can be reminded that we already own 2 DVD copies and 1 Blu-ray copy without having to scroll through all the movies staring with D.

Of course, there were many other changes to get my website to conform to HTML5 and CSS3, but I think the website should now be much more user-friendly (so I don’t have to answer too many question from my MIL).  Next on my plate is an Android app that will interface with the website and allow caching of movies (when you’re at the back of Target and can’t seem to get a data connection) and searching for quick lookups.  Luckily the in-laws didn’t go with iPhone’s, cause I couldn’t help them there.

Oh, hah, here’s the link to the movie website.  It’s open for browsing, but no editing without the password.

So Tuesday night I “finally” wrapped up development on my first Android app, my very own TipCalculator.  I uploaded and branched the source, copied the binary to my webpage, created a TipCalculator page on this blog, and downloaded it to my own phone (to make sure that worked).  It was all good and well until… I realized that I forgot to fix something after I moved some code around, and now I could no longer delete Tips.  Oh boy.

So anywho, more debugging, fixed a couple other issues (my rounding was bad, oops), and finally: TipCalculator version 1.1 was born!  You can check out more info if you care to on the TipCalculator page.   It’s nothing spiffy, but I figured I can add it to my portfolio if nothing else.

I just created an SVN repository to host my various projects.  Right now it only has the source for my lone android application, a tip calculator (unique, I know).  There are 1800+ results for Tip Calculator apps in the Google play store, so I doubt I’ll be adding mine to the fray, but the wife can use it and it was a good intro to Java and Android programming.

The SVN repo is here:  It’s open for browsing, but I don’t really need any community contributions right now.  I’m using eclipse, so the apps are setup as eclipse projects.   No apks just yet, working on fixing the last few bugs.  I’ll probably create an FTP account to host the actual releases.

Anywho, that’s that.

Ok, I used to have a website here, because I figured I would try out “micro-blogging” and don’t care for Twitter.  Twitter enables things like following others and allowing them to follow you, which I don’t care about.  So, anyway, here we are.