Preference in C between static const char *const and static const char []?
#1504
niooss-ledger
started this conversation in
General
Replies: 1 comment 1 reply
-
|
Modern compilers optimize both to equivalent code. The pointer is eliminated even at the lowest optimization levels, since it is static and never modified. This scrypt code was inherited from the original scrypt implementation by scrypt's author, and there's no functional reason to change it. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
While reading libsodium's code, I stumbled upon
itoa64insrc/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c:libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c
Lines 31 to 32 in a84a99a
I am wondering why the code was written with a pointer instead of a vector:
From my understanding, using a pointer makes the compiler create both a string and a pointer to it, which is less efficient than using the string directly. Nevertheless, when I checked the compiled code (when building for x86-64), the compiler only stored the string. I guess this is due to the fact the compiler was able to optimize uses of
itoa64to remove pointer dereferences (because of the secondconstinstatic const char *const itoa64).Therefore, while the current code is fine, I am wondering whether it was a deliberate choice (or coding style) to use a pointer instead of a vector. Was there a reason to choose this way of defining
itoa64?Beta Was this translation helpful? Give feedback.
All reactions