Notes
Slide Show
Outline
1
Reading Windows-based Code
  • Part 3
2
Code reading and programming language semantics
  • Syntax
  • Grammar
  • Blondes have black hair  - correct syntax


  • Semantics
  • Meaning


  • delete object; // program crashes here
3
Built-in types
  • Another name: primitive types or fundamental types


  • Type - some abstraction with corresponding semantics (for example, natural and real numbers)


  • Represented as a sequence of bytes


  • Signed or unsigned
4
Signed and unsigned types
  • Signed types
  • When interpreted as numbers can be negative or positive (-12 or 12)
  • FF = 11111111 = -1      7F = 01111111 = 127


  • Unsigned types
  • Interpreted as positive numbers (pointers, because memory addresses start from 0)
5
Built-in types and their sizes
  • Compiler and platform dependent:
  • char (signed)
  • wchar_t (signed) – for Unicode
  • short (signed)
  • int (signed)
  • long (signed)
  • unsigned char
  • unsigned short
  • unsigned int
  • unsigned long
  • bool
  • float (signed)
  • double (signed)
  • MSFT specific: __intN (n=8,16,32 or 64)
  • __int8, __int64
6
Some terminology
  • Operator
  • Manipulate variables and build expressions:  a + 2
  • Operand
  • Variable or other expression: (a + 2) * b
  • Expression
  • Some sentence that combines operands and operators; terminates by ‘;’
  • int a = 5; // initialization expression
  • a = 6; // assignment expression
  • Expression evaluation
  • Applying operators to operands to yield some value
  • Expression type
  • The type of the evaluated value
7
Built-in types declaration and definition
  • Declaration: creates a name, makes the name visible, so we can use it; doesn’t allocate memory
  • extern int counter;
  • counter = 10;
  • Definition: declares a variable and allocates memory
  • int counter;
  • One Definition Rule (ODR): only one definition is allowed but multiple same declarations
  • extern int counter;
  • extern int counter;
  • int counter;
8
The role of preprocessor
9
Separate compilation model
  • Compilation unit
  • C++ file plus included files (usually header files with declarations)


  • ODR across compilation units
  • Linker will complain if we have the same definition in different units because we cannot allocate the memory twice for the same variable (which memory location to use?)
10
Separate compilation model - example
  • // main.c
  • int maxCounter;



  • MaxCounter = 10;
11
Preprocessor directives
  • Include file directive


  • Define and undefine directives


  • Conditional compilation


  • Macros


  • Other directives
12
Include file directive
  • #include “file”
  • Search current directory; search standard directories


  • #include <file>
  • Search only standard directories
13
Define and undefine directives
  • #define name
  • #define DEBUG


  • #define name text
  • #define MAXSTR 256
  • int maxSize = MAXSTR; // replaced by 256


  • #undef name
  • #undef DEBUG
14
Creating new type names
  • #define NewType BuiltInType


  • #define BOOL int
  • BOOL choice;


  • #define HANDLE int
  • HANDLE hFile;


  • #define HWND int
  • HWND window;


  • #define DWORD unsigned long
  • DWORD dwCounter;


15
Type names are not new types (no type safety)
  • #define HANDLE int
  • HANDLE hFile;
  • #define HWND int
  • HWND window;


  • if (hFile == window)
  • {
  • hFile = window;  // no warning during compilation
  • }


  • double debt;
  • int value;


  • value = debt; // warning C4244: '=' : conversion from 'double' to 'int', possible loss of data


16
Conditional compilation
  • #ifdef name


  • #ifdef DEBUG
  • OutputDebugString(“…”);
  • #endif



17
Conditional compilation (…)
  • #ifndef name


  • #ifndef NDEBUG
  • OutputDebugString(“…”);
  • #endif



18
Conditional compilation (……)
  • #ifdef UNICODE
  • #define TCHAR wchar_t
  • #else
  • #define TCHAR char
  • #endif


  • TCHAR character;



19
Conditional compilation (………)
  • Constant expression


  • Any expression that consists only from constants


  • #if  0
  • // exclude code from compilation
  • #endif


20
Macros
  • Parameterized text replacement


  • #define Name(param1, param2, …)


  • !!! No space between Name and ‘(‘


  • #define PRINT(string) OutputDebugString(string)


  • PRINT(“Hello!”);
21
Complex macros
  • #define TRACE(module, string) if (debuggingEnabled) {\
  •  OutputDebugString(module);\
  •  OutputDebugString(“: “);\
  •  OutputDebugString(string);\
  •  OutputDebugString(“\n”); }


  • TRACE(“PROGRAM.EXE”, “Hello!”);


22
Useful debugging macros
  • __DATE__
  • __TIME__
  • __LINE__
  • __FILE__


  • #define TRACE(string) if (debuggingEnabled) {\
  •  OutputDebugString(__FILE__);\
  •  OutputDebugString(“: “);\
  •  OutputDebugString(string);\
  •  OutputDebugString(“\n”); }
23
Other directives
  • #line


  • #error


  • #pragma
24
Things to remember
  • Macros and macro definitions are substituted before compilation


  • Names and macros introduced by define directives are usually fully capitalized (TRACE, DEBUG, MAXSTR)


  • Pay attention to conditional compilation directives