Check out my result from Speed…

Check out my result from Speedtest.net! http://t.co/HdL1lXe

Uncategorized

ADVICE #6 Cache if possible Es…

ADVICE #6 Cache if possible
Especially database lookup tables and objects with constant state. This will boost your system’s performance.

Uncategorized

ADVICE #5 Use std::for_each v…

ADVICE #5
Use std::for_each vs hand written loops. You gain clarity, more organized code and better performance overall.

Uncategorized

ADVICE #4: Be portable. Exampl…

ADVICE #4 Be portable. Example: Use size_t for size related items; string s(“Some Elem”); size_t len(s.size()); Don’t use unsigned instead.

Uncategorized

ADVICE #3: Ensure you understa…

ADVICE #3 Ensure you understand pointers. Explain: Be able to deal with: char **pp(new char*[25]), int** fun(int** pp) to name a few.

Uncategorized

ADVICE #2: Write as little cod…

ADVICE #2 Write as little code as possible, but no less. Explain: implement what is required, don’t get fancy, don’t over-design. Deliver!

Uncategorized

ADVICE #1: Preallocate a vecto…

ADVICE #1 Preallocate a vector if you know a number of items to insert. Example: insert 30 items; std::vector<SomeType> v; v.reserve(30);

Uncategorized
Enter your password to view comments.

Protected: Object creation and performance.

This post is password protected. To view it please enter your password below:


Optimization

This is constant! No, it ain’t.

Does that sound familiar? He or she claims they return a constant pointer to a memory buffer in question so no user of his/her API can modify the content of this buffer. This is absolutely brilliant. How could I possibly doubt his/her C++ programming skills? As I was profiling a huge chunk of some serious code, I have noticed the memory buffer was indeed changing outside the scope of the object in which it lived in. It was so obvious to me, that whoever wrote this code, knew exactly how to get rid of constantness! He or she indeed knew “the how”, but they failed miserably to understand the very important part, namely, “why not”. Their “const-cleverness” and outstanding capability to promote techniques leading to an epic failure, make him an invaluable incumbent in our study. To be as passionate as he is in his endeavor of stealing constantness from objects that are meant to be constant and are created as constant for a good reason, requires a tremendous amount of courage. Courage required to withstand a possible unpredictable anger attack from a fellow programmer that is rather “const-aware” and it does not belong to the const-thief cult. Theses thieves not only pollute the const-correct code, but they praise themselves in doing so. If I really refresh my memory I may remember some pieces of their code. Not all of the details, but at least those lines where they expressed their const-cleverness.

The code in line 12 depicts how well one can violate the constant-corectness. The sad part is that we still find code similar to this in production code. When you attempt to utilize const_cast think twice before doing so. You are dealing with a constant object and you may not like it since you need to make some changes to this object, right? Well you could copy the object and go from there, but the copy will be expensive and you do not want to do it. The quickest way you can do it is to utilize const_cast. A lot of programmers do so because it is convenient and does not require more thought into what needs to be done to accomplish the task in hand. Such code will eventually make into production within environments where there is no formal code review process. A programmer silently writes pieces of code like above, tests them and then silently commits his or her changes. He finished his task, he finished his project on time and he is happy as he can get a good raise and bonus the next evaluation period. Does that sound like a reality check to you? It may, or it may not. In either case I am not promoting the use of const_cast! I am not saying do not use it all. There will be scenarios when const_cast may have valid usage scenarios. I will cover those in other posts.

Uncategorized

Arrays on the Heap: Part #3

In this part of the “Arrays on the Heap” sequence I will discuss a different approach to implement a dynamic array. I will finish with the complete implementation that is cleaner, extensible and better overall. Before I go into details of implementation, I would like to point out a certain property of multi dimensional arrays. It is not that difficult to observe that a 2-dimensional array is a collection of 1-dimensional arrays. This is rather easy to see and can be illustrated by taking a sample array int[2][3]. This 2-dimensional array has two 1-dimensional arrays, each having 3 elements. This is by definition and not by any other means. Going further one can conclude that 3-dimensional array is a collection of 2-dimensional arrays, and 4-dimensional array is a collection of 3-dimensional arrays and so on. To generalize this, I am going to say, that for any positive integer n, a multi-dimensional array of dimension n, is a set with cardinality C=n, of multi-dimensional arrays of dimension n-1. I am going to call this property, a composition property of multi-dimensional array. Huh, am I trying to be a mathematician? A set, a multi-dimension, a cardinality and what not, please forgive me: I like math, I like clean notation and, besides, math is everywhere, isn’t it? The cardinality C of a given set A, C(A), is the number of elements of set A. That is rather a simple definition. The composition property of multi-dimensonal array suggests a way or a method to implement a multi-dimensional array with C(A) = n. One starts by defining an array of dimension n as a collection of arrays of dimension n-1 and then provides a definition of an array of dimension equal to 1. This will be done by utilizing templates. With that in mind I am ready to provide an implementation of a n-dimensional array on the heap.

That would be it for a n-dimensional array on the heap for any positive integer n for which the upper bound is the bound of the compiler you work with. I do not do any error checking in my code. “Shame on you”, I hear you shout. And, believe me, I hear you and you are right. But as far as I am concerned, this is not production code, this is my rant code so I am allowed to do so and you should cut me some slack. You may also notice how my destructors eat all the exceptions, but no exception would ever escape from them. Yes, there are better ways to handle this, but I am just too lazy to do it now and I leave it to you as an exercise. I am also pretty sure you have noticed that I am using placement new. Yes, I do and there is a good reason for it. I want to avoid calling a default constructor; that’s all there is to it. And, no comments in my code? That’s right, no comments. I cannot spoil you so much. After all, that’s what this blog is all about. You have questions, comments or something is not clear? Please ask, post your question, post your comment. I will answer it as soon as I can, or others may help as well. Do not be afraid to ask questions. If I do not understand something, I ask questions till I get right answers and I fully understand the topic. That is the only way to learn and learn quickly. You gotta ask questions! Other than that, things are rolling fine. Questions or comments, ping me.

Arrays, Exercises, Pointers