undefined-behavior

Sequence points and order of evaluation

此生再无相见时 提交于 2019-12-18 05:07:16
问题 I was reading through K&R and i came across this example about uncertainty in behavior while evaluating expression like a[i]=i++ ; The C99 spec in $6.5.2 says that Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored. The above example from K&R holds good on the first statement. Please explain how does it fail on the second

Segmentation fault in strcpy

血红的双手。 提交于 2019-12-18 04:54:39
问题 consider the program below char str[5]; strcpy(str,"Hello12345678"); printf("%s",str); When run this program gives segmentation fault. But when strcpy is replaced with following, program runs fine. strcpy(str,"Hello1234567"); So question is it should crash when trying to copy to str any other string of more than 5 chars length. So why it is not crashing for "Hello1234567" and only crashing for "Hello12345678" ie of string with length 13 or more than 13. This program was run on 32 bit machine

Why is undefined behaviour allowed in C

青春壹個敷衍的年華 提交于 2019-12-17 20:47:05
问题 I have been messing around trying to learn C lately. Coming from Java, it surprised me that you can perform certain operations declared as "undefined". This just seems extremely unsafe to me. I understand it is the programmer's responsibility not to perform undefined operations, but why is it even allowed to start with? Why does the compiler not catch, for instance, array indices out of bounds, or even dangling pointers? You just end up accessing blocks of memory you never should access, with

Undefined behavior causing time travel

巧了我就是萌 提交于 2019-12-17 20:20:27
问题 One example of this article from a msdn blog made me ticker: It says that this function: void unwitting(bool door_is_open) { if (door_is_open) { walk_on_in(); } else { ring_bell(); // wait for the door to open using the fallback value fallback = value_or_fallback(nullptr); wait_for_door_to_open(fallback); } } Can be optimized into this one: void unwitting(bool door_is_open) { walk_on_in(); } Because calling value_or_fallback(nullptr) is undefined behavior (this is proven earlier in the

Why does a program accessing illegal pointer to pointer not crash?

自作多情 提交于 2019-12-17 20:01:12
问题 A program accessing illegal pointer to pointer does not crash with SIGSEGV. This is not a good thing, but I’m wondering how this could be and how the process survived for many days in production. It is bewildering to me. I have given this program a go in Windows, Linux, OpenVMS, and Mac OS and they have never complained. #include <stdio.h> #include <string.h> void printx(void *rec) { // I know this should have been a ** char str[1000]; memcpy(str, rec, 1000); printf("%*.s\n", 1000, str);

Order of evaluation and undefined behaviour

元气小坏坏 提交于 2019-12-17 19:34:31
问题 Speaking in the context of the C++11 standard (which no longer has a concept of sequence points, as you know) I want to understand how two simplest examples are defined. int i = 0; i = i++; // #0 i = ++i; // #1 There are two topics on SO which explain those examples within the C++11 context. Here it was said that #0 invokes UB and #1 is well-defined. Here it was said that both examples are undefined. This ambiguity confuses me much. I've read this well-structured reference three times already

Assigning a reference by dereferencing a NULL pointer

坚强是说给别人听的谎言 提交于 2019-12-17 18:54:48
问题 int& fun() { int * temp = NULL; return *temp; } In the above method, I am trying to do the dereferencing of a NULL pointer. When I call this function it does not give exception. I found when return type is by reference it does not give exception if it is by value then it does. Even when dereferencing of NULL pointer is assinged to reference (like the below line) then also it does not give. int* temp = NULL: int& temp1 = *temp; Here my question is that does not compiler do the dereferencing in

Setting extra bits in a bool makes it true and false at the same time

我只是一个虾纸丫 提交于 2019-12-17 18:34:15
问题 If I get a bool variable and set its second bit to 1, then variable evaluates to true and false at the same time. Compile the following code with gcc6.3 with -g option, ( gcc-v6.3.0/Linux/RHEL6.0-2016-x86_64/bin/g++ -g main.cpp -o mytest_d ) and run the executable. You get the following. How can T be equal to true and false at the same time? value bits ----- ---- T: 1 0001 after bit change T: 3 0011 T is true T is false This can happen when you call a function in a different language (say

At what point in the loop does integer overflow become undefined behavior?

≯℡__Kan透↙ 提交于 2019-12-17 17:39:32
问题 This is an example to illustrate my question which involves some much more complicated code that I can't post here. #include <stdio.h> int main() { int a = 0; for (int i = 0; i < 3; i++) { printf("Hello\n"); a = a + 1000000000; } } This program contains undefined behavior on my platform because a will overflow on the 3rd loop. Does that make the whole program have undefined behavior, or only after the overflow actually happens ? Could the compiler potentially work out that a will overflow so

At what point in the loop does integer overflow become undefined behavior?

◇◆丶佛笑我妖孽 提交于 2019-12-17 17:39:07
问题 This is an example to illustrate my question which involves some much more complicated code that I can't post here. #include <stdio.h> int main() { int a = 0; for (int i = 0; i < 3; i++) { printf("Hello\n"); a = a + 1000000000; } } This program contains undefined behavior on my platform because a will overflow on the 3rd loop. Does that make the whole program have undefined behavior, or only after the overflow actually happens ? Could the compiler potentially work out that a will overflow so