This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 2 years ago.
I was actually thinking that this program should throw a Compilation Error(coz, I am passing values to swap method and not &a, &b) but I was shocked to see that it got executed Successfully.
So, am posting this to know how/why it got executed without any error.
#include <iostream>
using namespace std;
void swap(int* x, int* y)
{
int z = *x;
*x = *y;
*y = z;
}
int main()
{
int a = 45, b = 35;
cout << "Before Swap\n";
cout << "a = " << a << " b = " << b << "\n";
swap(a, b);
cout << "After Swap with pass by pointer\n";
cout << "a = " << a << " b = " << b << "\n";
}
As is said so often on this site, using namespace std; is a bad idea.
You called std::swap<int>
Related
This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 1 year ago.
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
const int a = 1;
int* b;
b = (int*)&a;
*b = 6;
cout << &a << endl;
cout << b << endl;
cout << a << endl;
cout << *(&a) << endl;
cout << *b << endl;
return 0;
}
The output is as follows:
0x7ffc42aeb464
0x7ffc42aeb464
1
1
6
As the result, the memory address is same but the value is different. What is the reason?
Because you have cast it to non-const in (int*)&a. As I remember, this is undefined behavior.
I'm following simple C++ tutorial.
#include <iostream>
using namespace std;
int main()
{
int a = 1, b = 2;
cout << "Before swapping " << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(a,b);
cout << endl;
cout << "After swapping " << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
void swap(int &n1, int &n2)
{
int temp;
temp = n1;
n1 = n2;
n2 = temp;
}
The above code works fine (both g++ and icc), but if I were to use pointers in the functions the code fails if I do not include the prototype at the head of the program.
#include <iostream>
using namespace std;
void swap(int*, int*); // The code fails if I comment this line.
int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(&a, &b);
cout << endl;
cout << "After swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
void swap(int* n1, int* n2)
{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
As far as I know, C++ compiling process is top-bottom, so the 2nd code seems more reasonable in which the information of the function is provided before int main() is encountered. My question is, why the 1st code works fine even without the knowledge of function before int main()?
The issue with the first program is you're not actually calling your own swap function. At the top of the file, you have:
using namespace std;
which brings std::swap into scope and that's the function that you're actually calling. If you put a cout statement in your own swap you'll see that it's never actually called. Alternatively, if you declare your swap before main, you'll get an ambiguous call.
Note that this code is not required to behave like this, since iostream doesn't necessarily bring std::swap into scope, in which case you'll get the error that there is no swap to call.
In the second program, the call to swap(&a, &b) fails because there is no overload of std::swap that accepts 2 temporary pointers. If you declare your swap function before the call in main, then it calls your own function.
The real bug in your code is the using namespace std;. Never do that and you'll avoid issues of this nature.
The reason why the first version works is because it doesn't call your swap(...) function at all. The namespace std provides - Edit: depending on the headers you (and the standard headers themselves) include - swap(...) functions for various types and integers are one of them. If you would remove using namespace std you would have to type std::swap(...) to achieve the same effect (same goes for std::cout, std::endl).
That's one reason why using namespace is a double-edged sword for beginners in my opinion but that's another topic.
Your code is fine; but you're right, it fails if you comment on the line you point to.
But actually, as the others tell you, there is a Swap function in c ++, so it doesn't matter if you create a prototype of the function and do it later because the compiler calls its own swap function.
But since swap works for any data type, except for pointers, then you will understand the reason for your problem, since in this case you do have to create your own swap function that accepts pointers as parameters.
Just move your function above main to make it work correctly, nothing more:
#include <iostream>
using namespace std;
//void swap(int*, int*); // The code fails if I comment this line.
void swap(int* n1, int* n2)
{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(&a, &b);
cout << endl;
cout << "After swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
#include <iostream>
using namespace std;
void swap(int, int);
int main()
{
int a=10;
int b=20;
swap (a, b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
void swap(int x, int y)
{
int t;
t = x;
x = y;
y = t;
}
those code above can't swap the value of a and b.
but my question is , when I forgot to type the third line "void swap(int, int);
" , the values of a and b swaped !! why?
It's because you have
using namespace std;
At the beginning of your source code.
This is a a bad programming practice, whose consequences you just experienced, first hand. You told the compiler that you want to invoke std::swap, without having any clue that you actually did that.
It's ironical, because you version of swap() won't work right, but std::swap does; so you were operating under the mistaken impression that your code was working, when it didn't.
Never use "using namespace std;" with your code. Simply forget that this part of the C++ language ever existed.
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
system("pause");
swap(a, b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
system("pause");
return 0;
}
void swap is unnecessary
If you put the function definition above main then you don't need a prototype otherwise you do need it and the compiler should give you an error if you don't have a prototype
This question already has an answer here:
Why does passing object reference arguments to thread function fails to compile?
(1 answer)
Closed 7 years ago.
My code goes like this :-
#include <iostream>
#include <thread>
using namespace std;
void swapno (int &a, int &b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int x=5, y=7;
cout << "x = " << x << "\ty = " << y << "\n";
thread t (swapno, x, y);
t.join();
cout << "x = " << x << "\ty = " << y << "\n";
return 0;
}
This code fails to compile. Can anyone help me out why ?
Not only this code but the code in this also failed to send std::unique_ptr by reference. What's wrong with std::thread ?
The problem is that std::thread copies its arguments and stores them internally. If you want to pass an argument by reference you need to use the std::ref or std::cref functions to create reference wrappers.
Like
thread t (swapno, std::ref(x), std::ref(y));
You can do this, instead:
#include <iostream>
#include <thread>
void swapno (int *a, int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int x = 5, y = 7;
std::cout << "x = " << x << "\ty = " << y << "\n";
std::thread t (swapno, &x, &y);
t.join();
std::cout << "x = " << x << "\ty = " << y << "\n";
return 0;
}
You should achieve the same result ;)
This question already has answers here:
possible assignment in condition (C)
(2 answers)
Closed 8 years ago.
I'm new to StackExchange, but I have a simple class that seems to not return to the correct result when I run it.
Here is the code:
#include <iostream>
using namespace std;
int thisIsHowYouIfLikeABoss2(int, int);
int main()
{
cout << "One." << endl;
thisIsHowYouIfLikeABoss2(9, 9);
cout << "Two." << endl;
thisIsHowYouIfLikeABoss2(4, 9);
return 0;
}
int thisIsHowYouIfLikeABoss2 (int x, int y)
{
cout << "Welcome to the thisIsHowYouIfLikeABoss(), where I calculate if x = y easily." << endl;
if (x = y)
{
cout << "X is Y." << endl;
}
if (x != y)
{
cout << "X is not Y" << endl;
}
}
My compiler is GNU C++ Compiler for Ubuntu,if anyone was wondering.
= is the assignment operator, not the relational equality operator, which is ==.
Change your code to this:
if (x == y)
{
cout << "X is Y." << endl;
}
Protip: if you annotate your function's parameters with const then the compiler would have given you an error with that expression:
int thisIsHowYouIfLikeABoss2( const int x, const int y )
(Unlike in C# and Java, const in C++ does not mean the value is a compile-time fixed-value or literal, so you can use const with variables).