May 212011
 

For years, I’ve tortured DigiPen grads and intern candidates alike with this whiteboard interview question. Frankly, I’m sick of it and I need to switch things up. I’d expect senior engineers to get it fairly quickly, and on-the-metal types should just know this by rote. I wanted to see if the junior engineers could think through the problem logically, be circumspect in their reasoning, and if they were comfortable with pointers and basic compiler concepts. So here goes…

Given a simple C-structure:

struct foo_t
{
    int a;
    float b;
    char c[3];
    void * zPtr;
};

I would like you to write some code that outputs the number of bytes from the top of the structure to the beginning of the zPtr structure member. Your answer should be portable, fast, and resilient to code changes.

Aaaand Go! When you think you have it, the answer is over here.

 

Apr 182011
 

I had this kicking around on my hard drive, so I thought I ‘d throw it out there. It’s not exactly a big secret, but it certainly amused me when I wrote it. Use CWARNING and CERROR to add compile-time items to the Error List window in Visual Studio.

This is Visual Studio specific, but can be adapted to work with other compilers and environments. The use of __pragma appears to be Microsoft-specific, but the C99 and C++11 standards introduce the _Pragma keyword.

#ifndef _COMPILEWARNINGS_H_
#define _COMPILEWARNINGS_H_

#define __NUMTOSTRINGHELPER2(x)	#x
#define __NUMTOSTRINGHELPER(x)	__NUMTOSTRINGHELPER2(x)
#define __FILE_AND_LINE__       __FILE__"(" __NUMTOSTRINGHELPER(__LINE__) ")"

#define __ADDWARNING__(msg)     __FILE_AND_LINE__" : warning : " msg
#define __ADDERROR__(msg)       __FILE_AND_LINE__" : error : " msg
// edit 2014-24-02 - CINFO is not correct...still looking for the answer here
#define __ADDINFO__(msg)        __FILE_AND_LINE__" : info : " msg

#define CWARNING(msg)           __pragma(message(__ADDWARNING__(msg)))
#define CERROR(msg)             __pragma(message(__ADDERROR__(msg)))
#define CINFO(msg)              __pragma(message(__ADDINFO__(msg)))

#endif // _COMPILEWARNINGS_H_
CWARNING("This is just a warning");
CERROR("If you get this error, call for help and hide under your desk!!");