[C/C++] Floating point comparisons with NaN giving inconsistent results

You might notice that when comparing with NaN, the comparison gives wrong results at least for x64 builds. Take a look at the following piece of code. If you run this sample application the comparison statement if(lfv==0.0) returns true and the MessageBox is displayed. (We had a customer who reported this behavior.) #include “stdafx.h” #include […]
Continue reading…

 

C/C++ Interview Question: Why Executable file differs in size

I have following code snippets… Code snippet 1 int arr[1000000]; int main() { return arr[0]; } Code snippet 2: int arr[1000000] = {10}; int main() { return arr[0]; } After compilation of the above snippets the output exe differs in size. The second snippet’s executable is much bigger in size than the first snippet’s executable. […]
Continue reading…

 

Technical Interview question: Stack growth direction

This one is a brain teaser for my native programmers. Prove via code the direction of a threads’ stack growth i.e is it upwards or downwards. Post your answer as a comment. I’ll post mine in a while.
Continue reading…

 

Retrieve handle type string using NtQueryObject

Hmm so after a long time I’m back with a new post. This time I came across a cool API called NtQueryObject. One purpose of the API is to find the name of object type it’s given. For example if you are passing a process handle this API will return “Process” as the string. The […]
Continue reading…

 

Functions for converting strings to upper/lower case

Use following functions _strupr _strlwr std::transform – does the trick too but hard to understand [sourcecode language=’cpp’]/* STRLWR.C: This program uses _strlwr and _strupr to create  * uppercase and lowercase copies of a mixed-case string.  */ #include #include void main( void ) { char str[100] = “The String to End All Strings!”; printf( “Mixed: %sn”, […]
Continue reading…

 

How to check for keyhit in a windows console application?

For this purpose we use _kbhit function included in conio.h file. Please note that conio.h is a non-standard header file so can’t say if _kbhit will be available in other libraries. The purpose of  _kbhit is to check whether a key has been hit and so then we can use this function to wait till […]
Continue reading…

 

Careful with sscanf and fscanf

If you are not careful while using sscanf and fscanf, you code could lead to stack corruption errors leading to application crash. Recently when migrating a project in VC6 to VC8, a similar situation arose, I was getting stack corruption errors… “Run-Time Check Failure #2 – Stack around the variable ‘ByteVar’ was corrupted.” So I […]
Continue reading…

 

typedef a fixed length array

Let’s suppose you wanna create a typedef for a fixed length array, so first thing that you would do is this… [sourcecode language=’cpp’]typedef char[100] HundredChars;[/sourcecode] This will not work as typedefing an array requires different syntax, here is how we do it… [sourcecode language=’cpp’]typedef char HundredChars[100];[/sourcecode]
Continue reading…

 

Choosing between a function and a macro definition

This is a common and a funny problem. So what is this all about? Let me put it like this… We have a standard “c” function called _toupper and an equivalent macro is also available called _toupper. So my question is how to call function version of _toupper explicitly, if we write _toupper, macro version  […]
Continue reading…