The Way a Program Starts in Windows

If you write a program in C/C++ for Windows, you are very close to the interface with the operating system. For this reason, it is also interesting to have some idea of what it is doing.

There are two main types of Windows programs: graphical programs and console-based programs. The way Windows determine what kind of program you are trying to create is by a flag it stores inside the executable. This is also called the “subsystem” in which the program will run.

To determine the subsystem in visual C++, one has to set an option in the linker section. This will determine what Windows will see as the subsystem for that program.

Depending on the subsystem, Windows will call one form or another of the C library function that starts a program. The name of this function is also configurable (usually it is __tmainCRTStartup), and it can be changed from the linker section of VC++.


First Steps of a Program

The initialization function in the C library does just a little bit of work that is required by the C run time. One of the most important is to initialize the heap, so functions like malloc and the operator new can work properly.

The initialization function also sets some common values used by the system, such as environmental variables, and the version number of Windows.

Finally, __tmainCRTStartup calls the main function declared by the Windows program. Usually this is called WinMain, but it can be some variation of this depending of the version of Windows and if you are using ANSI or Unicode.

When a program returns from the main function, the initialization function cleans up the heap and returns to the operating system.


Reference

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Understanding const pointers and variables in C++

In modern C++, we are used to see const pointers. They are a useful way to avoid changes in memory passed to a function. As such, they are very common in function declarations.

The const word, however, has other uses. It turns out that when the const keyword is applied to the pointer itself (instead of the contents of the pointer), the const modifier is also very useful.

When we say, for example, const Type *t, we are saying that the contents of the object pointed by t cannot be changed. However, we can just as well say Type * const t, which means that the pointer t is unchangeable.

For example, check the following code:

int main() {
   int i, j;
   const int *p = &i;
   // *p =0; // ERROR: cannot modify the memory pointed by p
   p = &j;

   int * const q = &i;
   *q = 0;
   // q = &j;  // ERROR: cannot modify the pointer q
   return 0;

   int const k = 0;
   // k = 1;   // ERROR: cannot modify the variable k

}

See that Type * const t may be a syntactical clue for how the code is organized, because in short methods we usually don’t want to have variables and pointers changing. Thus, in such a case we should avoid having variables that are not const.


Understanding const Syntax

The reason for the difficulty of understanding the behavior of expressions such as Type * const t is due to the complexity of the C++ syntax.

Notice that the example const int k can also be written as int const k (as shown above). This denotes that the const modifier is intended to the variable.

When we have a pointer, however, we can also have a const memory location, which is represented as Type * const p. The const attribute is now referring to the content of the pointer, and this is indicated by having it after the * operator.

To simplify the notation, a better way to write const attributes is to make this difference explicit as in the examples bellow:

Type const * p; // can't modify what p points to
Type * const q; // can't modify q itself
Type const * const t;// can't modify neither t or point value

As a quick tip to remember the correct syntax, always keep the const close to the * operator, to make its meaning easier to understand.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Why is Twitter More Interesting Than FaceBook

I recently read a blog post [1] commenting on how twitter gives a better user experience than FaceBook. I hadn’t spent a lot of time thinking about the difference, but I suddenly realized that there is really a big shift in focus and tone when I use Titter or Facebook.

First, there are the obvious differences in interface. For example, when using Twitter there is the advantage that you are not required to follow what other people are saying. You are expected just to check one or another comment. Certainly, you can use Twitter as a two-way communication medium, but you can just as well concentrate in something unique about the person that is posting. This was the main point made by Scobleizer.

For Facebook, on the other hand, one has fewer ways to filter the contents of the conversation. Also, there is the annoyance of having comments mixed with the main information.


When Is FaceBook Useful?

There is clearly a context where this is useful. And this is exactly where Facebook was born: a group of friends that want to exchange news, comments, pictures, and videos. If that is the case, then Facebook is the right solution, but it works only if you do this with a small group of people that you know well. People are naturally receptive to what their friends want to say, even if it not useful for you.

You can’t say the same, however, when people that are not a close friend join your conversation. You can quickly become annoyed by what they say, especially if it is not something of your immediate interest.


Content Issues

Also, the content that works on Twitter may not work on FaceBook. For example, I use twitter to comment about my work, which mostly involve technology. But this is not something that all my friends enjoy. I don’t want to fill their Facebook first page with stuff that may not be of common interest.

Conversely, I feel better talking about personal matters on Facebook, because this is where my friends are. I feel that is difficult to mix both networks, but both have important use cases.

Personally, my conclusion on using Twitter and Facebook is that Twitter is much more scalable as a communications medium. You can basically talk anything you want on Twitter, and you will eventually find someone interested on it. This is hard to do in Facebook, unless you spending a lot of time to cultivate the right group of people as Facebook friends.

Facebook is better to keep in touch with people that you have something in common personally: either from the same school, neighborhood, or family. It is a great tool, but doesn’t go much further than that.

From the business point of view, I think FaceBook’s position is weaker, because people can get tired of the typical use cases (unless you are talking about a very young demographic group). Twitter appear to be more suited for business applications.

We still don’t know how the business will play out for both companies, but it look to me that their roles are starting to become easier to feel. Only time will tell, though, if they will change as quickly as necessary to continue growing.

[1] http://scobleizer.com/2009/11/02/the-chat-roomforum-problem-an-apology-to-technosailor/

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Using the Immediate Window on Visual Studio

This is a quick tip for visual studio users. It may be that most developers using VS already know this, but I only recently found about the immediate window. The immediate window is useful as a complement to visual C++ integrated debugger, as it allows one to evaluate small C++ (or C#) expressions during debugging. It is the closer you get from having a REPL in C++, using visual studio technology (of course, for people used to Python, Ruby, or Lisp, saying this is almost an insult, but that is what can be done).

To access the immediate window in debug mode one has the following options:

  • Type Control-Alt-I (the fastest way)
  • In the command window, type imme
  • Or find the option “Immediate Window” in the “view” menu (if you are lucky enough, some people can’t find it [1])

Once you have an immediate window, you can type simple expressions. The most useful of them are assignments, so you can easily change the value of a variable while observing its behavior.

You can also use this window just to check the value of data, without the need to call a “watch” or “quick watch” window. It is just easier to type the expression you want to see.

Finally, you can combine this with an unrelated but useful feature: you can move the instruction pointer for the program by drag-and-drop of the instruction pointer (the yellow arrow on VS editor). This can be used to simulate at least part of the “drop-to-frame” feature in Java.

[1] http://msdn.microsoft.com/en-us/library/f177hahy%28VS.80%29.aspx

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Day 10: Learn Keyboard Shortcuts

Hi, this is the 10th part of a series of posts on 30 tips to becoming a better developer. If you would like to keep up to date with the topics that I am covering, just check the main post.


Programming is like playing a game. It gets better when you have more practice. Although you need a lot of intellectual acuity in order to create good programs, it also helps if you can do it faster.

Working faster is one of the abilities of good programmers that separate them from the remaining of the pack. If you can program fast, you can find better solutions by just iterating. You can do three different solutions and select the best one, while your fellow will be stuck on the first option. It is a numbers game, in some way.

While it doesn’t make any good to write thousands of lines of bad code, writing more code will improve your capacity to reason about what needs to be developed. It is the typical reinforcement loop that can make you a real performer.

So, although working fast is not what makes good code, by practicing fast paced work you will actually achieve better results quicker. And one of the best ways to improve your speed is learn the tricks of the keyboard.


Developing as a Musician

The metaphor of a musician is an interesting one, because musicians don’t need to play fast every time, but it is a fundamental skill to play fast when necessary. The same goes for developers. But to “play” well, you need to understand the “instrument”.

To improve speed, I think it is essential to learn the most keyboard shortcuts you can. Typing is faster than using the mouse for programming tasks, that involve textual composition (it is different for people working on graphics, but many of them also love keyboard shortcuts).

Every decent development tool has a lot of keyboard shortcuts that can make you go faster. Start by learning just a few of them. Then, try to learn a new one every day. In a few weeks, you will see that your speed has increased dramatically.

It also helps to get a good text editor. What a good text editor is changes from person to person (I am a Vim fan), but there is a lot of good choices: vim, emacs, texmate/e, eclipse. Take the tool that best solves your problem and is adapted to your work style, and learn it like you would learn a good video game.

Go to the next post of the series:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter