Pointers- Difference From Sending a Copy of the Object?

This is a discussion on Pointers- Difference From Sending a Copy of the Object? within the C++ Programming forums, part of the General Programming Boards category; What is the difference between sending a pointer as, say, an argument, vs. just sending a copy of the data? ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    Pointers- Difference From Sending a Copy of the Object?

    What is the difference between sending a pointer as, say, an argument, vs. just sending a copy of the data?

    Example:

    Code:
    void send_me_a_string_pointer(string* pointer){
    
    std::cout<<(*pointer)<<"\n";
    
    }
    
    
    vs.
    
    
    void send_me_a_string_copy(string copy){
    
    std::cout<<copy<<"\n";
    
    }
    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    You could do things like
    - declare your own class, with cout statements in the constructor/destructor
    - use sizeof on the parameter in the function
    - try calling your function taking a pointer with NULL as a parameter
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Sometimes, the object which you wish to pass to the function is very expensive to copy.
    What if the string passed to send_me_a_string_copy was the Declaration of Independence or something
    ridiculously long! It would waste memory as well as time to copy such a long chunk of data. In this case,
    the average sized string usually won't take too long to copy. However, if you are passing another object
    that is always bulky or you are calling this function 10,000 times, then you might stand to benefit
    from passing by pointer.

    What about passing by reference?
    It's true that passing by reference or constant-reference can have the same effect, but passing an
    object by pointer allows you to pass a NULL value if you wish. Passing by copy or reference REQUIRES
    you to actually possess a string object. It might be best for your function to have some sort of default
    behavior if you didn't have such an object.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    Quote Originally Posted by DrPiratePhD View Post
    What about passing by reference?
    It's true that passing by reference or constant-reference can have the same effect, but passing an
    object by pointer allows you to pass a NULL value if you wish. Passing by copy or reference REQUIRES
    you to actually possess a string object. It might be best for your function to have some sort of default
    behavior if you didn't have such an object.
    Not calling a function if you "didn't have such an object" would eliminate any such need.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    Quote Originally Posted by Shokwav View Post
    What is the difference between sending a pointer as, say, an argument, vs. just sending a copy of the data?
    I know for sure that you won't be able to tell the difference if displaying a string is all you're going to do with a copy or a pointer.

    You might need a copy of an object if a function argument is going to be changed. If you change a copy of the object, the changes will not persist past the end of the function, so it's one way to protect yourself. If you want the changes the function makes to persist after the function ends, pass-by-reference semantics (which includes the use of pointers) is necessary.
    Last edited by whiteflags; 05-30-2011 at 07:54 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,402
    Quote Originally Posted by grumpy View Post
    Not calling a function if you "didn't have such an object" would eliminate any such need.
    but I'm sure you realize that there are situations where you want to call the function whether you have the object or not, and handle a null in a special way.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    Quote Originally Posted by Elkvis View Post
    but I'm sure you realize that there are situations where you want to call the function whether you have the object or not, and handle a null in a special way.
    Sure but that is a design choice. Either way, a decision has to be made as to whether an object is valid. The only difference is whether caller or callee makes that determination. Sometimes it makes sense for the caller to do it (eg if a function is called many times and supplied with the same object) and sometimes for the callee to do it.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Pass by value (sending a copy as you call it) is typically slower performance-wise and does not allow you to modify the original thing passed,
    Pass by pointer is typically faster and does allow you to modify the thing passed in. It also allows the thing being passed in to be optional (i.e. it can be NULL).
    In this case the best option is to pass by reference, or more specifically by const-reference. No slow copying occurs, no modifications can be made, and the value is not optional.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy object
    By C_ntua in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2010, 01:16 AM
  2. copy of object?
    By rogster001 in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2009, 04:23 AM
  3. Sending Struct pointers through pipes 2
    By kiros88 in forum C Programming
    Replies: 1
    Last Post: 09-02-2009, 05:52 PM
  4. Creating an object in a dll & sending it to an exe file
    By SteveRodrigue in forum C++ Programming
    Replies: 6
    Last Post: 12-14-2006, 01:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21