December 10, 1999

Assigning a Zero Value to All Members of a Struct

Sometimes you need to clear all the members of a struct after it was used. With large structs, you usually use memset() for that purpose. However, for smaller structs that occupy 2, 4 or 8 bytes of memory, calling memset() is rather expensive in terms of performance: struct Date {

Linker Optimizations

While most of the code optimizations are performed at compile time, there are optimizations that only a linker can perform. For example, it can detect unreferenced function calls. These are functions that exist in one or more source files and were compiled. If the linker detects that the application never

Deleting a const Object

In earlier stages of C++, it was impossible to delete a const object, even if that object was constructed on the free store. This could cause memory leaks. However, the C++ Standard was changed recently. You can now use operator delete to destroy const objects that were created by new:

Initializing a Bit Struct

To initialize a struct that contains bit fields, simply use the ={0}partial initialization list: int main(){ struct MP3_HEADER { unsigned Sync:11; unsigned Version:2; unsigned Layer:2; unsigned Protection:1; unsigned Bitrate:4; unsigned Frequency:2; unsigned Padding:1; unsigned Private:1; unsigned ChannelMode:2; unsigned ModeExtension:2; unsigned Copyright:1; unsigned Original:1; unsigned Emphasis:2; }; // create an instance

Switch Statement

Question: Does Java have a switch statement that can use doubles and floats? Answer: The Java switch statement only supports values of type char, byte,short, or int. To conditionally execute statements based on thevalues of doubles, floats, and longs, you must either convert them toa type supported by the switch