Search code examples
cmemoryc-preprocessoriar

Different Memory-need in case of using "const" and "#define" in IAR


case 1:
Defining all constants as preprocessor definition using #define like:

  #define  x          12 

Memory usage (from file_name.lst)

 2 240 bytes of CODE memory
   920 bytes of DATA memory

case 2:
Defining all constants as compiler definition using const unsigned int like:

  const unsigned int x = 12;

Memory usage (from file_name.lst)

 2 240 bytes of CODE memory
     1 byte  of CONST memory
   920 bytes of DATA memory

My Questions:
Where are preprocessor definitions saved? Flash, RAM, somewhere else?
Does it mean that preprocessor definition is a way to decrease memory-usage?


Solution

  • The preprocessor definitions are resolved by the preprocessor. That means the preprocessor will replace every occurance of the defined name (i.e. x) in your source code by the defined value (i.e. 12). The preprocessor runs before the compiler (hence the name preprocessor). During compile time and run time there is no more x, the compile just sees the 12 in places where you wrote x.