Re: [PATCH 2/2] Replace literal STRLEN_ #defines in refs.h with compiler evaluated expressions
- From
Linus Torvalds <torvalds@linux-foundation.org>
- Date
- Sep 29, 2007, 18:15 UTC
- Message-ID
- <alpine.LFD.0.999.0709291103380.3579@woody.linux-foundation.org>
- In-Reply-To
- <7vd4w1l872.fsf@gitster.siamese.dyndns.org>
On Sat, 29 Sep 2007, Junio C Hamano wrote:
Show 6 quoted lines
> > I do not think the above is wrong per se, but doesn't a good > compiler optimize > > #define PATH_REMOTES "remotes/" > #define STRLEN_PATH_REMOTES strlen(PATH_REMOTES)
Gcc does, yes.
HOWEVER.
Using "strlen()" may be optimized at compile-time, but it still ends up resulting in illegal C code if the constant needs to be a constant in the semantic sense.
IOW, the "sizeof()" trick can be portably used for things like array declarations etc. But strlen() cannot. Ie
char array[sizeof("hello")-1];is legal in non-function scope, but doing the same with "strlen()" is not.
(That said, gcc has been known to accept bad C code, and will in fact accept this one too!)
Linus