include <iostream> cause different result


testPoint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <iostream>
using namespace std;
void swap(void *a,void *b)
{
     void* temp;
     temp =a;
     a=b;
     b=temp;
}
int main()
{
   int *a,*b;
   int c=8;
   int d=9;
    a=&(c);
    b=&(d);
    printf("C: a is %d ,b is %d \n",*a,*b);
//    cout<<"C++: a is "<< (*a) <<" b is "<< (*b) <<endl;
    swap(a,b);
    printf("C: a is %d ,b is %d \n",*a,*b);
 //   cout<<"C++: a is "<< (*a) <<" b is "<< (*b) <<endl;
}

c++ testPoint.c

C: a is 8 ,b is 9
C: a is 9 ,b is 8

when I did "//#include <iostream>",
the result is
C: a is 8 ,b is 9
C: a is 8 ,b is 9

my question is why this happen??
so confused.

my gcc version:
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~89/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)
It happens because your swap function doesn't actually do anything, it just moves the local variables around.
When you include <iostream>, std::swap is called instead of your function.
oh,
thanks athar.
Topic archived. No new replies allowed.