will this be compiler(M$ cl) bug or Memory leakage?
-
#include
#define unsafe(i) \
( (i) >= 0 ? (i) : -(i) )inline
int safe(int i)
{
return i >= 0 ? i : -i;
}
int f() {cout << "Called f()!" << endl; return 0;};
void userCode(int x)
{
int ans;
ans = unsafe(x++); // Error! x is incremented twice
cout << "[1] "<< x << endl;
// ans = (f()) >= 0 ? (f()) : -(f());
ans = unsafe(f()); // Danger! f() is called twice
cout << "[2] "<< x << endl;ans = safe(x++); // Correct! x is incremented once
cout << "[3] "<< x << endl;
ans = safe(f()); // Correct! f() is called once
cout << "[4] "<< x << endl;
}void main(void)
{
userCode(100);
}I was doing a test code for inline functions comparison to macros. The original code was shown as above, things seemed normal as the result showed two times of
f()
calls. The result looked like:[1] 102
Called f();
Called f();
[2] 102
[3] 103
Called f();
[4] 103However, after I tried to call
ans = (f()) >= 0 ? (f()) : -(f());
instead ofans = unsafe(f());
, I got a result of callingf()
FOUR times instead of TWO times as supposed. In this tread, I failed to attach a image of the result, which looked like:[1] 102
Called f();
Called f();
Called f();
Called f();
[2] 102
[3] 103
Called f();
[4] 103I ran the program twice both of times I got calling
f()
FOUR times, nevertheless, I could repeat the result after I re-compiled the program again with the original code. Could this be a compiler bug? DJ -
#include
#define unsafe(i) \
( (i) >= 0 ? (i) : -(i) )inline
int safe(int i)
{
return i >= 0 ? i : -i;
}
int f() {cout << "Called f()!" << endl; return 0;};
void userCode(int x)
{
int ans;
ans = unsafe(x++); // Error! x is incremented twice
cout << "[1] "<< x << endl;
// ans = (f()) >= 0 ? (f()) : -(f());
ans = unsafe(f()); // Danger! f() is called twice
cout << "[2] "<< x << endl;ans = safe(x++); // Correct! x is incremented once
cout << "[3] "<< x << endl;
ans = safe(f()); // Correct! f() is called once
cout << "[4] "<< x << endl;
}void main(void)
{
userCode(100);
}I was doing a test code for inline functions comparison to macros. The original code was shown as above, things seemed normal as the result showed two times of
f()
calls. The result looked like:[1] 102
Called f();
Called f();
[2] 102
[3] 103
Called f();
[4] 103However, after I tried to call
ans = (f()) >= 0 ? (f()) : -(f());
instead ofans = unsafe(f());
, I got a result of callingf()
FOUR times instead of TWO times as supposed. In this tread, I failed to attach a image of the result, which looked like:[1] 102
Called f();
Called f();
Called f();
Called f();
[2] 102
[3] 103
Called f();
[4] 103I ran the program twice both of times I got calling
f()
FOUR times, nevertheless, I could repeat the result after I re-compiled the program again with the original code. Could this be a compiler bug? DJWhat compiler are you using? I can't repro the problem on VC 6 SP 5. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer. -- Michael P. Butler in the Lounge
-
What compiler are you using? I can't repro the problem on VC 6 SP 5. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer. -- Michael P. Butler in the Lounge
Microsoft Compiler Ver 12.00.8168 from VC++ 6. I could not reproduce the problem either. So I guess the chance would be high that I accidently uncommented both
ans = (f()) >= 0 ? (f()) : -(f());
andans = unsafe(f());
. So far, I could not think out other reasons. DJ