Sunday, June 08, 2008

Definition of Programming

Programming is all about knowing when to boil the orange sponge donkey across the phillipines with an orangutang gorilla crossed with a ham sandwich to the fourth power of twelve across the nile with an awful headache from the previous night when all of alfred's naughty jalapeno peppers frog-marched the nordic elves across the loom-lined geronimo induced swamp donkey over and above the fortran fortified kilomanjaro fence past the meticulously crafted anti disgusting sponge cake scenario where all the hats doth quoteth the milk which is not unlike the super werewolf from the infinite realm of ninja-step. it's hard to define, really.

Something I found on a blog.... I don't have any comments for this one...hehe

Monday, June 02, 2008

Memory allocation in C++

All I had to do was read some content from Registry, but it was not all that simple, I had to read 1 registry, then use the content to read another string and then use it.

While doing all this, I didn't want to use static size buffers, so I started doing malloc(), but guess what malloc just refused to allocate 104 TCHARs, hmm then I tried HeapAlloc() and that was able to allocate the memory.

Different ways to allocate/de-allocate memory:

malloc HeapAlloc HeapCreate
calloc GlobalAlloc GetProcessHeap
realloc LocalAlloc HeapFree
new   HeapDestroy
    VirtualAlloc

small amount of memory should be allocated through malloc family. For bigger size user HeapAlloc, GlobalAlloc and LocalAlloc are available only for backward compatibility, so should not be used.

For more detail information on Managing Heap Memory in Win32, refer : http://msdn.microsoft.com/en-us/library/ms810603.aspx

Sunday, May 11, 2008

Why write Unit tests

One of the reasons we don't refactor our code is that we are afraid that it might break something. But, if you have unit tests in place then, every time you change something, you have your unit test to rely on. It not only saves your time to verify the output, but it also assures you that nothing is broken.

Saturday, May 10, 2008

char and its relatives

In C, I was used to saying

char *psz = "This is some string";

here's what changes in VC++ :

VC++ has the ability to use multibyte characters and Unicode characters - this can be set using IDE options.

Between multibyte character system (MBCS) and Unicode, Unicode has greater acceptance, so we usually program in Unicode. Now to do that first all the string should be defined like this :

wschar_t *buff = L"This is some string";

here, the prefix L tells the compiler that the string is made up of Unicode chars and since char represents a 8 bit character, we need another datatype to represent Unicode char, so we have wschar_t (16 bit Unicode)

However, you might need to switch between ANSI string and unicode strings, to support such a situation, VC++ gives us  a macro TCHAR. It expands to wschar_t  if Unicode is defined else to char.

similarly, instead of harcoding "L" prefix, we again have an option in MFC to use a macro "_T"

We can write the macro ourselves in SDK as follows or include tchar.h

#ifdef UNICODE
#define _T(x) L##x
#else
#define _T(x) ##x
#endif

so now we can write

TCHAR *psz = _T("This is some string");

Modal vs Modeless Dialogs

Modal Modeless
Application control is lost is not lost
DialogBox API is used CreateDialog API is used
EndDialog is used to destory it DestroyWindow is used
Implements it own message loop uses application's message loop
behaves like a synchronous call behaves asynchronously

Calling conventions for functions

Finally I got to know what WINAPI is :)

it is a calling convention.

When a function call is made, you have a choice on how to pass function variables to the stack, they can be passed right to left or left to right, also there is a choice on whether the caller or the callee should clear the stack.

Based on these choices and some other things, we have different calling conventions :

__stdcall or CALLBACK or WINAPI,__cdecl,__fastcall,__thiscall

Check out the following links for details:

http://msdn.microsoft.com/en-us/library/984x0h58.aspx

http://www.codeproject.com/KB/cpp/calling_conventions_demystified.aspx

http://www.cs.cornell.edu/courses/cs412/2001sp/resources/microsoft-calling-conventions.html

Wednesday, January 16, 2008

Cross Thread operations

For quiet some time now I have been using random ways to doing begin invoke. Here is a good pattern for it.

   1: public partial class Form1 : Form
   2: {
   3:     public Form1()
   4:     {
   5:         InitializeComponent();
   6:     }
   7:  
   8:     private void Form1_Load(object sender, EventArgs e)
   9:     {
  10:         ControlsHelper.SafeGui(this, delegate()
  11:         {
  12:             textBox1.Text = "using ControlsHelper on UI thread"; // this succeeds
  13:         });
  14:         Thread t = new Thread(new ThreadStart(WorkOnNewThread));
  15:         t.Start();
  16:     }
  17:     private void WorkOnNewThread()
  18:     {
  19:         ControlsHelper.SafeGui(this, delegate()
  20:         {
  21:             textBox1.Text = "using ControlsHelper"; // this succeeds
  22:         });
  23:  
  24:         textBox1.Text = "direct"; // this fails
  25:     }
  26: }
  27:  
  28: public static class ControlsHelper
  29: {
  30:     public static void SafeGui(Control control, MethodInvoker invoker)
  31:     {
  32:         if (null == invoker) return;
  33:         if (control != null)
  34:         {
  35:             if (control.InvokeRequired)
  36:                 control.BeginInvoke(invoker, null);
  37:             else
  38:                 invoker();
  39:         }
  40:         else
  41:             invoker();
  42:     }
  43: }