"Good luck' follows careful preparation; 'bad luck' comes from sloppiness." - Robert Heinlein   |    "The talent of success is nothing more than doing what you can do, well." - Henry W. Longfellow   |    "Every artist was first an amateur." - Ralph Waldo Emerson   |    "The more difficulties one has to encounter, within and without, the more significant and the higher in inspiration his life will be" - Horace Bushnell   |    "Our doubts are traitors, and make us lose the good we oft might win, by fearing to attempt." - William Shakespeare   |    "The more you learn, the more you need to learn." - Robert Heinlein   |    "There is no such thing as luck; there is only adequate or inadequate preparation to cope with a statistical universe." - Robert Heinlein   |    "Nothing can stop the man with the right mental attitude from achieving his goal; nothing on earth can help the man with the wrong mental attitude." - Thomas Jefferson   |    Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.   |    "We are still masters of our fate. We are still captains of our souls." - Winston Churchill   |    "Success is the sum of small efforts, repeated day in and day out." - Robert Collier   |    "Nothing great was ever achieved without enthusiasm." - Ralph Waldo Emerson   |    "Perl - The only language that looks the same before and after RSA encryption." - Keith Bostic   |    "The big secret in life is that there is no big secret. Whatever your goal, you can get there if you're willing to work." - Oprah Winfrey   |    "Take calculated risks. That is quite different from being rash." - George S. Patton   |    "Nothing will ever be attempted if all possible objections must first be overcome." - Samuel Johnson   |    "If Java had true garbage collection, most programs would delete themselves upon execution." - Robert Sewell   |    "We are what we repeatedly do. Excellence, therefore, is not an act but a habit." - Aristotle   |   

Code Reactor

The best of modern Web development

Watch out for empty constructors

Well, this is a pretty rare caveat, but took me some time find. And it is a very subtle case of wrong assuming of things.

I will show this with an Ogre example, but the principle is the same in many other similar situations.

Ogre is a 3D engine, and naturally, has some convenience math classes like Vector2 or Vector3. Each such class has a number of constructors, both in form of ::Vector3(float x, float y, float z), but also in form of just a ::Vector3().

And at least for me, it is very easy to assume that a ::Vector3() constructor actually initializes the object to some default state, which for a vector would logically be (0,0,0). But when you think about it, it is not so certain at all… Also in my situation in release builds, these objects would be initialized to 0s anyway, even though I am sure it’s is technically a case of Undefined Behavior. In debug builds, this was not the case, and naturally, it created some nice bugs.

The moral of the story, be aware that empty constructors of such small data classes do not necessarily initialize any state of an object, especially when such classes have static members like Vector3::ZERO, which suggest that this is the default and best way to create a zero vector (like they do in Ogre).

Leave a Reply

You must be logged in to post a comment.

Your connection appears to be too slow, automatically disabling HeavyAjax (TM) for better performance...
You seem to run a browser without JavaScript support or it has been disabled. To fully experience Code Reactor please enable JavaScript. (It is not 1995 anymore :)
You seem to be using Internet Explorer. If you want to experience both Code Reactor and the rest of the web to their fullest and fastest, you are advised to download and install a real browser, like Opera, Firefox or Google Chrome.
You seem not only to use Internet Explorer, which is by far a joke when it comes to browsers, but to even use an old version of it!
If you want to experience both Code Reactor and the rest of the web to their fullest and fastest, you are STRONGLY advised to download and install a real browser, like Opera, Firefox or Google Chrome.