Live data from Hacker News

Declaring C String Constants

eklitzke.org

11–20 of 87 posts

Re: Declaring C String Constants

#11

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.

I hope you're not also against programming languages that don't allow you to specify if a string reference needs to be constant or not, or there'd be no programming languages left.

Re: Declaring C String Constants

#12
All 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? Don't dereference a pointer if you don't need to. That's it.

Re: Declaring C String Constants

#14
post #3

Or you could just #define STR "Lorem Ipsum" and be sure that it's going to be efficient.

This is a joke, right?

Why would it be a joke? Using `#define` for a string constant is perfectly reasonable for C code, IMO I'd argue it's the idiomatic and best way to do it. String constants that are `#define`d can take advantage of C's automatic string-literal concatenation, allowing you to avoid lots of unnecessary string operations in some cases. If you declare it as a pointer or array instead, then you can't do that.

Re: Declaring C String Constants

#16

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.

You're totally right. But wannabe C experts will never accept that. Stupid broken language. The only valid reason why it should be used is for legacy reasons.

Re: Declaring C String Constants

#17
I wonder how the results would have differed if those were automatic variables instead of static? That's a much more real-world situation. You wouldn't be able to ignore the overhead of re-initializing the array, since it would be occurring at run time instead of compile time.

Re: Declaring C String Constants

#18
post #10

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.

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.

Being a language not far form assembly shouldn't mean being broken and error prone.

Re: Declaring C String Constants

#19

While 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.

Beyond this,

  &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

#20
post #12

All 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…

Wasn't that sort of the point or using arrays instead of pointers? There is less dereferencing to get to the data, so you need to dereference fewer pointers.

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.

Post reply on HN