Declaring C String Constants
eklitzke.org
Declaring C String Constants
1–10 of 87 posts
Re: Declaring C String Constants
#2 .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
#3Re: Declaring C String Constants
#4Or you could just #define STR "Lorem Ipsum" and be sure that it's going to be efficient.
Re: Declaring C String Constants
#5Re: Declaring C String Constants
#6Guessing about pipelining, memory access times, and the effect of generated code size is much less valuable than real measurements.
Re: Declaring C String Constants
#7Any 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
#8Interestingly, 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…
Re: Declaring C String Constants
#9In 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
#10Any 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.