It's also OK if a or b is void* or such. Probably not if a char*, I probably want a warning on that. Some good heuristics are necessary.
-
Show this thread
-
I recognize the pattern memcpy(a, b, sizeof(*b)), but if the type of a or b changes, the other also needs to change.
1 reply 0 retweets 0 likesShow this thread -
memcpy(a, b, sizeof(type)) is sort of a more neutral statement that asserts this code expects that type. I want to validate on type change.
2 replies 0 retweets 1 likeShow this thread -
Replying to @sortiecat
Why not just write *a=*b if the point isn't to avoid type constraints?
1 reply 0 retweets 0 likes -
Replying to @RichFelker @sortiecat
for copying structs, for most compilers, this seems like a "more likely correct" solution (ex: if memcpy is handled poorly, ...).
1 reply 0 retweets 1 like -
Replying to @cr88192 @RichFelker
Yes, exactly. Unfortunately C doesn't have struct assignment by value, which is a shame. Feels like a C-y operation to me and not C++-like.
1 reply 0 retweets 0 likes -
Replying to @sortiecat @cr88192
Huh? Of course C has struct assignment by value.
1 reply 0 retweets 1 like -
Replying to @RichFelker @cr88192
Huh. You're right. Not sure where I got this impression from. Yay. Though C doesn't have struct == so my memcmp concerns still exist.
2 replies 0 retweets 1 like -
Type-checking memcpy(a, b, sizeof(*a)) is *a = *b. Type-checking memcmp(a, b, sizeof(*a)) is (a == b & 0) || memcmp(a, b, sizeof(a)). Cool!
1 reply 1 retweet 1 like -
The & 0 can be removed if you're sure a != b or do a == b || memcmp(a, b, sizeof(*a)) == 0 to check for a true comparison condition.
1 reply 0 retweets 0 likes
!= should work fine too.
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.