Function calls order, is it a GCC bug? [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Undefined Behavior and Sequence Points
Order of function call
I have found a problem in the following code when I compile it with GCC 4.5.2 (x86 32 bits).
# include <stdio.h>
int function(int x){
printf("%d\n", x);
return 2*x + 1;
}
int main(){
int x = 3*function(1) + 4*function(2) + 5*function(3) + 6*function(4) + 7*function(5) + 8*function(6);
printf("%d\n", x);
return 0;
}
Expected output:
1
2
3
4
5
6
299
Actual GCC output:
1
2
4
3
6
5
299
I compiled the same code with clang and the output is the expected one.

No. The order the calls take place is not specified. They could take place in any order, but for each pair of function calls made here, one will see either all of the side-effects of the other, or none (i.e. they cannot run in parallel unless they have no side effects, and in this example they have a big side effect, printf).

Related

What if I print non-existing element of an array with a static variable? [duplicate]

This question already has answers here:
Is accessing a global array outside its bound undefined behavior?
(8 answers)
Closed 8 months ago.
I am a Computer science student and I feel like I am missing something very simple. Could you please help me out ?
#include <stdio.h>
void do_stuff(int *c) {
static int a = 0;
int b = 0;
a+=3;
printf("%d %d\n", *(c+a), c[b]);
printf("%d %d\n", *(c+6), c[b]);
printf("%d %d\n", c[6], c[b]);
}
int main (void){
static int array[6] = {5,17,23,42,127,3};
do_stuff(array);
do_stuff(array);
do_stuff(array);
return 0;
}
This is the outcome of this code:
42 5
3 5
3 5
6 5
6 5
6 5
0 5
9 5
9 5
I don't get, why it is 6 5 for the second do_stuff(array).
I thought it would be 0 5 for every print of second and third do_stuff(array). Then I thought maybe It was something to do with static a and I tried it without a variable, so just with the number 6. But the answer was the same.
Could you please explain the reason for the outputs with the bold font?
Thank you for your help.
In an array with 6 elements, using index 6 will read the first position after the array, which is not 0. The read value depends on the underlying architecture and compiler implementation; depending if such memory position is mapped to your process or not, the OS may kill your application.
In your case, it looks like in memory you have the value of variable a stored just after the input array of do_stuff(), that's why printing c[6] basically prints the value of a.
Of course this is best described as undefined behavior and the source code is basically incorrect.

I'm not able to figure out how this code produces this output [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 1 year ago.
I'm not able to understand, why this code produces this output. Can anyone help me with this??
#include <stdio.h>
int main( void )
{
int num = 5;
printf( "%d %d %d", num, ++num, num++ );
}
The output will be 7 7 5 (and NOT 5 6 7)
Making side effects happen in the same order as in some other compiler.
It is never safe to depend on the order of evaluation of side effects. For example, a function call like this may very well behave differently from one compiler to another:
void func (int, int);
int i = 2;
func (i++, i++);
There is no guarantee (in either the C or the C++ standard language definitions) that the increments will be evaluated in any particular order. Either increment might happen first. func might get the arguments ‘2, 3’, or it might get ‘3, 2’, or even ‘2, 2’.
https://gcc.gnu.org/onlinedocs/gcc/Non-bugs.html

I need help understanding the replacement of TWO in the below code [duplicate]

This question already has answers here:
#define used with operators [duplicate]
(4 answers)
Closed 3 years ago.
I ran it through Code::Blocks and it shows me the final answer 1. How is TWO then replaced in the " i = i-2*TWO" statement and why is like that?
The code is part of a homework exercise I'm trying to solve/understand:
#include <stdio.h>
#define ONE 1
#define TWO ONE + ONE
int main(void) {
int i = 2;
i = i - 2 * TWO;
printf("%d\n", i);
return 0;
}
Just make the substitution yourself.
i = i - 2 * TWO;
is
i = i - 2 * ONE + ONE;
So you have
i = 2 - 2 * 1 + 1;
So i will have the value 1.
If you want to get the result equal to -2 then rewrite the macro like
#define TWO ( ONE + ONE )
You can see what the compiler sees as final expression by running gcc -E <filename.c>. That should help in understanding why the result is 1.

C - Output explanation of printf("%d %d\n",k=1,k=3); [duplicate]

This question already has answers here:
Explain the order of evaluation in printf [duplicate]
(5 answers)
Closed 6 years ago.
How to explain the output of the below code:
include <stdio.h>
int main(void) {
int k;
printf("%d %d\n",k=1,k=3);
return 0;
}
Ideone Link
My thinking was that 1 will be assigned to k variable and then 1 would be printed. Similarly 3 will be assigned to k and output will be 3.
Expected Output
1 3
Actual Output
1 1
I am extrapolating from
int a;
if (a = 3) {
...
}
is equal to
if (3) {
...
}
Please let me know where am I going wrong?
The problem is, the order of evaluation of function arguments are not defined, and there's no sequence point between the evaluation or arguments. So, this statement
printf("%d %d\n",k=1,k=3)
invokes undefined behavior, as you're trying to modify the same variable more than once without a sequence point in between.
Once a program invoking UB is run and (if) there's an output, it cannot be justified anyway, the output can be anything.
I expect the reason you're seeing 1 1 is because the two assignment statements are happening control is passed to printf.
printf("%d %d\n",k=1,k=3);
So in response to the down-votes, yes, this this is undefined behavior, and therefore you shouldn't count on this behavior continuing. However, in terms of identifying why the output was 1 1 and not 1 3, we could infer that the assignment of 3 may have been stomped on by a subsequent assignment of 1.
When printf is invoked, the call stack contains two entries containing the final value of k, which is 1.
You can test this out by replacing those with a function that prints something when it executes.
Sample Code:
#include <stdio.h>
int test(int n) {
printf("Test(%d)\n", n);
return n;
}
int main(void) {
int k;
printf("%d %d\n",k=test(1), k=test(3));
return 0;
}
Output:
Test(3)
Test(1)
1 1

gcc giving different result for windows and linux [duplicate]

This question already has answers here:
Why is this Undefined Behavior?
(3 answers)
Closed 7 years ago.
#include<stdio.h>
int main()
{
int x=2;
x=x++;
printf("%d",x);
return 0;
}
Output according to my logic :
2
Output on windows :
3
Output on Linux:
2
Why is windows giving 3 as output.
From what I understand x++ increments 2 to 3 but return back 2. So x should have 2.Is windows evaluating this anything different.
Similarly:
#include<stdio.h>
int main()
{
int x=2,y=4;
x=x++ + ++y;
printf("%d %d",x,y);
return 0;
}
Output according to me :
7 5
Output in windows :
8 5
Output on Linux :
7 5
Again the same situation.
Please help.....
x = x++ is undefined behavior so the two compilers are generating two different pieces of code.
Just x++ should suffice for your first piece of code.
Here is a question with your exact problem for the second piece of code

Resources