Pre-increment vs post-increment in terms of sequence points

In this answer there're some examples of well-defined and undefined expressions. I'm particularly interested in two of them: (6) i = i++ + 1; // Undefined Behaviour (7) i = ++i + 1; // Well-defined Behaviour This means that there's a difference betwe

Why does this type of recursion work in C ++?

This question already has an answer here: Why does flowing off the end of a non-void function without returning a value not produce a compiler error? 8 answers I made a recursive C++ program that looks like this: using namespace std; #include <iostre

Is it legal for an object to replace itself?

I have a feeling that this is not really legal or involves undefined behaviour, where the member object replaces itself with another instance. I searched here on StackOverflow and Google for quite a while and found mainly people who wanted to delete

Negative time conversion gives random values

I'm trying to calculate a difference between two points in time in C and because those points are before 1900, I need to use negative values. While converting is supposed to write the variable jahr_tod_negative into ts.tm_year, but almost every time

Variable initialization with itself

Is it safe to write such code? #include <iostream> int main() { int x = x-x;// always 0 int y = (y)? y/y : --y/y // always 1 } I know there is undefined behaviour, but isn't it in this case just a trash value? If it is, then same value minus same is

Is i = i + 1 an indefinite behavior?

I'm using codeblocks and it is giving a different output to other compilers and I can't find a solution to it.What's the undefined behaviour in this program and is there any solution to avoid it? This is the code to print the nth number in a number s

2D array indexing - indefinite behavior?

I've recently got into some pieces of code doing some questionable 2D arrays indexing operations. Considering as an example the following code sample: int a[5][5]; a[0][20] = 3; a[-2][15] = 4; a[5][-3] = 5; Are the indexing operations above subject t

Does this lambda c ++ 11 code invoke undefined behavior?

C++11 ยง5.1.2/6 The closure type for a lambda-expression with no lambda-capture has a [...] conversion function [...]. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invokin

How both (i + 1) & lt; ii and (i + 1) & gt; Are both true?

I'm currently learning C program But I came across some weird behavior I expected one result but two results is printed like this $ ./a.out yes1 0x80000000 yes3 0x80000000 How could possible that? I can't understand the result. OS : x86_64 Ubuntu Lin

Sequencing the assignment operator in C11 expressions

Introduction The C11 standard (ISO/IEC 9899:2011) has introduced a new definition of side effect sequencing within an expression (see related question). The sequence point concept has been complemented with sequenced before and sequenced after relati

Are all functions in C / C ++ supposed to return?

I was reading this paper on undefined behaviour and one of the example "optimisations" looks highly dubious: if (arg2 == 0) ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* No overflow is possible */

The program did not crash when the buffer overflowed

I want to read a string from keyboard and store in buf . I set a char buf[6] array , this array at most can store 5 characters and \0 . Then I type 123 456 789 it contain 11 characters and a \0 , the program still can run , but if I type a longer str

Uninitialized variable in C

I'm a little bit confused. As far as I know, if you declare an int in C, without initializing it, for e.g: int x; so its value is indeterminate. So if we try to use it or should have undefined behavior. So if i'm running the following code in VS2010

Difference between stack and heap

Please tell me the difference between stack and heap with respect to below code int main() { int arr[3]; int *a; arr [5] = 6; // out of bound but it will not give error. arr [3000] = 8 ; //SIGSEGV a = malloc (sizeof (int)); a[4] = 6; a[4000] = 8; //N

What are "partially overlapping objects"?

I was just going through all the possible Undefined Behaviours in this thread, and one of them is The result of assigning to partially overlapping objects I wondered if anyone could give me a definition of what "partially overlapping objects" ar

C / C ++: Is this behavior indefinite? (2D Tables)

Is it undefined behavior if I go through the elements of a 2D array in the following manner? int v[5][5], i; for (i = 0; i < 5*5; ++i) { v[i] = i; } Then again, does it even compile? (I can't try it right now, I'm not at home.) If it doesn't, then im

Validity of the code

Consider the following code : void populate(int *arr) { for(int j=0;j<4;++j) arr[j]=0; } int main() { int array[2][2]; populate(&array[0][0]); } There was a discussion regarding this on a local community whether the code is valid or not(Am I suppos

unexpected result when adding to the pointer

Someone told me this bit of code prints 29. Why is that? int *a = 17; printf("%d", a+3); Because when you add to a pointer it adds the object size. In this case the object size is 4 (sizeof(int) == 4) -- so 17 + 3 * 4 == 29.