C++ : The new and delete operators
March 21, 2009
If you have been accustomed to C, you may encounter the way heap memory is allocated:
char* c = (char*)malloc(sizeof(char) * x);
In C++, due to the existence of classes and constructors/destructors, malloc and free alone won’t suffice; it won’t be able to call the constructor and destructor, respectively. So, the keyword new was made to allocate the memory and call the constructor. The previous item will now look like this:
char* c = new char[x];
Notice that sizeof(char) is gone. Why? Because new will, in order to return the correct pointer type, need to know the type of data you are allocating, and the operator determines the size of an element from the type. The general syntax of the new keyword is:
<data type>* x = new <data type>[y];
where x is the pointer to store the address of the allocated memory, and y is the number of elements. When y = 1, the [y] part can be omitted:
<data type>* x = new <data type>;
Notice new’s two forms: the former, with the square brackets, is called vector new; the one without the brackets is called scalar new.
On the other hand, delete does the reverse: it calls the destructor before putting the memory back to the heap. The delete operator has two forms:
delete – for single-element allocations (scalar)
delete[] – for multi-element (array) allocations (vector)
To avoid being too technical, as I’ve done in some posts, the versions with the square brackets are for array (vector) allocations/deallocations, while the versions without the brackets are for one-element (scalar) allocations/deallocations.
.NET Framework runtimes and legacy computers
March 8, 2009
We may have heard of the .NET runtime in one form or another. Some applications require it in order to run, and many user-interface components out there rely on that framework. But I’ve found out, to my disappointment, that Windows 2000 (or XP) and earlier do not have it pre-installed.
The .NET framework is a significant installation on any machine (a check in the Control Panel applet will easily show how bloated it is, ~100 MB for v2.0), and though speed and performance isn’t much of an issue on newer computers, I tried installing it on an older computer I have at home, and my computer’s slowing to a crawl. Besides, I think v2.0 of the network requires Windows 98 or later – it’s fine, since it’s built with MSVC 2005 and that compiler has a minimum OS requirement of Windows 98, to say the least. The real thing I don’t like is v3.5 . It’s built with MSVC 2008, and, as I’ve said in an earlier post, requires Windows 2000 or later.
One important item to point: in industrial automation, the time used for executing MSIL code often isn’t tolerable.
I guess the v3.5 runtime is bloated more than twofold over v2.0. The latter’s already fine for me, so why install a newer one?
There aren’t any issues with the .NET framework under Vista, since they’re pre-installed. To say, though, I, like many other users, don’t like Vista – it’s bloated on two sides: disk usage and memory usage, and the more important issue is compatibility.
Even with the v2.0 framework installed on my machine, I still stick with developing native C++ code, with a VC2005 compiler (since I recently began hating VC2008), and I can use Adobe Flash for my UI needs.
And I’m making all of my DLLs in my libraries in native x86/x64 code.