undefined-behavior

Are a+=k and a=a+b different in c?

丶灬走出姿态 提交于 2020-04-16 02:44:05
问题 I tried to swap without a template and I encountered this. Are a+=k and a=a+b different? What is wrong for first case? a += b-(b=a); // this print same value of two a and b. a = a + b-(b=a); // this thing correctly swapped values. 回答1: Both result in undefined behavior because you're both accessing and modifying b without an intervening sequence point: 6.5p2: If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value

Is transmuting bytes to a float safe or might it produce undefined behavior?

依然范特西╮ 提交于 2020-04-13 07:28:26
问题 Are there byte-sequences that, when transmuted into either f32 or f64 , produce undefined-behavior in Rust? I'm counting non-finite values, such as NaN, Infinity, etc. as valid floating point values. The comments to this answer hint that there may be some problem converting a float from raw bytes. 回答1: The Rust reference provides a good list of situations where undefined behavior occurs. Of those, the one that most closely relates to the question is the following: Invalid values in primitive

How can I read a signed integer from a buffer of uint8_t without invoking un- or implementation-defined behaviour?

心已入冬 提交于 2020-03-18 04:50:27
问题 Here's a simple function that tries to do read a generic twos-complement integer from a big-endian buffer, where we'll assume std::is_signed_v<INT_T> : template<typename INT_T> INT_T read_big_endian(uint8_t const *data) { INT_T result = 0; for (size_t i = 0; i < sizeof(INT_T); i++) { result <<= 8; result |= *data; data++; } return result; } Unfortunately, this is undefined behaviour, as the last <<= shifts into the sign bit. So now we try the following: template<typename INT_T> INT_T read_big

Is it safe to call the functions from <cctype> with char arguments?

狂风中的少年 提交于 2020-03-17 11:55:32
问题 The C programming language says that the functions from <ctype.h> follow a common requirement: ISO C99, 7.4p1: In all cases the argument is an int , the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF . If the argument has any other value, the behavoir is undefined. This means that the following code is unsafe: int upper(const char *s, size_t index) { return toupper(s[index]); } If this code is executed on an implementation where char has

Allocating memory for a part of structure

我与影子孤独终老i 提交于 2020-03-14 07:04:50
问题 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

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

C++ delete - It deletes my objects but I can still access the data?

微笑、不失礼 提交于 2020-03-05 09:31:32
问题 I have written a simple, working tetris game with each block as an instance of a class singleblock. class SingleBlock { public: SingleBlock(int, int); ~SingleBlock(); int x; int y; SingleBlock *next; }; class MultiBlock { public: MultiBlock(int, int); SingleBlock *c, *d, *e, *f; }; SingleBlock::SingleBlock(int a, int b) { x = a; y = b; } SingleBlock::~SingleBlock() { x = 222; } MultiBlock::MultiBlock(int a, int b) { c = new SingleBlock (a,b); d = c->next = new SingleBlock (a+10,b); e = d-

Copying structs with uninitialized members

限于喜欢 提交于 2020-02-26 08:48:30
问题 Is it valid to copy a struct some of whose members are not initialized? I suspect it is undefined behavior, but if so, it makes leaving any uninitialized members in a struct (even if those members are never used directly) quite dangerous. So I wonder if there is something in the standard that allows it. For instance, is this valid? struct Data { int a, b; }; int main() { Data data; data.a = 5; Data data2 = data; } 回答1: Yes, if the uninitialized member is not an unsigned narrow character type

Methods to convert `void *` function parmeter inconsistant from type to type

人走茶凉 提交于 2020-02-22 19:21:55
问题 Note: This question attempts to improve what I attempted to ask here, but fell short. Also, I have seen this, and this. They discuss similar concepts, but do not answer these questions. My environment is Windows 10, and for testing I used two compilers, CLANG and GCC. I am passing variables via a void * function argument, and need to convert them. I would like to get some feedback on inconsistencies I am seeing between methods for different types. The following is a stripped-down depiction of

Methods to convert `void *` function parmeter inconsistant from type to type

末鹿安然 提交于 2020-02-22 19:20:11
问题 Note: This question attempts to improve what I attempted to ask here, but fell short. Also, I have seen this, and this. They discuss similar concepts, but do not answer these questions. My environment is Windows 10, and for testing I used two compilers, CLANG and GCC. I am passing variables via a void * function argument, and need to convert them. I would like to get some feedback on inconsistencies I am seeing between methods for different types. The following is a stripped-down depiction of