Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Do mainstream compilers convert passed-by-reference basic types into pass-by-copy?
- int foo(const int & i)
- {
- cout << i; // Do whatever read-only with i.
- }
- int foo(const int i)
- {
- cout << i;
- }
- int foo(const int i, int * ptr_i)
- {
- cout << i; // no dereferencement, therefore faster (?)
- // many more read-only operations with i.
- *ptr_i = 123;
- }
- int foo(const int & i)
- int foo(const int i)
- int foo(const int &i, int *p)
- {
- *p = 42;
- cout << i; // prints 42
- return 0;
- }
- int main()
- {
- int x = 5;
- foo(x, &x);
- return 0;
- }
- int foo(const int i, int *p)
- {
- *p = 42;
- cout << i; // prints 5
- return 0;
- }
- int main()
- {
- int x = 5;
- foo(x, &x);
- return 0;
- }
- int vars[] = {1,2,3,12,3,23,1,213,231,1,21,12,213,21321,213,123213,213123};
- int ok1(const int i){
- return sqrtl(vars[i]);
- }
- int ok2(const int & i){
- return sqrtl(vars[i]);
- }
- void main() {
- int i;
- std::cin >> i;
- //i = ok1(i);
- i = ok2(i);
- std::cout << i;
- }
- i = ok1(i);
- 000D1014 mov ecx,dword ptr [i]
- 000D1017 fild dword ptr vars (0D3018h)[ecx*4]
- 000D101E call _CIsqrt (0D1830h)
- 000D1023 call _ftol2_sse (0D1840h)
- i = ok2(i);
- 013A1014 mov ecx,dword ptr [i]
- 013A1017 fild dword ptr vars (13A3018h)[ecx*4]
- 013A101E call _CIsqrt (13A1830h)
- 013A1023 call _ftol2_sse (13A1840h)
- int vars[] = {1,2,3,12,3,23,1,213,231,1,21,12,213,21321,213,123213,213123};
- int ok1(const int i, int * pi) {
- *pi = 2;
- return sqrtl(vars[i]);
- }
- int ok2(const int & i, int * pi) {
- *pi = 2;
- return sqrtl(vars[i]);
- }
- void main() {
- int i;
- int * pi = &i;
- std::cin >> i;
- i = ok1(i, pi);
- //i = ok2(i, pi);
- std::cout << i;
- }
- i = ok1(i, pi);
- 00891014 mov ecx,dword ptr [i]
- 00891017 fild dword ptr vars (893018h)[ecx*4] // access vars[i]
- 0089101E call _CIsqrt (891830h)
- 00891023 call _ftol2_sse (891840h)
- i = ok2(i, pi);
- 011B1014 fild dword ptr [vars+8 (11B3020h)] // access vars[2]
- 011B101A call _CIsqrt (11B1830h)
- 011B101F call _ftol2_sse (11B1840h)
Add Comment
Please, Sign In to add comment