the code is problem in Offline compiler but no problem in online

bbcodes did not working on winpe(it didn't work on the creating first post! the area was covered in a yellow color.)

http://cpp.sh/9zyqi

But I did not ask about code problem to you!
The same code(did not change) is returning properly(correct) in the online compiler.

As well as, in online compilers without using gmp.h!

I want to know if the online compilers have a different setting.(they use gcc)

When I download mingw to pc from the official site and compile the code, it returns correct up to 7.

this code did not returned correct on any pc.
however it returns correct up to 7 number on pc with using visual studio or mingw.

it returned "Toplam: 943505408" for 8 number but it is false!
it returned "Toplam: 20473705" for 9 number but it is false!



Last edited on
Line 9 modifies and accesses a variable twice in the same line. C++ doesn't specify what order those modifications and accesses have to happen in. Your compiler should warn you with a message like "warning: operation on 'm' may be undefined".

---

Bu bir Google çevirisidir, lütfen herhangi bir hatayı affedin. Satır 9, aynı satırda bir değişkeni iki kez değiştirir ve erişir. C ++, bu değişikliklerin ve erişimlerin hangi sırada olması gerektiğini belirtmez. Derleyiciniz sizi "uyarı: 'm' üzerindeki işlem tanımsız olabilir" mesajı ile uyarmalıdır.

-Albatross
Last edited on
Thanks Albatross (4343)

I didn't get a warning from a compiler. compiled directly.

Actually, this warning is not important because it is returning correctly.

I keep your writings, archive them, to use them later in english learning. for thanks.


Bir sayi giriniz: 20
(20+0^2) = 20
(19+1^2) = 20
(18+2^2) = 22
(17+3^2) = 26
(16+4^2) = 32
(15+5^2) = 40
(14+6^2) = 50
(13+7^2) = 62
(12+8^2) = 76
(11+9^2) = 92
(10+10^2) = 110
(9+11^2) = 130
(8+12^2) = 152
(7+13^2) = 176
(6+14^2) = 202
(5+15^2) = 230
(4+16^2) = 260
(3+17^2) = 292
(2+18^2) = 326
(1+19^2) = 362
Toplam: 7709202550968287232 




Bir sayi giriniz: 8
(8+0^2) = 8
(7+1^2) = 8
(6+2^2) = 10
(5+3^2) = 14
(4+4^2) = 20
(3+5^2) = 28
(2+6^2) = 38
(1+7^2) = 50
Toplam: 9533440000 


these are correct

you can calculate the numbers. if you want
Last edited on
How did online compilers set up?
-----
or in other words(saying); online compilers compile a file differently?

I used this(g++ -o filename.exe filename.cpp)

I guess This error is because I am compiling a text file directly.
Last edited on
In C++, "Undefined" means that compilers are free to do whatever they want, even if it doesn't make sense to you. When you change and access a variable in the same expression, C++ does not require the changes and accesses to be in any specific order. cpp.sh's g++ selects an order that makes sense to you. Visual Studio and MinGW select a different order. This is actually easy for you to solve: just move some of your code to a new expression. Then, it should work correctly with all three compilers.

---

C ++ 'da "Undefined", derleyicilerin sizin için anlamlı olmasa bile istediklerini yapmakta serbest oldukları anlamına gelir. Bir değişkeni aynı ifadede değiştirip eriştiğinizde, C ++, değişikliklerin ve erişimlerin belirli bir sırada olmasını gerektirmez. cpp.sh's g ++, sizin için anlamlı olan bir sipariş seçer. Visual Studio ve MinGW farklı düzeni seçin. Bu, çözmeniz için aslında kolaydır: sadece kodunuzun bir kısmını yeni bir ifadeye taşıyın. Sonra, her üç derleyici ile düzgün çalışmalıdır.

1
2
const long r = m+(i*i);
return ( r * formul(--m, ++i) );


-Albatross

(Whew, it took some work to write that paragraph in a way that Google would translate it from English > Turkish > English without ruining it. I really hope it makes some sense in Turkish, even if it's likely a pretty broken translation.)
ya ben internetten farklı bir dosya çekiyorum!
Either I'm pulling a different file on the Internet!

yada online derleyiciler farklı ayarladı!
or online compilers set it up differently!

yada bu windows ile alakalı olabilir!
or it could be related to windows!

biraz sonra Linux'ta deneyeceğim.
I'll try it on Linux a little later.

it's same results again.

you can see this image!
https://imgur.com/a/6odEzXy
The thing that relevance with my code is problem of gnu and windows!
the thing about my code is
Benim kodum ile alakali olan sey ise windows ve gnu müşkilatı!

my code is working in linux! Why?

The meaning of müşkil(at) is problem(s). not difficult(s).

The translation engine pages that reflected me could be different !!
bana yansiyan ceviri motoru sayfalari farkli olabilir !!



Line 9 modifies and accesses a variable twice in the same line. C++ doesn't specify what order those modifications and accesses have to happen in. Your compiler should warn you with a message like "warning: operation on 'm' may be undefined".


Hey, Albatros!

this problem is related to using an outdated gnu gcc version on cpp.sh server.
bu problem ise cpp.sh serverda güncel olmayan bir gnu gcc version kullanılması ile alakalıdir.
Last edited on
You're not getting it. The problem is not the compilers, the problem is your code. This line
 
return ( (m+(i*i)) * formul((--m),(++i)));
has undefined behavior. What that means is the language specification allows compilers to generate any machine code for it.
To fix your code you have to break up that expression in a way that the order of operations becomes defined. For example, either
1
2
3
auto a = formul(--m, ++i);
auto b = m + i * i;
return a * b;
or
1
2
3
auto b = m + i * i;
auto a = formul(--m, ++i);
return a * b;
Actually, this warning is not important because it is returning correctly.

You're making a common mistake here. You're assuming that the code is correct because you get the right answer. But as others have pointed out, your code relies on undefined behavior.

So your code works by accident, not by design.
Undefined behaviour is a crapshoot, as I show in these results over here.
https://www.dreamincode.net/forums/topic/177775-comma-operator/page__view__findpost__p__1042539

> my code is working in linux! Why?
Never equate "working" with "bug-free".
Especially when other compilers are giving you a different answer.

The failure of any particular compiler to give you a compilation warning is not an argument of correctness on your part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ cat foo.c
int formul(int x, int y) {
  return x+y;
}

int foo ( int m, int i ) {
  return ( (m+(i*i)) * formul((--m),(++i)));
}
$ gcc -c -Wall -Wextra foo.c
foo.c: In function ‘foo’:
foo.c:6:32: warning: operation on ‘m’ may be undefined [-Wsequence-point]
   return ( (m+(i*i)) * formul((--m),(++i)));
                                ^
foo.c:6:38: warning: operation on ‘i’ may be undefined [-Wsequence-point]
   return ( (m+(i*i)) * formul((--m),(++i)));
                                      ^
foo.c:6:38: warning: operation on ‘i’ may be undefined [-Wsequence-point]

GCC isn't going to be printing these warnings "for fun", it prints them because there IS a problem that needs to be fixed.
Topic archived. No new replies allowed.