Live data from Hacker News

Declaring C String Constants

eklitzke.org

1–10 of 87 posts

Re: Declaring C String Constants

#2
Interestingly, we get

        .LC0:
                .string "Lorem ipsum"
        [...]
                movl    $.LC0, %esi
                movl    $ptr2, %edi
                xorl    %eax, %eax
                jmp     bogus
for the same experiment done with a const pointer to const data,

        const char * const ptr2 = "Lorem ipsum";
(Incidentally, this kind of thing is why I prefer to write 'char const STAR const' rather than 'const char STAR const', and 'char const STAR' rather than 'const char STAR'; it systematically places the 'const's.)

(PS. HN really doesn't work well for trying to write either unicode or ascii star characters.)

Re: Declaring C String Constants

#6
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.

Re: Declaring C String Constants

#7

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.

Any poster that needs to post comments about how any language requiring real understanding needs to disappear needs to disappear.

Re: Declaring C String Constants

#8
post #2

Interestingly, we get .LC0: .string "Lorem ipsum" [...] movl $.LC0, %esi movl $ptr2, %edi xorl %eax, %eax jmp bogus for the same experiment done with a const pointer to const data, const char * const ptr2 = "Lorem ipsum"; (Incidentally, this kind of thing is why I prefer to write 'char const STAR const' rather than 'const char STAR const', and 'char const STAR' rather than 'const char STAR'; it systematically places…

Hence the need to compile with -O2 rather than -O3, I would assume.

Re: Declaring C String Constants

#9
Arrays and pointers are not the same in C, nor have they ever been. The difference is more than that of mere syntax choices for equivalent concepts.

In the pointer version of the code, the pointer is reassignable. Only the data it references can't be modified, the pointer can. To prevent the pointer from being modified, the declaration should have been:

const char * const ptr = "Lorem ipsum";

With the array version, there is no pointer to be modified and so no need or ability to have a second const.

Re: Declaring C String Constants

#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.
Post reply on HN