undefined-behavior

When does invoking a member function on a null instance result in undefined behavior?

和自甴很熟 提交于 2019-12-11 14:34:08
问题 Consider the following code: #include <iostream> struct foo { // (a): void bar() { std::cout << "gman was here" << std::endl; } // (b): void baz() { x = 5; } int x; }; int main() { foo* f = 0; f->bar(); // (a) f->baz(); // (b) } We expect (b) to crash, because there is no corresponding member x for the null pointer. In practice, (a) doesn't crash because the this pointer is never used. Because (b) dereferences the this pointer ( (*this).x = 5; ), and this is null, the program enters undefined

Unable to apply configured parameters for a sound device

ぐ巨炮叔叔 提交于 2019-12-11 13:01:17
问题 I wrote the following simple example with asoundlib.h : #include <alsa/asoundlib.h> #include <sys/fcntl.h> #include <unistd.h> typedef struct sound_device_config_t sound_device_config_t; struct sound_device_config_t{ unsigned int rate; unsigned int periods; snd_pcm_uframes_t period_size; unsigned int channels; snd_pcm_format_t sample_format; }; static void _configure_device(snd_pcm_t **pcm_handle_ptr, const char *sd); int main(int argc, char * argv[]){ sound_device_config_t *cfg_ptr = malloc

C++ confusion about one definition rule

旧城冷巷雨未停 提交于 2019-12-11 11:34:12
问题 I was reading about One Definition Rule. It says that: if one .cpp file defines struct S { int x; }; and the other .cpp file defines struct S { int y; }; , the behavior of the program that links them together is undefined. This is usually resolved with unnamed namespaces. I don't understand why & how it is undefined? Will someone explain me the actual reason behind this? How it is resolved with unnamed namespaces? 回答1: It's just as it says. You defined the same class S twice, with different

Allowed operations on an possibly invalid pointer by the strict interpretation of the C Standard

拥有回忆 提交于 2019-12-11 10:06:09
问题 Original Question (please see "Edit: Updated scenario") This question might be a duplicate in one or another way to a huge collection of questions around undefined behavior of pointers to objects that gone out of scope etc. But all questions I found here to this topic are mostly specialized use cases. So I would like to spin the question upside down, not asking if something is forbidden, but WHAT exactly is allowed? To have a possible scenario: You have a function, that takes a pointer - you

Casting structure pointer to union pointer

混江龙づ霸主 提交于 2019-12-11 05:25:37
问题 I have a union containing various compatible struct members: enum Type { T1, T2, … }; struct S1 { enum Type type; … }; struct S2 { enum Type type; … }; … union U { enum Type type; struct S1 as_s1; struct S2 as_s2; … }; The type field of these structures is treated as immutable, and only the field of the union corresponding to the current type is accessed. I am aware that casting union U * to struct S1 * is defined as long as the type invariant is upheld, but is the reverse also true? struct

Undefined Behavior quirk: reading outside a buffer causes a loop to never terminate?

ぐ巨炮叔叔 提交于 2019-12-11 04:58:12
问题 I wrote a very trivial program to try to examine the undefined behavior attached to buffer overflows. Specifically, regarding what happens when you perform a read on data outside the allocated space. #include <iostream> #include<iomanip> int main() { int values[10]; for (int i = 0; i < 10; i++) { values[i] = i; } std::cout << values << " "; std::cout << std::endl; for (int i = 0; i < 11; i++) { //UB occurs here when values[i] is executed with i == 10 std::cout << std::setw(2) << i << "(" <<

Why does ActionEvent ask for an id?

混江龙づ霸主 提交于 2019-12-11 04:28:20
问题 When making a new ActionEvent , you're required to provide an integer as an id . The documentation says that this is: An integer that identifies the event. For information on allowable values, see the class description for ActionEvent And the class description for ActionEvent says: An unspecified behavior will be caused if the id parameter of any particular ActionEvent instance is not in the range from ACTION_FIRST to ACTION_LAST . My confusion comes in with the values of ACTION_FIRST and

Is this undefined behaviour in C++ calling a function from a dangling pointer

ⅰ亾dé卋堺 提交于 2019-12-11 04:27:37
问题 A question came up here on SO asking "Why is this working" when a pointer became dangling. The answers were that it's UB, which means it may work or not. I learned in a tutorial that: #include <iostream> struct Foo { int member; void function() { std::cout << "hello";} }; int main() { Foo* fooObj = nullptr; fooObj->member = 5; // This will cause a read access violation but... fooObj->function(); // Because this doesn't refer to any memory specific to // the Foo object, and doesn't touch any

Do the strict aliasing rules in C++20 allow `reinterpret_cast` between the standard c++ unicode chars and the underlining types?

巧了我就是萌 提交于 2019-12-11 04:08:57
问题 Do the C++20 's strict aliasing rules [basic.lval]/11 arbitrarily allow following... cast between char* and char8_t* string str = "string"; u8string u8str { (char8_t*) &*str.data() }; // c++20 u8string u8string u8str2 = u8"zß水🍌" string str2 { (char*) u8str2.data() }; cast between uint32_t* , uint_least32_t* and char32_t* vector<uint32_t> ui32vec = { 0x007a, 0x00df, 0x6c34, 0x0001f34c }; u32string u32str { (char32_t*) &*ui32vec.data(), ui32vec.size() }; u32string u32str2 = U"zß水🍌" vector

If a function never returns is it valid to omit a return statement

时光怂恿深爱的人放手 提交于 2019-12-11 03:43:30
问题 Consider a function foo() that might never exit: int foo(int n) { if(n != 0) return n; else for(;;); /* intentional infinite loop */ return 0; /* (*) */ } Is it valid C to omit the final return-statement? Will it evoke undefined behavior if I leave out that final statement? 回答1: Even if it does return without a return statement there is no UB unless you use the return value. 回答2: You can omit the last return statment which is after an indefinite loop. But you may be getting compilation