-5

I am trying to write some C code for embedded programming purpose where memory is very restricted that I need to reduce code size as much as possible.

then I have a few questions regarding code size

1. does length of variable name matter for code size? 
   Is it good to keep variable name short for embedded programming?

2. Does each letter count as 1 byte for actual code size?
   for ex. 'if' statement, is it counted as 2 byte?
   when code to loaded to memory is prepared how letters including key words count?
in His Steps
  • 3,075
  • 6
  • 30
  • 38
  • 5
    No on both counts. Do you know how a compiler works and what machine code is? – Thilo Oct 24 '14 at 00:46
  • I am not really familiar with compiler and machine code – in His Steps Oct 24 '14 at 00:47
  • 1
    Most compilers offer an option to emit the generated assembly code (eg, with gcc that's the -S option - discussed [here](http://stackoverflow.com/questions/137038/how-do-you-get-assembler-output-from-c-c-source-in-gcc)). That can help you understand how your C code gets translated into machine instructions. – fvu Oct 24 '14 at 00:59
  • 1
    As Thilo notes, your assumptions about how C compilers work are incorrect. How restricted is memory? What's the current breakdown of code/data/stack/heap ? – Andrew C Oct 24 '14 at 02:09
  • 1
    'I am not really familiar with compiler and machine code' - you are unable to perform this task. – Martin James Oct 24 '14 at 04:22
  • 2
    I'd strongly recommend any programmer to study some assembler or hardware-related programming, to learn how computers and programs actually work in reality, below the fluff of high level languages. – Lundin Oct 24 '14 at 06:55

2 Answers2

4

Optimizing code and data size is usually a task for an experienced (5–10+ years of experience) software engineer. One needs a thorough understanding of all levels of a system to make changes which reduce code size (or static data size, stack size, heap size, etc.) by other than blind guesses.

Such an understanding would preclude all of your questions since compiled code does not have variable names in it at all. And the source code is translated in several steps to machine instructions: the source code does not exist on the target system at all.

Some simple techniques of reducing code size are:

  • identify duplicated operations and fold them into a common function
  • remove unused code
  • simplify or reduce the complexity of the application
  • remove features based on how much code each uses

Some of these are easy. Some can be very difficult to evaluate.

wallyk
  • 56,922
  • 16
  • 83
  • 148
1

Keywords and variable names do not matter at all. What does matter is:

  • Size of structs, strings, constants, arrays (if they are static or initialized large)
  • Header files that may include unnecessary structs / strings / etc
  • Resources like bitmaps etc
Grantly
  • 2,546
  • 2
  • 21
  • 31
  • What about number of function calls? And I think memory allocated from heap doesn't count in as code size is that correct? – in His Steps Oct 24 '14 at 00:50
  • Thats correct, heap does not count as code. But Stack does, especially if it includes constants... With respect to number of function calls...it really depends on the function. Some are entirely lightweight like inline functions, and some use big stacks...That part is code specific – Grantly Oct 24 '14 at 00:52
  • Why the downvote? Is it about heaps not being code? Well I agree - thats not entirely accurate...Heaps are code when the code is running, but not before - except for static variables / declarations. – Grantly Oct 24 '14 at 01:08
  • I'm not the downvote, but for many (maybe even most) applications minimizing heap size is far more important than minimizing .text/.data. Also inline functions of anything other than getters/setters is going to consume more space than a function call. – Andrew C Oct 24 '14 at 01:23
  • Yes @AndrewC I can see your point. I guess I was thinking tiny and minimal use inline functions, which defeats the purpose really as they are used often. And the heap - being for working global memory - I see your point too. Thanks – Grantly Oct 24 '14 at 01:27