943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1621
  • C++ RSS
Oct 26th, 2007
0

passby pointer or reference?

Expand Post »
One function is pass by pointer which stores the reference, the other is just pass by a reference... are theses methods equvilent or does one method require more memory than the other?

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void cubeByRef(int *num);
  5. void cubeByRef2(int &num);
  6.  
  7. int main(){
  8. int a = 2;
  9. cubeByRef(&a);
  10. cout << "cubeByRef : " << a << endl;
  11.  
  12. a = 2;
  13. cubeByRef2(a);
  14. cout << "cubeByRef2: " << a << endl;
  15.  
  16. return 0;
  17. }
  18.  
  19. void cubeByRef(int *num){
  20. *num = *num * *num * *num;
  21. }
  22.  
  23. void cubeByRef2(int &num){
  24. num = num * num * num;
  25. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Oct 26th, 2007
0

Re: passby pointer or reference?

>> are theses methods equvilent
yes (almost). There are a couple things that can be done with pointer num that can not be done with the reference.

>> or does one method require more memory than the other?
no
Last edited by Ancient Dragon; Oct 26th, 2007 at 9:24 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 26th, 2007
0

Re: passby pointer or reference?

Other than pointer arithmetics or something I don't know anything else... but the memory thing was what I wanted to know...
cheers
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Oct 26th, 2007
0

Re: passby pointer or reference?

The internal code produced by your compiler most likely is the same for both functions, so there would be no difference in memory requirements. The difference between the two functions is the c++ language requirements, compilers may implement them however it wants to.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: array and structures.
Next Thread in C++ Forum Timeline: learning WIN32 GDI





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC