Visual C++ and vsscanf

Posted by Kaya Kupferschmidt • Wednesday, February 1. 2006 • Category: C++
When it comes to its IDE and its compiler, I consider Microsoft Visual C++ 7.1 and up to be a very good product. Especially the C++ language compatibility is at a very high level. But when it comes to its included standard C library, its compatibility even has gone worse with the 8.0 release, and its C99 compliance is also very liited.

This fact can you get into troubles earlier than one might guess. When you want to write your own string classes (and also in some other cases), it is useful to have a method with a variable number of arguments that in turn calls sprintf or sscanf (which both accept a variable number of arguments). Image the following:

class String {
public:
   int sprintf(const char* fmt,...)

private:
   char* _Buffer;
}

int String::sprintf(const char* fmt, ...) {
   va_list arglist;
   va_start(arglist, fmt);
   return sprintf(_Buffer, fmt, arglist);
}

Continue reading "Visual C++ and vsscanf"

operator delete != delete operator

Posted by Kaya Kupferschmidt • Monday, January 23. 2006 • Category: C++
Today I had an important lesson about a subtle detail that hides behind overloading the new and delete operator in C++. Actually in most cases, you do not need to overload these operators, but the possibility to do so comes in handy if you want to work with your own allocation schemes. So the following code is just legal in C++

class Myclass {
public:
Myclass();
~Myclass();
void* operator new(size_t size);
void operator delete(void* );
};

Continue reading "operator delete != delete operator"

Smart Pointers in C++

Posted by Kaya Kupferschmidt • Saturday, July 9. 2005 • Category: C++
If one is building a complex piece of software with many objects, almost automatically the problem of shared objects arises. That means there exist objects which do not only have one owner, but multiple owners instead - they are shared objects. The difficult question with shared objects is their lifecycle. Once an object is created and bound to different owners, it is not clear any more how and when the object should be removed from memory.

One common approach to this problem is reference counting, that is, each shared object contains a reference counter that is incremented each time the object is bound to a new owner and it is decremented as soon as one of its owners wants to release the object. Using C++ templates the reference counting mechanism can be easily automated by introducing so called smart pointers. The following snippets of code implement both an object with reference counting (which in turn can be used as a base class for many classes) and the basic ideas how to implement a shared pointer.

Continue reading "Smart Pointers in C++"

IDEs do make some difference...

Posted by Kaya Kupferschmidt • Sunday, April 10. 2005 • Category: C++
...even at some points where I didn't expect them: I am still working on the soapfilm demo and I found a very strange and mysterious bug: When I start the demo from within VIsual C++, it runs fine. But as soon as I try to run the executable directly from the Windows explorer, it does not run.

Some investigations brought me to the following very strange explanation: When running the program from Visual C++, the GLSL compiler does its job without complaining. Even if I insert some random characters into the shader program, the compiler does not seem to return any error, but no outpu is produced. But as soon as I start the demo directly from the explorer, the GLSL compiler starts to complain, and the program terminates.

Strange, eh? And how should I debug in such a scenario when the program behaves differently in the IDE?

Update: I found the bug - there was only the string terminating zero missing in the shader source. It seems as if the test program was run from within Visual C++ there has been a valid string terminator at the end of the shader code by incident. Did I ever say that most bugs are trivial?

But concerning the problems with ATI hardware, I suspect that there is a bug in the GLSL part of the drivers (Catalyst 5.4). I am able to reproduce an error by following this simple procedure:
1. Run 3dLabs ShaderGen.
2. Press "Build" - no problem so far.
3. Press "Build" a second time - suddenly the GLSL compiler complains about errors, although the shader code didn't change.

Measuring Performance in Windows with C++

Posted by Kaya Kupferschmidt • Friday, March 11. 2005 • Category: C++
Currently I try to make up some performance measurement instruments for C++ programs running under Windows. But the whole project turns out to be more complicated than I first thought - which explains why I haven't found a good solution yet. But let me explain the obstacles one will stumble over alomst immediately, especially if you want to profile routines that do not need much CPU time.

First what are the requirements of a good time-based profiler?

  • High temporal resolution. When one wants to profile a single function, a high temporal resolution of the timing mechanism is indispensable for valid results. Especially with computers getting faster and faster, the timer should be capable of capturing events within the range of 100ns.

  • Minimal overhead. If you want to measure very small time intervalls, the capturing mechanism has to be very fast in order to keep its own influence minimal.

  • Impassable of context switches. Today every operating system performs preemptive multitasking, that means the current running thread could be interrupted at any point. If this just happens within a function of which you are collecting run-time information, the timing mechansism must take into account the fact that not all CPU cycles between the start and the end of the profiling are were used to execute this function.

  • Independance of CPU in SMP systems. In SMP systems (or even in Hyperthreading systems) the current thread may be executed by different CPUs after each context switch. If the timing mechanism is relying on some CPU-specific data (like the number of cycles executed), special care must be taken to either incorporate such CPU switches or to simply attach a thread to exactly one CPU.

  • Independance of Power Saving modes. Another problem arises if power-saving modes are enables, which might slow down the core frequency of the CPU and thus falsify the profiling results.


As you can imagine a mechanism that is independant of all such side-effects mentioned above is not very simple to implement and probably requires special support from the operating system.

Continue reading "Measuring Performance in Windows with C++"

Using Templates instead of virtual functions

Posted by Kaya Kupferschmidt • Friday, March 4. 2005 • Category: C++
Today I want to present a C++ technique that helps to improve performance for time critical applications and still offers a certain degree of generality. I have successfully used this technique in my graphics project Project Magnum.

Before we jump into the details, let us first analyse the problem. Imagine you have written a fairly general class or algorithm that could be used for some different tasks. The classical approaches in C++ to enable different specialisations is either to use templates or to use virtual functions, with each of the two solutions having its own strengths and weaknesses. So let us have a closer look at this two techniques in order to expose the differences.

Continue reading "Using Templates instead of virtual functions"


A Simple Sidebar