-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
void * vs char * vs uint8_t * #5497
Comments
I'll start collecting what I know:
Subjective stuff:
|
|
Fun fact: char != signed char != unsigned char. Whether |
I think we should stick to the common sense and try to be consistent with the standard C coding style. |
They revised that in C99:
So char for us (--std=c99) is either signed char or unsigned char, or is it really a third type despite having the same range as signed or unsigned char? edit it is a third type, just read my own quote. ;) |
What about |
do you have a more concrete example for that case? |
|
yeah I understand the behaviour, the thing is that I cannot see how |
My usage of these types of parameters is atm:
|
Ah, I mean for example if we'd use char* for incoming serial bytes, and wait for a control character (like, 0xff), this would need not always obvious casting when comparing to one of the "characters" the pointer points to. |
Just to make my conventions a little clearer (and I argue that at least in my work I'm pretty consistent with that): unless the type is 100% clear (e.g. I expect an IPv6 header) I use |
Doesn't affect uint8_t as 0xff is a perfectly valid value there. |
unless the type is 100% clear (e.g. I expect an IPv6 header) I use |void *|.
Do you mean the expected type within the function or at the call site?
e.g., for UART, SPI, ... the callee always expects a byte array, but the
caller might send any type.
|
Both, but primarily on the caller side. A case were it even isn't that clear for the callee would be |
You do monospace by using |
does it work when replying via email: |
No, doesn't seem like it. |
So whenever a buffer (which is equivalent to byte array) is expected, we'd use |
I would opt for keeping all three around (and use them reasonably), too. |
PS: |
👎 in any case I use it anything else doesn't make sense.
Huh? That was exactly the opposite of what I said, nor do I propose anything. I use
For the first part I agree, but I'm not quite sure what we need to be "100% sure". When the situation arises, one look into the function's code makes clear, why I used a |
Oh btw, just remember another thing I generally use |
I know. ;) But you also said that you'd make the decision primarily based on the caller side expected type, which is contradicting. I thought we could clear that up. |
I reject your reality and substitute my own ;-). No but seriously, I thought it was clear: When the type from the caller can be expected to be anything, than I use |
I feel we're getting somewhere:
|
Is not wanting to convert all the usages of |
@Stargateur @liuziangexit of course you're right, C defines Is anyone trying to use RIOT on such a platform? |
@kaspar030 I was just talking about semantic, if #include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
bool check(uint8_t *a, uint32_t *b)
{
*a = 5;
*b = 6;
if (*a == 5) {
return true;
}
return false;
}
int main(void)
{
uint32_t foo;
// This is undefined behavior
printf("%s", check((uint8_t *)&foo, &foo) ? "true" : "false");
} However a lot of compiler do not optimize this cause a lot of people think |
yes, the CPP standard says you can only use the std::byte, uchar and char to access an
obj, otherwise is UB(include using union, which is legal in C99 but illegal
in CPP
…On Sun, Jul 1, 2018 at 7:57 PM Antoine PLASKOWSKI ***@***.***> wrote:
@kaspar030 <https://github.com/kaspar030> I just talk about semantic, if
uint8_t is available, CHAR_BIT would be 8. But keep in mind that uint8_t
do not allow you to alias <https://stackoverflow.com/q/98650/7076153>
other type contrary to char.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5497 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AOnNUjtLMOqILKT1hfRtbsRqZhTxMpneks5uCLkRgaJpZM4IsrxN>
.
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
I thought "uint8_t" doesn't have to be a character type. It is when typedef'ed to "(signed) char", right? |
Yes but you will rely on implemented behavior, in my projet I avoid that, and personally I think it's a very bad way to implement |
Just to be clear, it only disallows optimization in case uint8_t is not a typedef'ed char? |
no that the contrary, that will not allow optimisation, if You can see https://stackoverflow.com/questions/57259126/why-does-the-rust-compiler-not-optimize-code-assuming-that-two-mutable-reference, Rust should assume nothing alias on most case too. But does not currently. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
Looking at the code base and recent changes, there seems to be a consensus now:
If anyone disagrees, please reopen. |
We do use the three inconsistently.
I would like to collect facts on what we know about each other, then make an informed decision on when to use what, then make the uses consistent.
The text was updated successfully, but these errors were encountered: