passby pointer or reference?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

passby pointer or reference?

 
0
  #1
Oct 26th, 2007
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?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: passby pointer or reference?

 
0
  #2
Oct 26th, 2007
>> 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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: passby pointer or reference?

 
0
  #3
Oct 26th, 2007
Other than pointer arithmetics or something I don't know anything else... but the memory thing was what I wanted to know...
cheers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: passby pointer or reference?

 
0
  #4
Oct 26th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC