For example, if I declare int num = 7;
and only use it in a way that it could have and should have been declared const char num = 7;
since char uses less memory than int, will gcc optimize that or does it not even matter since it will be converted to assembly? I realize that in this case, the variable would be optimized out completely but could there ever be a scenario where gcc decides a variable is better as a different type?
Asked
Active
Viewed 121 times
1
-
Do you want optimization for speed or for space? What GCC might choose to do depends on the options you pass to the frontend program. It also depends on the target platform, alignment requirements and possibly many other things. In short, there's no simple "yes" or "no" answer to this. – Some programmer dude Sep 01 '17 at 14:03
-
Modern compilers do weird stuff while optimising, short of completely omitting a variable, I would not put what you are thinking of past GCC. However, I am pretty sure that if you do any kind of dynamic assignments (calculations, user input...) then no compiler should dare to shrink width of a variable, because of not being able to tell whether wider data come in at runtime. – Yunnosch Sep 01 '17 at 14:05
-
4`char` uses less storage space but operations on it most likely involve more CPU operations than `int`. So replacing `int` with `char` would slow program down while reducing its size. – el.pescado - нет войне Sep 01 '17 at 14:05
-
IMO, it is very unlikely that GCC would change the type of an object whose type is explicitly chosen by the programmer. – Jonathan Leffler Sep 01 '17 at 14:05
-
@Someprogrammerdude I'd like to know if it could do it either optimizing for speed or optimizing for space. I'm really wondering if it is actually any more efficient speed or spacewise to use char or short ever instead of int or if gcc will just do it for me – Dom Sep 01 '17 at 14:08
-
I'm not that familiar with the optimization options of GCC, but I do know that `-Os` is used to optimize for size *of the executable*. There might be other more specific flags that causes runtime-size optimizations. – Some programmer dude Sep 01 '17 at 14:12
-
You could check generated assembly. See https://stackoverflow.com/questions/137038/how-do-you-get-assembler-output-from-c-c-source-in-gcc. – dbrank0 Sep 01 '17 at 14:31
-
1Unless you have lots of them, using `char` instead of `int` saves neither time nor space. The compiler has to emit extra code to convert back and forth, and that code takes more space than the `char` saves. Worrying about using a `char` as a tiny int is a great example of a pointless, unnecessary, counterproductive microoptimization. – Steve Summit Sep 01 '17 at 14:56
-
"could have and should have been declared `const char`" -- I disagree with the "should have" part. – Steve Summit Sep 01 '17 at 14:57
-
1to optimize for speed or space use [`[u]int_fastN_t`](https://stackoverflow.com/q/8500677/995714) and [`[u]int_leastN_t`](https://stackoverflow.com/q/20373575/995714) instead http://en.cppreference.com/w/c/types/integer But in this case if the compile realizes that the variable is not modified anywhere it may simply replaces with an immediate value. In other cases it may not uses memory at all and leave the value in registers – phuclv Sep 01 '17 at 14:59
-
@SteveSummit why? Wouldn't it always make sense to declare a variable as the smallest type that can hold what is needed or else you would be wasting space. If the answer to the question is that gcc does optimize it then it wouldn't matter and "should have" could be taken away (ignoring code clarity). – Dom Sep 01 '17 at 15:01
-
1@Dom Simply stated, no, it does not necessarily make sense. By that logic (a) you should have a single-seat car that you always drive when you don't have any passengers, (b) you should always compress all your files after you edit them, and uncompress them whenever you want to view them, (c) you should always pay for a $7 item with a seven dollar bill, because using a 10 or a 20 is "wasteful"... – Steve Summit Sep 01 '17 at 15:07
-
1@Dom: Odds are that space will get wasted *anyway*; on most modern architectures, objects are aligned on multi-byte boundaries. You may only be using the low 8 bits of a given word, but that doesn't necessarily mean the upper bits are available for anything else to use. – John Bode Sep 01 '17 at 18:18
1 Answers
4
If there's no observable difference in the program's side-effects, then the compiler is entitled to make whatever optimisations it likes. The 7
might never appear in the generated code (for example, if you use it only in expressions such as n+3
) and it needn't have any storage if you never take its address.

Toby Speight
- 27,591
- 48
- 66
- 103