The Evils of Declaring Global const CString Objects

 

Most of the MFC programmers has tendency to declare the global const CString objects instead of global const char* array (or LPCTSTR) as string constants.

To optimize the storage of constant data, you have to make sure that the constant data is always get stored with your program. Before going to CString specific issues, let’s go take some other examples.

If you want it to be a constant, you must declare it as an initialized const static or global variable as follows. (If no const specified there, it will not be stored with your program)

[sourcecode language='cpp']
const char g_pch[] = “test”;[/sourcecode]

If several instances of your program are running, the same EXE and DLL files will be mapped to each process’s virtual address space. To know what’s happening inside and to

know where the constant data being stored, we’ve to understand about the different sections that Visual C++ Linker generates. Some of the important sections and their characteristics are as follows
Name Type Access Contents
.text Code Read-only Program code
.rdata Data Read-only Constant initialized data
.data Data Read/write Non-constant initialized data
.bss Data Read/write Non-constant uninitialized data

The g_pch data we mentioned before will be stored under .rdata section which is part of the program. it’s happened because the data is constant (declared as const).

The linker will put the built in types and even structures as global constant as part of programs. But the case is different for C++ objects. C++ objects should be initialized using constructors, thus the compiler will generate code for constructors and destructors. Take the following example

[sourcecode language='cpp']
const CRect g_rect(0, 0, 100, 100);
[/sourcecode]

In the above example, the data (g_rect) will be stored under .bss section and initializes, when the program loads. The things are more evil if you use CString objects. Consider the following example.

[sourcecode language='cpp']
const CString g_str(“Hello World!”);
[/sourcecode]

In this case the CString object will be put inside the .bss section and there’s a constant string which we passed to the constructor. It will be stored under .data section (as we have not specified it as const). In addition to that, CString objects are keeping data by dynamically allocating memory in the heap. To optimize the storage of constant data, it’s always better to declare strings are const char* or if your program wants to support both UNICODE and non-UNICODE build, then delcare it as

[sourcecode language='cpp']LPCTSTR g_pch = _T(“Hello World”);[/sourcecode]

So take care of Global CString constants. I got this nice tip from the book “Programming Visual C++” by David J. Kruglinski

 

New Year Letter from Warren Buffet – Globally Applicable Every Time

 

Today I got an E-Mail forward from my friend. I don’t know whether this written by W.Buffet. But still it’s worth reading especially in this time of financial crisis.

We begin this New Year with dampened enthusiasm and dented optimism. Our happiness is diluted and our peace is threatened by the financial illness that has infected our families, organizations and nations.
Everyone is desperate to find a remedy that will cure their financial illness and help them recover their financial health. They expect the financial experts to provide them with remedies, forgetting the fact that it is these experts who created this financial mess.
Every new year, I adopt a couple of old maxims as my beacons to guide my future. This self-prescribed therapy has ensured that with each passing year, I grow wiser and not older. This year, I invite you to tap into the financial wisdom of our elders along with me, and become financially wiser.
Hard work: All hard work brings profit; but mere talk leads only to poverty.
Laziness: A sleeping lobster is carried away by the water current.
Earnings: Never depend on a single source of income.
Spending: If you buy things you don’t need, you’ll soon sell things you need.
Savings: Don’t save what is left after spending; spend what is left after saving.
Borrowings: The borrower becomes the lender’s slave.
Accounting: It’s no use carrying an umbrella, if your shoes are leaking.
Auditing: Beware of little expenses; a small leak can sink a large ship.
Risk-taking: Never test the depth of the river with both feet.
Investment: Don’t put all your eggs in one basket.

I’m certain that those who have already been practicing these Principles remain financially healthy. I’m equally confident that those who resolve to start practicing these principles will quickly regain their financial health.
Let us become wiser and lead a happy, healthy, prosperous and Peaceful life.

Warren Buffet

 

Virtual CD/DVD for Windows 7 (beta)

 

After installing Windows 7 in my laptop, I had to use some of my DVD image files. As usual I tried installing Magic Disc which is my favorite Virtual DVD Drive. The installation had shown up some problems and finally somehow I was able to launch Magic Disc but I failed to map the image file and instead I tried Virtual Clone Drive. Now things are working fine but Virtual Clone Drive is not having much options and easiness of use like Magic Disc. Bye Magic Disc at least till you fix the problems with Win7.

Update[3/15/2009 - 10:06 AM IST] : My all-time-favorite Magic Disc is now supporting Windows 7. You can download it from their website.