language-lawyer

Allocating memory for a part of structure

与世无争的帅哥 提交于 2020-03-14 07:03:51
问题 I have the following example #include <stdlib.h> #include <stdio.h> #include <stddef.h> typedef struct test{ int a; long b; int c; } test; int main() { test *t = (test*) malloc(offsetof(test, c)); t -> b = 100; } It works fine, but Im not sure about it. I think I have UB here. We have a pointer to an object of a structure type. But the object of the structure type is not really valid. I went through the standard and could not find any definition of this behavior. The only section I could find

Can I override a virtual function with a pure virtual one?

感情迁移 提交于 2020-03-13 04:42:33
问题 I have three classes: B , D and G . D is a B and G is a D . Both B and D are abstract. B is from a third party. B has a non-pure, virtual method that G needs to implement (to be a D ). Can I and is it good practice to redefine/override a virtual function to be pure virtual? Example: class B // from a third party { public: virtual void foo(); }; class D : public B { public: void foo() override = 0; // allowed by gcc 4.8.2 virtual void bar() = 0; }; class G : public D { public: // forgot to

Can I use NULL as substitution for the value of 0?

这一生的挚爱 提交于 2020-03-12 11:46:09
问题 Am I allowed to use the NULL pointer as replacement for the value of 0 ? Or is there anything wrong about that doing? Like, for example: int i = NULL; as replacement for: int i = 0; As experiment I compiled the following code: #include <stdio.h> int main(void) { int i = NULL; printf("%d",i); return 0; } Output: 0 Indeed it gives me this warning, which is completely correct on its own: warning: initialization makes integer from pointer without a cast [-Wint-conversion] but the result is still

Can I use NULL as substitution for the value of 0?

穿精又带淫゛_ 提交于 2020-03-12 11:43:39
问题 Am I allowed to use the NULL pointer as replacement for the value of 0 ? Or is there anything wrong about that doing? Like, for example: int i = NULL; as replacement for: int i = 0; As experiment I compiled the following code: #include <stdio.h> int main(void) { int i = NULL; printf("%d",i); return 0; } Output: 0 Indeed it gives me this warning, which is completely correct on its own: warning: initialization makes integer from pointer without a cast [-Wint-conversion] but the result is still

Can I use NULL as substitution for the value of 0?

∥☆過路亽.° 提交于 2020-03-12 11:43:29
问题 Am I allowed to use the NULL pointer as replacement for the value of 0 ? Or is there anything wrong about that doing? Like, for example: int i = NULL; as replacement for: int i = 0; As experiment I compiled the following code: #include <stdio.h> int main(void) { int i = NULL; printf("%d",i); return 0; } Output: 0 Indeed it gives me this warning, which is completely correct on its own: warning: initialization makes integer from pointer without a cast [-Wint-conversion] but the result is still

Will consteval functions allow template parameters dependent on function arguments?

孤街浪徒 提交于 2020-03-10 10:50:02
问题 In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant<int, i>::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at runtime, thus making the template instantiation impossible. In C++20 we will have consteval functions, which are required to be evaluated at compile-time, so the runtime constraint should be removed. Does it mean this code will be legal? consteval int foo(int

Will consteval functions allow template parameters dependent on function arguments?

五迷三道 提交于 2020-03-10 10:48:31
问题 In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant<int, i>::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at runtime, thus making the template instantiation impossible. In C++20 we will have consteval functions, which are required to be evaluated at compile-time, so the runtime constraint should be removed. Does it mean this code will be legal? consteval int foo(int

Was `long` guaranteed to be as wide as `size_t`

让人想犯罪 __ 提交于 2020-03-01 05:16:12
问题 When looking for evidence of unsigned long being enough to hold size_t for the purpose of being argument to printf I ran into two fact(oid)s. First there's this answer stating that long is indeed not guaranteed to be large enough for size_t . On the other hand I saw this answer suggesting to use printf("%lu", (unsigned long)x) in pre C99, x being of size_t . So the question is could you assume or were you guaranteed that long were enough to hold size_t in pre C99 . The other question is

Was `long` guaranteed to be as wide as `size_t`

僤鯓⒐⒋嵵緔 提交于 2020-03-01 05:16:09
问题 When looking for evidence of unsigned long being enough to hold size_t for the purpose of being argument to printf I ran into two fact(oid)s. First there's this answer stating that long is indeed not guaranteed to be large enough for size_t . On the other hand I saw this answer suggesting to use printf("%lu", (unsigned long)x) in pre C99, x being of size_t . So the question is could you assume or were you guaranteed that long were enough to hold size_t in pre C99 . The other question is

Was `long` guaranteed to be as wide as `size_t`

前提是你 提交于 2020-03-01 05:15:09
问题 When looking for evidence of unsigned long being enough to hold size_t for the purpose of being argument to printf I ran into two fact(oid)s. First there's this answer stating that long is indeed not guaranteed to be large enough for size_t . On the other hand I saw this answer suggesting to use printf("%lu", (unsigned long)x) in pre C99, x being of size_t . So the question is could you assume or were you guaranteed that long were enough to hold size_t in pre C99 . The other question is