Any programming language that needs an entire blog post about how to declare string constants "the right way" is a programming language that needs to disappear.
Declaring C String Constants
11–20 of 87 posts
Re: Declaring C String Constants
#12Re: Declaring C String Constants
#13Re: Declaring C String Constants
#14Or you could just #define STR "Lorem Ipsum" and be sure that it's going to be efficient.
This is a joke, right?
Re: Declaring C String Constants
#15Re: Declaring C String Constants
#16Any programming language that needs an entire blog post about how to declare string constants "the right way" is a programming language that needs to disappear.
Re: Declaring C String Constants
#17Re: Declaring C String Constants
#18Any programming language that needs an entire blog post about how to declare string constants "the right way" is a programming language that needs to disappear.
C is not that far removed from assembly, and gives the programmer strong control over how the program operates. It is a tool, and if you need safety over control / performance / portability / etc., then don't use C.
Re: Declaring C String Constants
#19While I do enjoy this sort of analysis, I feel the decision to choose one method over another should be based on benchmarks (preferably with more than one size of string.) After benchmarking, then do an analysis of the assembly code. Guessing about pipelining, memory access times, and the effect of generated code size is much less valuable than real measurements.
&ptr
and &arr
are different types! The signature for bogus(arr, &arr)
is something like void bogus(const char* arg1, const char (*arg2)[12])
While they do point to the same data, it's pretty rare to actually want to declare a const char (*)[N]
as a function argument - template magic code excepted, you don't gain much over a char *
and it's not the same as a char**
The two lines do different things, so of course they will generate different code. What do you know, the one that actually lets you do useful things is a few bytes longer.Re: Declaring C String Constants
#20All this talk about do_ptr being slower because it copies from main memory is misleading. Yes, it's slower, because it's doing something different than what do_arr does. It's not just a slower version of doing the same thing. So trying to compare do_ptr and do_arr makes no sense. And the whole thesis of this piece, that declaring string constants as arrays is better, has no supporting evidence. You know what's better…
I would like to see more empirical measurement in articles like this. There has to be a reason both compiler decided to emit that pointer based code, surely there is some advantage.