Guest User

Untitled

a guest
Aug 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. Do mainstream compilers convert passed-by-reference basic types into pass-by-copy?
  2. int foo(const int & i)
  3. {
  4. cout << i; // Do whatever read-only with i.
  5. }
  6.  
  7. int foo(const int i)
  8. {
  9. cout << i;
  10. }
  11.  
  12. int foo(const int i, int * ptr_i)
  13. {
  14. cout << i; // no dereferencement, therefore faster (?)
  15. // many more read-only operations with i.
  16. *ptr_i = 123;
  17. }
  18.  
  19. int foo(const int & i)
  20.  
  21. int foo(const int i)
  22.  
  23. int foo(const int &i, int *p)
  24. {
  25. *p = 42;
  26. cout << i; // prints 42
  27. return 0;
  28. }
  29.  
  30. int main()
  31. {
  32. int x = 5;
  33. foo(x, &x);
  34. return 0;
  35. }
  36.  
  37. int foo(const int i, int *p)
  38. {
  39. *p = 42;
  40. cout << i; // prints 5
  41. return 0;
  42. }
  43.  
  44. int main()
  45. {
  46. int x = 5;
  47. foo(x, &x);
  48. return 0;
  49. }
  50.  
  51. int vars[] = {1,2,3,12,3,23,1,213,231,1,21,12,213,21321,213,123213,213123};
  52.  
  53. int ok1(const int i){
  54. return sqrtl(vars[i]);
  55. }
  56.  
  57. int ok2(const int & i){
  58. return sqrtl(vars[i]);
  59. }
  60.  
  61. void main() {
  62. int i;
  63. std::cin >> i;
  64. //i = ok1(i);
  65. i = ok2(i);
  66. std::cout << i;
  67. }
  68.  
  69. i = ok1(i);
  70. 000D1014 mov ecx,dword ptr [i]
  71. 000D1017 fild dword ptr vars (0D3018h)[ecx*4]
  72. 000D101E call _CIsqrt (0D1830h)
  73. 000D1023 call _ftol2_sse (0D1840h)
  74.  
  75. i = ok2(i);
  76. 013A1014 mov ecx,dword ptr [i]
  77. 013A1017 fild dword ptr vars (13A3018h)[ecx*4]
  78. 013A101E call _CIsqrt (13A1830h)
  79. 013A1023 call _ftol2_sse (13A1840h)
  80.  
  81. int vars[] = {1,2,3,12,3,23,1,213,231,1,21,12,213,21321,213,123213,213123};
  82.  
  83. int ok1(const int i, int * pi) {
  84. *pi = 2;
  85. return sqrtl(vars[i]);
  86. }
  87.  
  88. int ok2(const int & i, int * pi) {
  89. *pi = 2;
  90. return sqrtl(vars[i]);
  91. }
  92.  
  93. void main() {
  94. int i;
  95. int * pi = &i;
  96. std::cin >> i;
  97. i = ok1(i, pi);
  98. //i = ok2(i, pi);
  99. std::cout << i;
  100. }
  101.  
  102. i = ok1(i, pi);
  103. 00891014 mov ecx,dword ptr [i]
  104. 00891017 fild dword ptr vars (893018h)[ecx*4] // access vars[i]
  105. 0089101E call _CIsqrt (891830h)
  106. 00891023 call _ftol2_sse (891840h)
  107.  
  108. i = ok2(i, pi);
  109. 011B1014 fild dword ptr [vars+8 (11B3020h)] // access vars[2]
  110. 011B101A call _CIsqrt (11B1830h)
  111. 011B101F call _ftol2_sse (11B1840h)
Add Comment
Please, Sign In to add comment